Skip to content
Merged
24 changes: 16 additions & 8 deletions Helper/MagentoPaymentDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

namespace Adyen\Payment\Helper;

use Adyen\AdyenException;
use Magento\Checkout\Api\Data\PaymentDetailsInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;

class MagentoPaymentDetails
Expand All @@ -20,19 +23,30 @@ class MagentoPaymentDetails
protected Config $configHelper;
protected CartRepositoryInterface $cartRepository;
protected ConnectedTerminals $connectedTerminalsHelper;
protected PaymentMethods $adyenPaymentMethodsHelper;

public function __construct(
PaymentMethodsFilter $paymentMethodsFilter,
Config $configHelper,
CartRepositoryInterface $cartRepository,
ConnectedTerminals $connectedTerminals
ConnectedTerminals $connectedTerminals,
PaymentMethods $adyenPaymentMethodsHelper
) {
$this->paymentMethodsFilter = $paymentMethodsFilter;
$this->configHelper = $configHelper;
$this->cartRepository = $cartRepository;
$this->connectedTerminalsHelper = $connectedTerminals;
$this->adyenPaymentMethodsHelper = $adyenPaymentMethodsHelper;
}

/**
* @param PaymentDetailsInterface $result
* @param int $cartId
* @return PaymentDetailsInterface
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function addAdyenExtensionAttributes(PaymentDetailsInterface $result, int $cartId): PaymentDetailsInterface
{
$quote = $this->cartRepository->get($cartId);
Expand All @@ -42,15 +56,9 @@ public function addAdyenExtensionAttributes(PaymentDetailsInterface $result, int
if (!$this->configHelper->getIsPaymentMethodsActive($storeId) && !$isAdyenPosCloudEnabled) {
return $result;
}
$magentoPaymentMethods = $result->getPaymentMethods();

list($magentoPaymentMethods, $adyenPaymentMethodsResponse) =
$this->paymentMethodsFilter->sortAndFilterPaymentMethods($magentoPaymentMethods, $quote);

$result->setPaymentMethods($magentoPaymentMethods);
$extensionAttributes = $result->getExtensionAttributes();

$extensionAttributes->setAdyenPaymentMethodsResponse($adyenPaymentMethodsResponse);
$extensionAttributes->setAdyenPaymentMethodsResponse($this->adyenPaymentMethodsHelper->getApiResponse($quote));

if ($isAdyenPosCloudEnabled) {
$connectedTerminals = $this->connectedTerminalsHelper->getConnectedTerminals($storeId);
Expand Down
45 changes: 45 additions & 0 deletions Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class PaymentMethods extends AbstractHelper
AdyenMotoConfigProvider::CODE
];

/**
* In-memory cache for the /paymentMethods response
*
* @var string|null
*/
protected ?string $paymentMethodsApiResponse = null;

/**
* @var CartRepositoryInterface
*/
Expand Down Expand Up @@ -535,6 +542,44 @@ protected function setQuote(CartInterface $quote): void
$this->quote = $quote;
}

/**
* This method sets the /paymentMethods response in the in-memory cache.
*
* @param string $response
* @return void
*/
protected function setApiResponse(string $response): void
{
$this->paymentMethodsApiResponse = $response;
}

/**
* This method checks the in-memory cache for the /paymentMethods response.
* If the response is not in the cache, it will fetch it from the Adyen Checkout API.
*
* @param CartInterface $quote
* @return string|null
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getApiResponse(CartInterface $quote): ?string
{
if (!isset($this->paymentMethodsApiResponse)) {
$channel = $this->request->getParam('channel');
$adyenPaymentMethodsResponse = $this->getPaymentMethods(
$quote->getId(),
$quote->getBillingAddress()->getCountryId(),
null,
$channel
);

$this->setApiResponse($adyenPaymentMethodsResponse);
}

return $this->paymentMethodsApiResponse;
}

/**
* @return string|null
*/
Expand Down
27 changes: 13 additions & 14 deletions Helper/PaymentMethodsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

namespace Adyen\Payment\Helper;

use Adyen\AdyenException;
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Framework\App\RequestInterface;

class PaymentMethodsFilter
{
Expand All @@ -32,26 +33,24 @@ class PaymentMethodsFilter
];

private PaymentMethods $paymentMethods;
private RequestInterface $request;

public function __construct(
PaymentMethods $paymentMethods,
RequestInterface $request
PaymentMethods $paymentMethods
) {
$this->paymentMethods = $paymentMethods;
$this->request = $request;
}

