Skip to content

Commit

Permalink
Add payment methods filtering
Browse files Browse the repository at this point in the history
Filter based on enabled express checkout settings and availability rules

ISSUE: AD4CR22I-14
  • Loading branch information
filipkojic committed Dec 30, 2024
1 parent 31ebd1f commit d42d655
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<argument type="service" id="country.repository"/>
<argument type="service" id="shipping_method.repository"/>
<argument type="service" id="Adyen\Shopware\Service\PaymentMethodsService"/>
<argument type="service" id="Adyen\Shopware\Service\PaymentMethodsFilterService"/>
<argument type="service" id="Adyen\Shopware\Util\Currency"/>
</service>
<service id="Adyen\Shopware\ScheduledTask\Webhook\WebhookHandlerFactory">
Expand Down
10 changes: 9 additions & 1 deletion src/Service/ExpressCheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class ExpressCheckoutService
*/
private $paymentMethodsService;

/**
* @var PaymentMethodsFilterService
*/
private $paymentMethodsFilterService;

/**
* @var Currency
*/
Expand All @@ -50,12 +55,14 @@ public function __construct(
EntityRepository $countryRepository,
EntityRepository $shippingMethodRepository,
PaymentMethodsService $paymentMethodsService,
PaymentMethodsFilterService $paymentMethodsFilterService,
Currency $currencyUtil
) {
$this->cartService = $cartService;
$this->countryRepository = $countryRepository;
$this->shippingMethodRepository = $shippingMethodRepository;
$this->paymentMethodsService = $paymentMethodsService;
$this->paymentMethodsFilterService = $paymentMethodsFilterService;
$this->currencyUtil = $currencyUtil;
}

Expand Down Expand Up @@ -209,13 +216,14 @@ public function createCart(
$cart = $this->cartService->recalculate($cart, $updatedSalesChannelContext);

$paymentMethods = $this->paymentMethodsService->getPaymentMethods($updatedSalesChannelContext);
$filteredPaymentMethods = $this->paymentMethodsFilterService->filterAndValidatePaymentMethods($paymentMethods, $cart, $salesChannelContext);

return [
'cart' => $cart,
'shippingMethods' => $shippingMethods,
'shippingMethod' => $shippingMethod,
'shippingLocation' => $shippingLocation,
'paymentMethods' => $paymentMethods,
'paymentMethods' => $filteredPaymentMethods,
];
}

Expand Down
44 changes: 44 additions & 0 deletions src/Service/PaymentMethodsFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use Adyen\Shopware\Handlers\ApplePayPaymentMethodHandler;
use Adyen\Shopware\PaymentMethods\RatepayDirectdebitPaymentMethod;
use Adyen\Shopware\PaymentMethods\RatepayPaymentMethod;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\Rule\CartRuleScope;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
Expand Down Expand Up @@ -305,4 +307,46 @@ public function getGiftCardPaymentMethodId(SalesChannelContext $context): ?strin
// Return the payment method ID or null if not found
return $paymentMethod ? $paymentMethod->getId() : null;
}

public function filterAndValidatePaymentMethods(
PaymentMethodsResponse $paymentMethodsResponse,
Cart $cart,
SalesChannelContext $salesChannelContext
): PaymentMethodsResponse {
// Extract original payment methods
$paymentMethods = $paymentMethodsResponse->getPaymentMethods();

// Supported payment methods and their corresponding configuration checks
$allowedMethods = [
'paywithgoogle' => [$this->configurationService, 'isGooglePayExpressCheckoutEnabled'],
'paypal' => [$this->configurationService, 'isPayPalExpressCheckoutEnabled'],
'applepay' => [$this->configurationService, 'isApplePayExpressCheckoutEnabled']
];

// Filter methods by type and configuration
$filteredMethods = array_filter($paymentMethods, function ($method) use ($allowedMethods, $salesChannelContext) {
$type = $method['type'];
if (!isset($allowedMethods[$type])) {
return false;
}

$configCheck = $allowedMethods[$type];
return $configCheck($salesChannelContext->getSalesChannelId());
});

// Check rules for every method
$validMethods = [];
foreach ($filteredMethods as $method) {
$availabilityRule = $method['availabilityRule'] ?? null;

if (!$availabilityRule || $availabilityRule->getPayload()->match(new CartRuleScope($cart, $salesChannelContext))) {
$validMethods[] = $method;
}
}

$paymentMethodsResponse->setPaymentMethods($validMethods);

return $paymentMethodsResponse;
}

}

0 comments on commit d42d655

Please sign in to comment.