Skip to content

Commit

Permalink
Improve payment filter to skip filter services if they not support on…
Browse files Browse the repository at this point in the history
…e of the active payment methods
  • Loading branch information
Moritz Müller committed Jan 30, 2024
1 parent cd18bee commit 480f9fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
34 changes: 19 additions & 15 deletions src/Components/PaymentFilter/DefaultPaymentFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function filterPaymentMethods(
PaymentMethodCollection $methodCollection,
PaymentFilterContext $filterContext
): PaymentMethodCollection {
$supportedPaymentMethods = $this->getSupportedPaymentMethods($methodCollection);
if ($supportedPaymentMethods->count() === 0) {
return $methodCollection;
}

$currency = $filterContext->getCurrency();
$billingAddress = $filterContext->getBillingAddress();

Expand All @@ -50,31 +55,30 @@ public function filterPaymentMethods(
$this->validateMaxValue($currentValue);
}
} catch (PaymentMethodNotAllowedException) {
$methodCollection = $this->removePaymentMethod($methodCollection);
$methodCollection = $this->removePaymentMethods($methodCollection, $supportedPaymentMethods);
}

return $methodCollection;
}

/**
* returns true, if the method should be filtered out.
*
* @internal method needs to be public, so it can be called by `removePaymentMethod`
*/
public function canMethodRemoved(PaymentMethodEntity $paymentMethod): bool
protected function getSupportedPaymentMethods(PaymentMethodCollection $paymentMethodCollection): PaymentMethodCollection
{
$refClass = new \ReflectionClass($this->paymentHandlerClass);

return $refClass->isAbstract()
? is_subclass_of($paymentMethod->getHandlerIdentifier(), $this->paymentHandlerClass)
: $paymentMethod->getHandlerIdentifier() === $this->paymentHandlerClass;
return $paymentMethodCollection->filter(function (PaymentMethodEntity $paymentMethod) use ($refClass) {
return $refClass->isAbstract()
? is_subclass_of($paymentMethod->getHandlerIdentifier(), $this->paymentHandlerClass)
: $paymentMethod->getHandlerIdentifier() === $this->paymentHandlerClass;
});
}

protected function removePaymentMethod(PaymentMethodCollection $paymentMethodCollection): PaymentMethodCollection
{
$that = $this;
// filter-method needs a closure (forced anonymous function) so we can not use [$this, 'filterMethod']
return $paymentMethodCollection->filter(static fn (PaymentMethodEntity $entity) => !$that->canMethodRemoved($entity));
protected function removePaymentMethods(
PaymentMethodCollection $paymentMethodCollection,
PaymentMethodCollection $toBeRemoved
): PaymentMethodCollection {
return $paymentMethodCollection->filter(static function (PaymentMethodEntity $entity) use ($toBeRemoved) {
return !$toBeRemoved->has($entity->getUniqueIdentifier());
});
}

private function validateAddress(CustomerAddressEntity|OrderAddressEntity|null $address): void
Expand Down
7 changes: 6 additions & 1 deletion src/Components/PaymentFilter/KlarnaPaymentMethodFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public function filterPaymentMethods(PaymentMethodCollection $methodCollection,
{
$methodCollection = parent::filterPaymentMethods($methodCollection, $filterContext);

$supportedPaymentMethods = $this->getSupportedPaymentMethods($methodCollection);
if ($supportedPaymentMethods->count() === 0) {
return $methodCollection;
}

if ($this->hasCustomProducts($filterContext)) {
$methodCollection = $this->removePaymentMethod($methodCollection);
$methodCollection = $this->removePaymentMethods($methodCollection, $supportedPaymentMethods);
}

return $methodCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ public function filterPaymentMethods(PaymentMethodCollection $methodCollection,
{
$methodCollection = parent::filterPaymentMethods($methodCollection, $filterContext);

$supportedPaymentMethods = $this->getSupportedPaymentMethods($methodCollection);
if ($supportedPaymentMethods->count() === 0) {
return $methodCollection;
}

$billingAddress = $filterContext->getBillingAddress();
$shippingAddress = $filterContext->getShippingAddress();

// Different billing and shipping addresses are not allowed for secured invoice
if ($billingAddress instanceof OrderAddressEntity
&& $shippingAddress instanceof OrderAddressEntity
&& !AddressCompare::areOrderAddressesIdentical($billingAddress, $shippingAddress)) {
$methodCollection = $this->removePaymentMethod($methodCollection);
$methodCollection = $this->removePaymentMethods($methodCollection, $supportedPaymentMethods);
} elseif ($billingAddress instanceof CustomerAddressEntity
&& $shippingAddress instanceof CustomerAddressEntity
&& !AddressCompare::areCustomerAddressesIdentical($billingAddress, $shippingAddress)) {
$methodCollection = $this->removePaymentMethod($methodCollection);
$methodCollection = $this->removePaymentMethods($methodCollection, $supportedPaymentMethods);
}

return $methodCollection;
Expand Down

0 comments on commit 480f9fc

Please sign in to comment.