Skip to content

Commit

Permalink
PAYOSWXP-154: payment-filter: do not replace response, just remove me…
Browse files Browse the repository at this point in the history
…thods & remove onlyAvailable-Flag
  • Loading branch information
rommelfreddy authored and janteuber committed Aug 15, 2024
1 parent 4e06b97 commit 4560b8d
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ public function onCartLoaded(CheckoutCartPageLoadedEvent|OffcanvasCartPageLoaded
return;
}

$filteredPaymentMethods = $this->paymentFilterService->filterPaymentMethods(
new PaymentMethodCollection([(new PaymentMethodEntity())->assign([
'id' => PayoneAmazonPayExpress::UUID,
'handlerIdentifier' => PayoneAmazonPayExpressPaymentHandler::class,
])]),
$paymentMethods = new PaymentMethodCollection([(new PaymentMethodEntity())->assign([
'id' => PayoneAmazonPayExpress::UUID,
'handlerIdentifier' => PayoneAmazonPayExpressPaymentHandler::class,
])]);

$this->paymentFilterService->filterPaymentMethods(
$paymentMethods,
new PaymentFilterContext(
salesChannelContext: $event->getSalesChannelContext(),
currency: $event->getSalesChannelContext()->getCurrency(),
Expand All @@ -84,7 +86,7 @@ public function onCartLoaded(CheckoutCartPageLoadedEvent|OffcanvasCartPageLoaded
)
);

if (!$filteredPaymentMethods->has(PayoneAmazonPayExpress::UUID)) {
if (!$paymentMethods->has(PayoneAmazonPayExpress::UUID)) {
return;
}

Expand Down
14 changes: 7 additions & 7 deletions src/Components/PaymentFilter/DefaultPaymentFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function __construct(
final public function filterPaymentMethods(
PaymentMethodCollection $methodCollection,
PaymentFilterContext $filterContext
): PaymentMethodCollection {
): void {
$supportedPaymentMethods = $this->getSupportedPaymentMethods($methodCollection);
if ($supportedPaymentMethods->getElements() === []) {
return $methodCollection;
return;
}

$currency = $filterContext->getCurrency();
Expand Down Expand Up @@ -70,10 +70,8 @@ final public function filterPaymentMethods(

$this->additionalChecks($methodCollection, $filterContext);
} catch (PaymentMethodNotAllowedException $paymentMethodNotAllowedException) {
$methodCollection = $this->removePaymentMethods($methodCollection, $paymentMethodNotAllowedException->getDisallowedPaymentMethodCollection());
$this->removePaymentMethods($methodCollection, $paymentMethodNotAllowedException->getDisallowedPaymentMethodCollection());
}

return $methodCollection;
}

protected function additionalChecks(
Expand All @@ -91,11 +89,13 @@ private function getSupportedPaymentMethods(PaymentMethodCollection $paymentMeth
: $paymentMethod->getHandlerIdentifier() === $this->paymentHandlerClass);
}

private function removePaymentMethods(PaymentMethodCollection $paymentMethodCollection, ?PaymentMethodCollection $itemsToRemove = null): PaymentMethodCollection
private function removePaymentMethods(PaymentMethodCollection $paymentMethodCollection, ?PaymentMethodCollection $itemsToRemove = null): void
{
$itemsToRemove = $itemsToRemove ?: $this->getSupportedPaymentMethods($paymentMethodCollection);

return $paymentMethodCollection->filter(static fn (PaymentMethodEntity $entity) => !$itemsToRemove->has($entity->getUniqueIdentifier()));
foreach ($itemsToRemove->getIds() as $id) {
$paymentMethodCollection->remove($id);
}
}

private function validateAddress(CustomerAddressEntity|OrderAddressEntity|null $address): void
Expand Down
37 changes: 13 additions & 24 deletions src/Components/PaymentFilter/FilteredPaymentMethodRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,23 @@ public function load(Request $request, SalesChannelContext $context, Criteria $c
$response = $this->getDecorated()->load($request, $context, $criteria);

$currentRequest = $this->requestStack->getCurrentRequest();
if (!$currentRequest) {
return $response;
}

if ($request->query->getBoolean('onlyAvailable') || $request->request->getBoolean('onlyAvailable')) {
$orderId = $currentRequest->get('orderId');
if ($orderId) {
$order = $this->orderFetcher->getOrderById($orderId, $context->getContext());
if (!$order) {
throw new \RuntimeException('order not found!');
}
$filterContext = $this->paymentFilterContextFactory->createContextForOrder($order, $context);
} else {
$filterContext = $this->paymentFilterContextFactory->createContextForCart(
$this->cartService->getCart($context->getToken(), $context),
$context
);
$orderId = $currentRequest?->get('orderId');
if ($orderId) {
$order = $this->orderFetcher->getOrderById($orderId, $context->getContext());
if (!$order) {
throw new \RuntimeException('order not found!');
}

$paymentMethods = $response->getPaymentMethods();

$paymentMethods = $this->iterablePaymentFilter->filterPaymentMethods($paymentMethods, $filterContext);

$criteria->setIds($paymentMethods->getIds());

return $this->getDecorated()->load($request, $context, $criteria);
$filterContext = $this->paymentFilterContextFactory->createContextForOrder($order, $context);
} else {
$filterContext = $this->paymentFilterContextFactory->createContextForCart(
$this->cartService->getCart($context->getToken(), $context),
$context
);
}

$this->iterablePaymentFilter->filterPaymentMethods($response->getPaymentMethods(), $filterContext);

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ class GenericExpressCheckoutFilterOther implements PaymentFilterServiceInterface
/**
* if AmazonPay/PayPal Express is selected, no other payment methods should be available.
*/
public function filterPaymentMethods(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): PaymentMethodCollection
public function filterPaymentMethods(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): void
{
$actualPaymentMethod = $filterContext->getSalesChannelContext()->getPaymentMethod();

if ($methodCollection->has($actualPaymentMethod->getId())
&& \in_array($actualPaymentMethod->getHandlerIdentifier(), PaymentHandlerGroups::GENERIC_EXPRESS, true)
) {
return new PaymentMethodCollection([$actualPaymentMethod]);
$idsToRemove = array_filter($methodCollection->getIds(), static fn (string $id): bool => $id !== $actualPaymentMethod->getId());
foreach ($idsToRemove as $id) {
$methodCollection->remove($id);
}
}

return $methodCollection;
}
}
6 changes: 2 additions & 4 deletions src/Components/PaymentFilter/IterablePaymentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ public function __construct(private readonly iterable $services)
public function filterPaymentMethods(
PaymentMethodCollection $methodCollection,
PaymentFilterContext $filterContext
): PaymentMethodCollection {
): void {
foreach ($this->services as $service) {
$methodCollection = $service->filterPaymentMethods($methodCollection, $filterContext);
$service->filterPaymentMethods($methodCollection, $filterContext);
}

return $methodCollection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface PaymentFilterServiceInterface
public function filterPaymentMethods(
PaymentMethodCollection $methodCollection,
PaymentFilterContext $filterContext
): PaymentMethodCollection;
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function validate(Cart $cart, ErrorCollection $errors, SalesChannelContex

$filterContext = $this->paymentFilterContextFactory->createContextForCart($cart, $context);

$paymentMethods = $this->iterablePaymentFilter->filterPaymentMethods($paymentMethods, $filterContext);
$this->iterablePaymentFilter->filterPaymentMethods($paymentMethods, $filterContext);

if ($paymentMethods->count() === 0) {
$errors->add(
Expand Down
11 changes: 6 additions & 5 deletions src/Components/PaymentFilter/TotalPriceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ class TotalPriceFilter implements PaymentFilterServiceInterface
public function filterPaymentMethods(
PaymentMethodCollection $methodCollection,
PaymentFilterContext $filterContext
): PaymentMethodCollection {
): void {
if ($filterContext->getOrder()) {
$price = $filterContext->getOrder()->getPrice()->getTotalPrice();
} elseif ($filterContext->getCart()) {
$price = $filterContext->getCart()->getPrice()->getTotalPrice();
} else {
return $methodCollection;
return;
}

if ($price <= 0) {
return $methodCollection->filter(static fn (PaymentMethodEntity $entity) => !is_subclass_of($entity->getHandlerIdentifier(), AbstractPayonePaymentHandler::class));
$idsToRemove = $methodCollection->filter(static fn (PaymentMethodEntity $entity) => is_subclass_of($entity->getHandlerIdentifier(), AbstractPayonePaymentHandler::class))->getIds();
foreach ($idsToRemove as $id) {
$methodCollection->remove($id);
}
}

return $methodCollection;
}
}
36 changes: 18 additions & 18 deletions tests/Components/PaymentFilter/AbstractPaymentFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public function testItHidesPaymentMethodForNotAllowedCountry(string $paymentHand
$this->getAllowedCurrency()
);

$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);
$this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $methods);
static::assertInPaymentCollection(PaymentHandlerMock::class, $methods);
}

/**
Expand Down Expand Up @@ -80,10 +80,10 @@ public function testItHidesPaymentMethodForNotAllowedCurrency(string $paymentHan
$this->getDisallowedCurrency()
);

$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);
$this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $methods);
static::assertInPaymentCollection(PaymentHandlerMock::class, $methods);
}

/**
Expand Down Expand Up @@ -117,10 +117,10 @@ public function testItHidesPaymentMethodForNotAllowedValueOnCheckout(float $notA
$cart
);

$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);
$this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $methods);
static::assertInPaymentCollection(PaymentHandlerMock::class, $methods);
}

/**
Expand Down Expand Up @@ -152,10 +152,10 @@ public function testItHidesPaymentMethodForNotAllowedValueOnEditOrderPage(float
$order
);

$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);
$this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertNotInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
static::assertNotInPaymentCollection($paymentHandlerClass, $methods);
static::assertInPaymentCollection(PaymentHandlerMock::class, $methods);
}

/**
Expand Down Expand Up @@ -186,10 +186,10 @@ public function testItNotHidesPaymentMethodForAllowedConditionsOnCheckout(string
$cart
);

$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);
$this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
static::assertInPaymentCollection($paymentHandlerClass, $methods);
static::assertInPaymentCollection(PaymentHandlerMock::class, $methods);
}

/**
Expand Down Expand Up @@ -219,10 +219,10 @@ public function testItNotHidesPaymentMethodForAllowedConditionsOnEditOrderPage(s
$order
);

$result = $this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);
$this->getFilterService($paymentHandlerClass)->filterPaymentMethods($methods, $filterContext);

static::assertInPaymentCollection($paymentHandlerClass, $result);
static::assertInPaymentCollection(PaymentHandlerMock::class, $result);
static::assertInPaymentCollection($paymentHandlerClass, $methods);
static::assertInPaymentCollection(PaymentHandlerMock::class, $methods);
}

public function notAllowedValues(): \Generator
Expand Down
Loading

0 comments on commit 4560b8d

Please sign in to comment.