Skip to content

Commit 2b54556

Browse files
committed
Merge remote-tracking branch 'origin/MC-39109' into 2.4-develop-pr46
2 parents 2dac4d1 + 60c0dbb commit 2b54556

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-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\Api\PaymentMethodListInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
16+
/**
17+
* Payment vault information management process
18+
*/
19+
class PaymentVaultInformationManagement
20+
{
21+
/**
22+
* @var PaymentMethodListInterface
23+
*/
24+
private $vaultPaymentMethodList;
25+
26+
/**
27+
* @var StoreManagerInterface
28+
*/
29+
private $storeManager;
30+
31+
/**
32+
* PaymentVaultInformationManagement constructor.
33+
*
34+
* @param PaymentMethodListInterface $vaultPaymentMethodList
35+
* @param StoreManagerInterface $storeManager
36+
*/
37+
public function __construct(
38+
PaymentMethodListInterface $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+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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\Test\Unit\Plugin;
9+
10+
use Magento\Checkout\Api\PaymentInformationManagementInterface;
11+
use Magento\Store\Api\Data\StoreInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Vault\Api\PaymentMethodListInterface;
14+
use Magento\Vault\Plugin\PaymentVaultInformationManagement;
15+
use Magento\Quote\Api\Data\PaymentInterface;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Test for payment vault information management plugin
21+
*/
22+
class PaymentVaultInformationManagementTest extends TestCase
23+
{
24+
/**
25+
* @var StoreManagerInterface|MockObject
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @var StoreInterface|MockObject
31+
*/
32+
private $store;
33+
34+
/**
35+
* @var PaymentMethodListInterface|MockObject
36+
*/
37+
private $paymentMethodList;
38+
39+
/**
40+
* @var PaymentVaultInformationManagement
41+
*/
42+
private $plugin;
43+
44+
/**
45+
* @var PaymentInformationManagementInterface|MockObject
46+
*/
47+
private $paymentInformationManagement;
48+
49+
/**
50+
* @var PaymentInterface|MockObject
51+
*/
52+
private $payment;
53+
54+
/**
55+
* @inheritDoc
56+
*/
57+
protected function setUp(): void
58+
{
59+
$this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
60+
->disableOriginalConstructor()
61+
->onlyMethods(['getStore'])
62+
->getMockForAbstractClass();
63+
$this->store = $this->getMockBuilder(StoreInterface::class)
64+
->disableOriginalConstructor()
65+
->onlyMethods(['getId'])
66+
->getMockForAbstractClass();
67+
$this->paymentMethodList = $this->getMockBuilder(PaymentMethodListInterface::class)
68+
->disableOriginalConstructor()
69+
->onlyMethods(['getActiveList'])
70+
->getMockForAbstractClass();
71+
$this->paymentInformationManagement = $this
72+
->getMockBuilder(PaymentInformationManagementInterface::class)
73+
->disableOriginalConstructor()
74+
->getMockForAbstractClass();
75+
$this->payment = $this->getMockBuilder(PaymentInterface::class)
76+
->onlyMethods(['setMethod'])
77+
->disableOriginalConstructor()
78+
->getMockForAbstractClass();
79+
$this->plugin = new PaymentVaultInformationManagement($this->paymentMethodList, $this->storeManager);
80+
}
81+
82+
/**
83+
* Test payment method for vault before saving payment information
84+
*
85+
* @param string $requestPaymentMethodCode
86+
* @param string $methodCode
87+
* @dataProvider vaultPaymentMethodDataProvider
88+
*
89+
* @return void
90+
*/
91+
public function testBeforeSavePaymentInformation($requestPaymentMethodCode, $methodCode): void
92+
{
93+
$this->store->method('getId')
94+
->willReturn(1);
95+
$this->storeManager->method('getStore')
96+
->willReturn($this->store);
97+
$activeVaultMethod = $this->getMockBuilder(PaymentInterface::class)
98+
->disableOriginalConstructor()
99+
->addMethods(['getCode', 'getProviderCode'])
100+
->getMockForAbstractClass();
101+
$activeVaultMethod->method('getCode')
102+
->willReturn($methodCode);
103+
$this->paymentMethodList->method('getActiveList')
104+
->willReturn([$activeVaultMethod]);
105+
$this->payment->method('getMethod')
106+
->willReturn($requestPaymentMethodCode);
107+
$this->payment->expects($this->once())
108+
->method('setMethod')
109+
->with($methodCode);
110+
111+
$this->plugin->beforeSavePaymentInformation(
112+
$this->paymentInformationManagement,
113+
'1',
114+
$this->payment,
115+
null
116+
);
117+
}
118+
119+
/**
120+
* Data provider for BeforeSavePaymentInformation.
121+
*
122+
* @return array
123+
*/
124+
public function vaultPaymentMethodDataProvider(): array
125+
{
126+
return [
127+
['braintree_cc_vault_01', 'braintree_cc_vault'],
128+
];
129+
}
130+
}

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)