From e78887327d36672c3da5ae4a6ef82e5171893a67 Mon Sep 17 00:00:00 2001 From: Frederik Rommel Date: Tue, 3 Sep 2024 09:44:43 +0200 Subject: [PATCH] RATESWSX-309: improve payment filter --- CHANGELOG.md | 2 + .../Checkout/Service/PaymentFilterService.php | 90 ++++++++++--------- .../Checkout/Service/PaymentMethodRoute.php | 22 ++--- 3 files changed, 59 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 826c55f9..2540e184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## WIP +* RATESWSX-309: improve payment filter / fix bug if no payment methods are available + ## Version 7.0.1 - Released on 2024-06-07 RATESWSX-303: fix admin-session logout endless redirect & make admin-session urls more unified diff --git a/src/Components/Checkout/Service/PaymentFilterService.php b/src/Components/Checkout/Service/PaymentFilterService.php index 9177eb45..452a534b 100644 --- a/src/Components/Checkout/Service/PaymentFilterService.php +++ b/src/Components/Checkout/Service/PaymentFilterService.php @@ -35,57 +35,65 @@ public function __construct( ) { } - public function filterPaymentMethods(PaymentMethodCollection $paymentMethodCollection, SalesChannelContext $salesChannelContext, OrderEntity $order = null): PaymentMethodCollection + public function filterPaymentMethods(PaymentMethodCollection $paymentMethodCollection, SalesChannelContext $salesChannelContext, OrderEntity $order = null): void { - // make sure that not legacy payment methods are in collection. - $paymentMethodCollection = $paymentMethodCollection->filter(static fn (PaymentMethodEntity $entity): bool => $entity->getHandlerIdentifier() !== LegacyPaymentHandler::class); - - return $paymentMethodCollection->filter(function (PaymentMethodEntity $paymentMethod) use ($salesChannelContext, $order): ?bool { - if (!MethodHelper::isRatepayMethod($paymentMethod->getHandlerIdentifier())) { - // payment method is not a ratepay method - so we won't check it. - return true; + foreach ($paymentMethodCollection->getElements() as $key => $paymentMethod) { + if (!$this->isPaymentMethodAvailable($paymentMethod, $salesChannelContext, $order)) { + $paymentMethodCollection->remove($key); } + } + } - if (!$order instanceof OrderEntity) { - $customer = $salesChannelContext->getCustomer(); - if (!$customer instanceof CustomerEntity || - !$customer->getActiveBillingAddress() instanceof CustomerAddressEntity || - !$customer->getActiveShippingAddress() instanceof CustomerAddressEntity - ) { - return false; - } - } + private function isPaymentMethodAvailable(PaymentMethodEntity $paymentMethod, SalesChannelContext $salesChannelContext, OrderEntity $order = null): bool + { + if ($paymentMethod->getHandlerIdentifier() === LegacyPaymentHandler::class) { + return false; + } - $searchService = $order instanceof OrderEntity ? $this->profileByOrderEntity : $this->profileBySalesChannelContext; - $profileConfig = $searchService->search( - $searchService->createSearchObject($order ?? $salesChannelContext)->setPaymentMethodId($paymentMethod->getId()) - )->first(); + if (!MethodHelper::isRatepayMethod($paymentMethod->getHandlerIdentifier())) { + // payment method is not a ratepay method - so we won't check it. + return true; + } - if ($profileConfig === null) { - // no profile config for this sales channel has been found + if (!$order instanceof OrderEntity) { + $customer = $salesChannelContext->getCustomer(); + if (!$customer instanceof CustomerEntity || + !$customer->getActiveBillingAddress() instanceof CustomerAddressEntity || + !$customer->getActiveShippingAddress() instanceof CustomerAddressEntity + ) { return false; } + } - /** @var ProfileConfigMethodCollection $methodConfigs */ - $methodConfigs = $profileConfig->getPaymentMethodConfigs()->filterByMethod($paymentMethod->getId()); - $methodConfig = $methodConfigs->first(); + $searchService = $order instanceof OrderEntity ? $this->profileByOrderEntity : $this->profileBySalesChannelContext; + $profileConfig = $searchService->search( + $searchService->createSearchObject($order ?? $salesChannelContext)->setPaymentMethodId($paymentMethod->getId()) + )->first(); - if (!$methodConfig instanceof ProfileConfigMethodEntity) { - // no profile method config is found - return null; - } + if ($profileConfig === null) { + // no profile config for this sales channel has been found + return false; + } + + /** @var ProfileConfigMethodCollection $methodConfigs */ + $methodConfigs = $profileConfig->getPaymentMethodConfigs()->filterByMethod($paymentMethod->getId()); + $methodConfig = $methodConfigs->first(); + + if (!$methodConfig instanceof ProfileConfigMethodEntity) { + // no profile method config is found + return false; + } - // trigger event to filter the payment methods - /** @var RatepayPaymentFilterEvent $filterEvent */ - $filterEvent = $this->eventDispatcher->dispatch(new RatepayPaymentFilterEvent( - $paymentMethod, - $profileConfig, - $methodConfig, - $salesChannelContext, - $order - )); + // trigger event to filter the payment methods + /** @var RatepayPaymentFilterEvent $filterEvent */ + $filterEvent = $this->eventDispatcher->dispatch(new RatepayPaymentFilterEvent( + $paymentMethod, + $profileConfig, + $methodConfig, + $salesChannelContext, + $order + )); - return $filterEvent->isAvailable(); - }); + return $filterEvent->isAvailable(); } } diff --git a/src/Components/Checkout/Service/PaymentMethodRoute.php b/src/Components/Checkout/Service/PaymentMethodRoute.php index 002eba35..a6ab57ae 100644 --- a/src/Components/Checkout/Service/PaymentMethodRoute.php +++ b/src/Components/Checkout/Service/PaymentMethodRoute.php @@ -36,9 +36,9 @@ public function getDecorated(): AbstractPaymentMethodRoute return $this->innerService; } - public function load(Request $request, SalesChannelContext $salesChannelContext, Criteria $criteria): PaymentMethodRouteResponse + public function load(Request $request, SalesChannelContext $context, Criteria $criteria): PaymentMethodRouteResponse { - $response = $this->innerService->load($request, $salesChannelContext, $criteria); + $response = $this->innerService->load($request, $context, $criteria); $currentRequest = $this->requestStack->getCurrentRequest(); if (!$currentRequest instanceof Request) { @@ -53,21 +53,15 @@ public function load(Request $request, SalesChannelContext $salesChannelContext, /** @var OrderEntity|null $order */ $order = $this->orderRepository->search( CriteriaHelper::getCriteriaForOrder($orderId), - $salesChannelContext->getContext() + $context->getContext() )->first(); } - if ($order !== null || $request->query->getBoolean('onlyAvailable', false)) { - $paymentMethods = $this->paymentFilterService->filterPaymentMethods( - $response->getPaymentMethods(), - $salesChannelContext, - $order - ); - - $criteria->setIds($paymentMethods->getIds()); - - return $this->innerService->load($request, $salesChannelContext, $criteria); - } + $this->paymentFilterService->filterPaymentMethods( + $response->getPaymentMethods(), + $context, + $order + ); return $response; }