Skip to content

Commit ae14bd4

Browse files
committed
[ECP-9641-v9] Implement a separate config provider for virtual quotes
1 parent 4827010 commit ae14bd4

File tree

8 files changed

+144
-17
lines changed

8 files changed

+144
-17
lines changed

Block/Form/PosCloud.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,11 @@ public function __construct(
8888
}
8989

9090
/**
91-
* @return array|mixed
92-
* @throws \Adyen\AdyenException
93-
* @throws \Magento\Framework\Exception\LocalizedException
94-
* @throws \Magento\Framework\Exception\NoSuchEntityException
91+
* @return array
9592
*/
96-
public function getConnectedTerminals()
93+
public function getConnectedTerminals(): array
9794
{
98-
$connectedTerminals = $this->connectedTerminalsHelper->getConnectedTerminals();
95+
$connectedTerminals = $this->connectedTerminalsHelper->getConnectedTerminalsApiResponse();
9996

10097
if (!empty($connectedTerminals['uniqueTerminalIds'])) {
10198
return $connectedTerminals['uniqueTerminalIds'];

Helper/ConnectedTerminals.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313

1414
use Adyen\AdyenException;
1515
use Adyen\Payment\Logger\AdyenLogger;
16+
use Exception;
1617
use Magento\Checkout\Model\Session;
1718

1819
class ConnectedTerminals
1920
{
21+
/**
22+
* In-memory cache for the /connected-terminals response
23+
*
24+
* @var array|null
25+
*/
26+
protected ?array $connectedTerminalsApiResponse = null;
27+
2028
protected Session $session;
2129
protected Data $adyenHelper;
2230
private AdyenLogger $adyenLogger;
@@ -66,4 +74,37 @@ public function getConnectedTerminals(int $storeId = null): array
6674

6775
return $responseData;
6876
}
77+
78+
/**
79+
* This method sets the /connected-terminals response in the in-memory cache.
80+
*
81+
* @param array $response
82+
* @return void
83+
*/
84+
protected function setConnectedTerminalsApiResponse(array $response): void
85+
{
86+
$this->connectedTerminalsApiResponse = $response;
87+
}
88+
89+
/**
90+
* This method returns the /connected-terminals response from the in-memory cache if it exists or calls the
91+
* getConnectedTerminals method to get the response and set it in the in-memory cache.
92+
*
93+
* @param int|null $storeId
94+
* @return array|null
95+
*/
96+
public function getConnectedTerminalsApiResponse(?int $storeId = null): ?array
97+
{
98+
try {
99+
if (!isset($this->connectedTerminalsApiResponse)) {
100+
$this->setConnectedTerminalsApiResponse($this->getConnectedTerminals($storeId));
101+
}
102+
} catch (Exception $e) {
103+
$this->adyenLogger->error(
104+
sprintf("An error occurred while trying to get connected terminals: %s", $e->getMessage())
105+
);
106+
}
107+
108+
return $this->connectedTerminalsApiResponse;
109+
}
69110
}

Helper/MagentoPaymentDetails.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function addAdyenExtensionAttributes(PaymentDetailsInterface $result, int
6161
$extensionAttributes->setAdyenPaymentMethodsResponse($this->adyenPaymentMethodsHelper->getApiResponse($quote));
6262

6363
if ($isAdyenPosCloudEnabled) {
64-
$connectedTerminals = $this->connectedTerminalsHelper->getConnectedTerminals($storeId);
64+
$connectedTerminals = $this->connectedTerminalsHelper->getConnectedTerminalsApiResponse($storeId);
6565

6666
if (!empty($connectedTerminals['uniqueTerminalIds'])) {
6767
$extensionAttributes->setAdyenConnectedTerminals($connectedTerminals['uniqueTerminalIds']);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
*
4+
* Adyen Payment module (https://www.adyen.com/)
5+
*
6+
* Copyright (c) 2026 Adyen N.V. (https://www.adyen.com/)
7+
* See LICENSE.txt for license details.
8+
*
9+
* Author: Adyen <magento@adyen.com>
10+
*/
11+
12+
namespace Adyen\Payment\Model\Ui;
13+
14+
use Adyen\AdyenException;
15+
use Adyen\Payment\Helper\Config;
16+
use Adyen\Payment\Helper\ConnectedTerminals;
17+
use Adyen\Payment\Helper\PaymentMethods;
18+
use Magento\Checkout\Model\ConfigProviderInterface;
19+
use Magento\Checkout\Model\Session;
20+
use Magento\Framework\Exception\LocalizedException;
21+
use Magento\Framework\Exception\NoSuchEntityException;
22+
use Magento\Store\Model\StoreManagerInterface;
23+
24+
/**
25+
* This config provider is initially used to provide payment methods and connected terminals to the checkout
26+
* as the required `shipping-information` or `payment-information` API calls are not triggered for virtual quotes.
27+
*/
28+
class AdyenVirtualQuoteConfigProvider implements ConfigProviderInterface
29+
{
30+
/**
31+
* @param PaymentMethods $paymentMethodsHelper
32+
* @param StoreManagerInterface $storeManager
33+
* @param Config $configHelper
34+
* @param Session $session
35+
* @param ConnectedTerminals $connectedTerminalsHelper
36+
*/
37+
public function __construct(
38+
private readonly PaymentMethods $paymentMethodsHelper,
39+
private readonly StoreManagerInterface $storeManager,
40+
private readonly Config $configHelper,
41+
private readonly Session $session,
42+
private readonly ConnectedTerminals $connectedTerminalsHelper
43+
) {}
44+
45+
/**
46+
* @return array
47+
* @throws AdyenException
48+
* @throws LocalizedException
49+
* @throws NoSuchEntityException
50+
*/
51+
public function getConfig(): array
52+
{
53+
$storeId = $this->storeManager->getStore()->getId();
54+
$config = [];
55+
56+
if ($this->session->getQuote()->isVirtual()) {
57+
if ($this->configHelper->getIsPaymentMethodsActive($storeId)) {
58+
$config['payment']['adyen']['virtualQuote']['paymentMethodsResponse'] =
59+
$this->paymentMethodsHelper->getApiResponse($this->session->getQuote());
60+
}
61+
62+
if ($this->configHelper->getAdyenPosCloudConfigData("active", $storeId, true)) {
63+
$config['payment']['adyen']['virtualQuote']['connectedTerminals'] =
64+
$this->connectedTerminalsHelper->getConnectedTerminalsApiResponse($storeId);
65+
}
66+
}
67+
68+
return $config;
69+
}
70+
}

Test/Unit/Helper/MagentoPaymentDetailsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function testAddAdyenExtensionAttributes(): void
215215
]);
216216

217217
$connectedTerminalsMock = $this->createConfiguredMock(ConnectedTerminals::class, [
218-
'getConnectedTerminals' => self::CONNECTED_TERMINALS
218+
'getConnectedTerminalsApiResponse' => self::CONNECTED_TERMINALS
219219
]);
220220

221221
$configHelperMock = $this->createConfiguredMock(Config::class, [
@@ -271,7 +271,7 @@ public function testAddAdyenExtensionAttributesReturnsEarlyWhenBothDisabled(): v
271271

272272
$connectedTerminalsMock = $this->createMock(ConnectedTerminals::class);
273273
$connectedTerminalsMock->expects($this->never())
274-
->method('getConnectedTerminals');
274+
->method('getConnectedTerminalsApiResponse');
275275

276276
$paymentMethodsHelperMock = $this->createMock(PaymentMethods::class);
277277
$paymentMethodsHelperMock->expects($this->never())

etc/frontend/di.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<item name="adyen_pm_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenPmConfigProvider</item>
2020
<item name="adyen_pos_cloud_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider</item>
2121
<item name="adyen_giftcard_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenGiftcardConfigProvider</item>
22+
<item name="adyen_virtual_quote_config_provider" xsi:type="object">Adyen\Payment\Model\Ui\AdyenVirtualQuoteConfigProvider</item>
2223
</argument>
2324
</arguments>
2425
</type>
@@ -181,4 +182,4 @@
181182
</argument>
182183
</arguments>
183184
</type>
184-
</config>
185+
</config>

view/frontend/web/js/model/adyen-configuration.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ define(
4747
},
4848
getCspNonce: function () {
4949
return window.checkoutConfig.payment.adyen.cspNonce;
50+
},
51+
getVirtualQuotePaymentMethodsResponse: function () {
52+
return window.checkoutConfig.payment.adyen.virtualQuote.paymentMethodsResponse;
53+
},
54+
getVirtualQuoteConnectedTerminals: function () {
55+
return window.checkoutConfig.payment.adyen.virtualQuote.connectedTerminals;
5056
}
5157
};
5258
},

view/frontend/web/js/view/payment/adyen-methods.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ define(
1313
[
1414
'uiComponent',
1515
'Magento_Checkout/js/model/payment/renderer-list',
16-
'Magento_Checkout/js/action/get-payment-information',
17-
'Magento_Checkout/js/model/quote'
16+
'Magento_Checkout/js/model/quote',
17+
'Adyen_Payment/js/model/adyen-configuration',
18+
'Adyen_Payment/js/model/adyen-payment-service'
1819
],
1920
function (
2021
Component,
2122
rendererList,
22-
getPaymentInformation,
23-
quote
23+
quote,
24+
adyenConfiguration,
25+
adyenPaymentService
2426
) {
2527
'use strict';
2628

@@ -47,10 +49,20 @@ define(
4749
initialize: function () {
4850
this._super();
4951

50-
// Virtual quote doesn't call payment-information or shipping-information endpoints.
51-
// payment-information endpoint should be called manually to fetch Adyen extension attributes.
52+
/*
53+
* Virtual quote doesn't call payment-information or shipping-information endpoints. Hence,
54+
* the config provider is used to provide payment methods and connected terminals to the checkout.
55+
*/
5256
if (quote.isVirtual()) {
53-
getPaymentInformation();
57+
const paymentMethodsResponse = adyenConfiguration.getVirtualQuotePaymentMethodsResponse();
58+
if (paymentMethodsResponse) {
59+
adyenPaymentService.setPaymentMethods(JSON.parse(paymentMethodsResponse));
60+
}
61+
62+
const connectedTerminals = adyenConfiguration.getVirtualQuoteConnectedTerminals();
63+
if (connectedTerminals) {
64+
adyenPaymentService.setConnectedTerminals(connectedTerminals);
65+
}
5466
}
5567
}
5668
});

0 commit comments

Comments
 (0)