Skip to content

Commit a75033b

Browse files
MC-39109: Vault throw The requested Payment Method is not available. error
1 parent f55f411 commit a75033b

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Vault\Plugin;
9+
10+
use Magento\Checkout\Api\PaymentInformationManagementInterface;
11+
use Magento\Quote\Api\Data\AddressInterface;
12+
use Magento\Quote\Api\Data\PaymentInterface;
13+
use Magento\Vault\Model\PaymentMethodList;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
16+
/**
17+
* Payment vault information management process
18+
*/
19+
class PaymentVaultInformationManagement
20+
{
21+
/**
22+
* @var PaymentMethodList
23+
*/
24+
private $vaultPaymentMethodList;
25+
26+
/**
27+
* @var StoreManagerInterface
28+
*/
29+
private $storeManager;
30+
31+
/**
32+
* PaymentVaultInformationManagement constructor.
33+
*
34+
* @param PaymentMethodList $vaultPaymentMethodList
35+
* @param StoreManagerInterface $storeManager
36+
*/
37+
public function __construct(
38+
PaymentMethodList $vaultPaymentMethodList,
39+
StoreManagerInterface $storeManager
40+
) {
41+
$this->vaultPaymentMethodList = $vaultPaymentMethodList;
42+
$this->storeManager = $storeManager;
43+
}
44+
45+
/**
46+
* Set available vault method code without index to payment
47+
*
48+
* @param PaymentInformationManagementInterface $subject
49+
* @param string $cartId
50+
* @param PaymentInterface $paymentMethod
51+
* @param AddressInterface|null $billingAddress
52+
* @return void
53+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
54+
*/
55+
public function beforeSavePaymentInformation(
56+
PaymentInformationManagementInterface $subject,
57+
string $cartId,
58+
PaymentInterface $paymentMethod,
59+
AddressInterface $billingAddress = null
60+
): void {
61+
$availableMethods = $this->vaultPaymentMethodList->getActiveList($this->storeManager->getStore()->getId());
62+
foreach ($availableMethods as $availableMethod) {
63+
if (strpos($paymentMethod->getMethod(), $availableMethod->getCode()) !== false) {
64+
$paymentMethod->setMethod($availableMethod->getCode());
65+
}
66+
}
67+
}
68+
}

app/code/Magento/Vault/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@
5353
</argument>
5454
</arguments>
5555
</type>
56+
<type name="Magento\Checkout\Api\PaymentInformationManagementInterface">
57+
<plugin name="ProcessPaymentVaultInformationManagement"
58+
type="Magento\Vault\Plugin\PaymentVaultInformationManagement"/>
59+
</type>
5660
</config>

0 commit comments

Comments
 (0)