Skip to content
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
45 changes: 23 additions & 22 deletions Helper/MagentoPaymentDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@

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
{
protected PaymentMethodsFilter $paymentMethodsFilter;
protected Config $configHelper;
protected CartRepositoryInterface $cartRepository;
protected ConnectedTerminals $connectedTerminalsHelper;

/**
* @param Config $configHelper
* @param CartRepositoryInterface $cartRepository
* @param ConnectedTerminals $connectedTerminalsHelper
* @param PaymentMethods $adyenPaymentMethodsHelper
*/
public function __construct(
PaymentMethodsFilter $paymentMethodsFilter,
Config $configHelper,
CartRepositoryInterface $cartRepository,
ConnectedTerminals $connectedTerminals
) {
$this->paymentMethodsFilter = $paymentMethodsFilter;
$this->configHelper = $configHelper;
$this->cartRepository = $cartRepository;
$this->connectedTerminalsHelper = $connectedTerminals;
}
protected readonly Config $configHelper,
protected readonly CartRepositoryInterface $cartRepository,
protected readonly ConnectedTerminals $connectedTerminalsHelper,
protected readonly PaymentMethods $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 +49,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
69 changes: 58 additions & 11 deletions Helper/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\SerializerInterface;
Expand Down Expand Up @@ -85,6 +86,12 @@ class PaymentMethods extends AbstractHelper
self::ADYEN_BOLETO
];

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

/**
Expand All @@ -106,6 +113,7 @@ class PaymentMethods extends AbstractHelper
* @param Locale $localeHelper
* @param ShopperConversionId $generateShopperConversionId
* @param CheckoutSession $checkoutSession
* @param RequestInterface $request
*/
public function __construct(
Context $context,
Expand All @@ -114,18 +122,19 @@ public function __construct(
protected readonly Data $adyenHelper,
protected readonly AdyenLogger $adyenLogger,
protected readonly Repository $assetRepo,
protected readonly Source $assetSource,
protected readonly DesignInterface $design,
protected readonly ThemeProviderInterface $themeProvider,
protected readonly ChargedCurrency $chargedCurrency,
protected readonly Config $configHelper,
protected readonly MagentoDataHelper $dataHelper,
protected readonly SerializerInterface $serializer,
protected readonly Source $assetSource,
protected readonly DesignInterface $design,
protected readonly ThemeProviderInterface $themeProvider,
protected readonly ChargedCurrency $chargedCurrency,
protected readonly Config $configHelper,
protected readonly MagentoDataHelper $dataHelper,
protected readonly SerializerInterface $serializer,
protected readonly PaymentTokenRepositoryInterface $paymentTokenRepository,
protected readonly SearchCriteriaBuilder $searchCriteriaBuilder,
protected readonly Locale $localeHelper,
protected readonly ShopperConversionId $generateShopperConversionId,
protected readonly CheckoutSession $checkoutSession
protected readonly SearchCriteriaBuilder $searchCriteriaBuilder,
protected readonly Locale $localeHelper,
protected readonly ShopperConversionId $generateShopperConversionId,
protected readonly CheckoutSession $checkoutSession,
protected readonly RequestInterface $request
) {
parent::__construct($context);
}
Expand Down Expand Up @@ -434,6 +443,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
39 changes: 19 additions & 20 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 @@ -31,27 +32,24 @@ class PaymentMethodsFilter
AdyenPayByLinkConfigProvider::CODE
];

private PaymentMethods $paymentMethods;
private RequestInterface $request;

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

private readonly PaymentMethods $paymentMethods,
) {}

/**
* @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 All @@ -61,6 +59,7 @@ public function sortAndFilterPaymentMethods(array $magentoPaymentMethods, CartIn
$magentoPaymentMethods = $this->sortPaymentMethodsList($magentoPaymentMethods, $adyenPaymentMethods);
}

// TODO: Remove $adyenPaymentMethodsResponse from the response as it's not being used anymore.
return [$magentoPaymentMethods, $adyenPaymentMethodsResponse];
}

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;
}
}
38 changes: 26 additions & 12 deletions Plugin/MultishippingPaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@

namespace Adyen\Payment\Plugin;

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

class MultishippingPaymentMethods
{
protected PaymentMethodsFilter $paymentMethodsFilter;
protected Config $configHelper;

/**
* @param PaymentMethodsFilter $paymentMethodsFilter
* @param Config $configHelper
* @param PaymentMethods $paymentMethods
*/
public function __construct(
PaymentMethodsFilter $paymentMethodsFilter,
Config $configHelper
) {
$this->paymentMethodsFilter = $paymentMethodsFilter;
$this->configHelper = $configHelper;
}

protected readonly PaymentMethodsFilter $paymentMethodsFilter,
protected readonly Config $configHelper,
protected readonly PaymentMethods $paymentMethods
) {}

/**
* @param Billing $billing
* @param array $result
* @return array
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function afterGetMethods(
Billing $billing,
array $result
Expand All @@ -39,8 +51,10 @@ public function afterGetMethods(
return $result;
}

list ($result, $adyenMethods) = $this->paymentMethodsFilter->sortAndFilterPaymentMethods($result, $quote);
$billing->setAdyenPaymentMethodsResponse($adyenMethods);
list ($result) = $this->paymentMethodsFilter->sortAndFilterPaymentMethods($result, $quote);
$billing->setAdyenPaymentMethodsResponse(
$this->paymentMethods->getApiResponse($quote)
);

return $result;
}
Expand Down
Loading
Loading