Skip to content

Commit

Permalink
PAYOSWXP-113: Improve payment filter to skip filter services if they …
Browse files Browse the repository at this point in the history
…not support one of the active payment methods (#291)

* Improve payment filter to skip filter services if they not support one of the active payment methods

* PAYOSWXP-113: Improve payment filter to skip filter services if they not support one of the active payment methods

---------

Co-authored-by: Moritz Müller <[email protected]>
Co-authored-by: Frederik Rommel <[email protected]>
  • Loading branch information
3 people authored Mar 1, 2024
1 parent 6b46c16 commit b283616
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
34 changes: 20 additions & 14 deletions src/Components/PaymentFilter/DefaultPaymentFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ public function __construct(
) {
}

public function filterPaymentMethods(
final public function filterPaymentMethods(
PaymentMethodCollection $methodCollection,
PaymentFilterContext $filterContext
): PaymentMethodCollection {
if ($this->getSupportedPaymentMethods($methodCollection)->getElements() === []) {
return $methodCollection;
}

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

Expand All @@ -49,32 +53,34 @@ public function filterPaymentMethods(
$this->validateMinValue($currentValue);
$this->validateMaxValue($currentValue);
}
$this->additionalChecks($methodCollection, $filterContext);
} catch (PaymentMethodNotAllowedException) {
$methodCollection = $this->removePaymentMethod($methodCollection);
$methodCollection = $this->removePaymentMethods($methodCollection);
}

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 additionalChecks(
PaymentMethodCollection $methodCollection,
PaymentFilterContext $filterContext
): void {
}

private function getSupportedPaymentMethods(PaymentMethodCollection $paymentMethodCollection): PaymentMethodCollection
{
$refClass = new \ReflectionClass($this->paymentHandlerClass);

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

protected function removePaymentMethod(PaymentMethodCollection $paymentMethodCollection): PaymentMethodCollection
private function removePaymentMethods(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));
$itemsToRemove = $this->getSupportedPaymentMethods($paymentMethodCollection);

return $paymentMethodCollection->filter(static fn (PaymentMethodEntity $entity) => !$itemsToRemove->has($entity->getUniqueIdentifier()));
}

private function validateAddress(CustomerAddressEntity|OrderAddressEntity|null $address): void
Expand Down
9 changes: 3 additions & 6 deletions src/Components/PaymentFilter/KlarnaPaymentMethodFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@

namespace PayonePayment\Components\PaymentFilter;

use PayonePayment\Components\PaymentFilter\Exception\PaymentMethodNotAllowedException;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector;

class KlarnaPaymentMethodFilter extends DefaultPaymentFilterService
{
public function filterPaymentMethods(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): PaymentMethodCollection
protected function additionalChecks(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): void
{
$methodCollection = parent::filterPaymentMethods($methodCollection, $filterContext);

if ($this->hasCustomProducts($filterContext)) {
$methodCollection = $this->removePaymentMethod($methodCollection);
throw new PaymentMethodNotAllowedException('PAYONE does not support custom products within Klarna payments');
}

return $methodCollection;
}

private function hasCustomProducts(PaymentFilterContext $filterContext): bool
Expand Down
12 changes: 4 additions & 8 deletions src/Components/PaymentFilter/PayoneBNPLPaymentMethodFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,27 @@

namespace PayonePayment\Components\PaymentFilter;

use PayonePayment\Components\PaymentFilter\Exception\PaymentMethodNotAllowedException;
use PayonePayment\Core\Utils\AddressCompare;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;

class PayoneBNPLPaymentMethodFilter extends DefaultPaymentFilterService
{
public function filterPaymentMethods(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): PaymentMethodCollection
protected function additionalChecks(PaymentMethodCollection $methodCollection, PaymentFilterContext $filterContext): void
{
$methodCollection = parent::filterPaymentMethods($methodCollection, $filterContext);

$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);
throw new PaymentMethodNotAllowedException('Different billing and shipping addresses are not allowed for secured invoice');
} elseif ($billingAddress instanceof CustomerAddressEntity
&& $shippingAddress instanceof CustomerAddressEntity
&& !AddressCompare::areCustomerAddressesIdentical($billingAddress, $shippingAddress)) {
$methodCollection = $this->removePaymentMethod($methodCollection);
throw new PaymentMethodNotAllowedException('Different billing and shipping addresses are not allowed for secured invoice');
}

return $methodCollection;
}
}

0 comments on commit b283616

Please sign in to comment.