/**
* @param array $magentoPaymentMethods
* @param CartInterface $quote
* @return array
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function sortAndFilterPaymentMethods(array $magentoPaymentMethods, CartInterface $quote): array
{
$channel = $this->request->getParam('channel');
$adyenPaymentMethodsResponse = $this->paymentMethods->getPaymentMethods(
$quote->getId(),
$quote->getBillingAddress()->getCountryId(),
null,
$channel
);

$adyenPaymentMethodsResponse = $this->paymentMethods->getApiResponse($quote);
$adyenPaymentMethodsDecoded = json_decode($adyenPaymentMethodsResponse, true);

if (!empty($adyenPaymentMethodsDecoded)) {
Expand Down
73 changes: 73 additions & 0 deletions Model/Ui/AdyenVirtualQuoteConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2026 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Model\Ui;

use Adyen\AdyenException;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\ConnectedTerminals;
use Adyen\Payment\Helper\PaymentMethods;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

/**
* This config provider is initially used to provide payment methods and connected terminals to the checkout
* as the required `shipping-information` or `payment-information` API calls are not triggered for virtual quotes.
*/
class AdyenVirtualQuoteConfigProvider implements ConfigProviderInterface
{
/**
* @param PaymentMethods $paymentMethodsHelper
* @param StoreManagerInterface $storeManager
* @param Config $configHelper
* @param Session $session
* @param ConnectedTerminals $connectedTerminalsHelper
*/
public function __construct(
private readonly PaymentMethods $paymentMethodsHelper,
private readonly StoreManagerInterface $storeManager,
private readonly Config $configHelper,
private readonly Session $session,
private readonly ConnectedTerminals $connectedTerminalsHelper
) {}

/**
* @return array
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getConfig(): array
{
$storeId = $this->storeManager->getStore()->getId();
$config = [];

if ($this->session->getQuote()->isVirtual()) {
if ($this->configHelper->getIsPaymentMethodsActive($storeId)) {
$config['payment']['adyen']['virtualQuote']['paymentMethodsResponse'] =
$this->paymentMethodsHelper->getApiResponse($this->session->getQuote());
}

if ($this->configHelper->getAdyenPosCloudConfigData("active", $storeId, true)) {
$connectedTerminals = $this->connectedTerminalsHelper->getConnectedTerminals($storeId);
if (!empty($connectedTerminals['uniqueTerminalIds'])) {
$config['payment']['adyen']['virtualQuote']['connectedTerminals'] =
$connectedTerminals['uniqueTerminalIds'];
}
}
}

return $config;
}
}
11 changes: 11 additions & 0 deletions Plugin/MultishippingPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace Adyen\Payment\Plugin;

use Adyen\AdyenException;
use Adyen\Payment\Block\Checkout\Multishipping\Billing;
use Adyen\Payment\Helper\Config;
use Adyen\Payment\Helper\PaymentMethodsFilter;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class MultishippingPaymentMethods
{
Expand All @@ -28,6 +31,14 @@ public function __construct(
$this->configHelper = $configHelper;
}

/**
* @param Billing $billing
* @param array $result
* @return array
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function afterGetMethods(
Billing $billing,
array $result
Expand Down
59 changes: 59 additions & 0 deletions Plugin/SortAndFilterAdyenPaymentMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/

namespace Adyen\Payment\Plugin;

use Adyen\AdyenException;
use Adyen\Payment\Helper\PaymentMethodsFilter;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\PaymentMethodManagementInterface;

class SortAndFilterAdyenPaymentMethods
{
protected PaymentMethodsFilter $paymentMethodsFilter;
protected CartRepositoryInterface $quoteRepository;

/**
* @param PaymentMethodsFilter $paymentMethodsFilter
* @param CartRepositoryInterface $quoteRepository
*/
public function __construct(
PaymentMethodsFilter $paymentMethodsFilter,
CartRepositoryInterface $quoteRepository
) {
$this->paymentMethodsFilter = $paymentMethodsFilter;
$this->quoteRepository = $quoteRepository;
}

/**
* @param PaymentMethodManagementInterface $paymentMethodManagement
* @param array $paymentMethodManagementResult
* @param int $cartId
* @return array
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function afterGetList(
PaymentMethodManagementInterface $paymentMethodManagement,
array $paymentMethodManagementResult,
int $cartId
): array {
list($filteredPaymentMethods) = $this->paymentMethodsFilter->sortAndFilterPaymentMethods(
$paymentMethodManagementResult,
$this->quoteRepository->get($cartId)
);

return $filteredPaymentMethods;
}
}
Loading
Loading