Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading