Skip to content

Commit

Permalink
RATESWSX-309: improve payment filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Sep 3, 2024
1 parent 6733ab0 commit e788873
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 55 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 49 additions & 41 deletions src/Components/Checkout/Service/PaymentFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
22 changes: 8 additions & 14 deletions src/Components/Checkout/Service/PaymentMethodRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down

0 comments on commit e788873

Please sign in to comment.