forked from ripenecommerce/magento2-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatch-Magento_Payment-M2.1.3-MAGETWO-60351-optimize-payment-methods-checkout.patch
250 lines (249 loc) · 8.89 KB
/
Patch-Magento_Payment-M2.1.3-MAGETWO-60351-optimize-payment-methods-checkout.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
From c41743d409ad75f6ef02da2f06162046a69cdc28 Mon Sep 17 00:00:00 2001
From: Pieter Hoste <[email protected]>
Date: Sun, 23 Apr 2017 10:37:21 +0200
Subject: [PATCH] Backport of MAGETWO-60351 for Magento 2.1 - Unnecessary
disabled payment methods on checkout page #4868
diff --git a/Plugin/PaymentConfigurationProcess.php b/Plugin/PaymentConfigurationProcess.php
new file mode 100644
index 0000000..ff5d7b3
--- /dev/null
+++ b/Plugin/PaymentConfigurationProcess.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Copyright © 2013-2017 Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+namespace Magento\Payment\Plugin;
+
+/**
+ * Class PaymentConfigurationProcess
+ *
+ * Removes inactive payment methods and group from checkout configuration.
+ */
+class PaymentConfigurationProcess
+{
+ /**
+ * @var \Magento\Payment\Api\PaymentMethodListInterface
+ */
+ private $paymentMethodList;
+
+ /**
+ * @var \Magento\Store\Model\StoreManagerInterface
+ */
+ private $storeManager;
+
+ /**
+ * @param \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList
+ * @param \Magento\Store\Model\StoreManagerInterface $storeManager
+ */
+ public function __construct(
+ \Magento\Payment\Api\PaymentMethodListInterface $paymentMethodList,
+ \Magento\Store\Model\StoreManagerInterface $storeManager
+ ) {
+ $this->paymentMethodList = $paymentMethodList;
+ $this->storeManager = $storeManager;
+ }
+
+ /**
+ * Checkout LayoutProcessor before process plugin.
+ *
+ * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $processor
+ * @param array $jsLayout
+ * @return array
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ */
+ public function beforeProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout)
+ {
+ $configuration = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
+ ['children']['payment']['children']['renders']['children'];
+
+ if (!isset($configuration)) {
+ return [$jsLayout];
+ }
+
+ $storeId = $this->storeManager->getStore()->getId();
+ $activePaymentMethodList = $this->paymentMethodList->getActiveList($storeId);
+ $getCodeFunc = function ($method) {
+ return $method->getCode();
+ };
+ $activePaymentMethodCodes = array_map($getCodeFunc, $activePaymentMethodList);
+
+ foreach ($configuration as $paymentGroup => $groupConfig) {
+ $notActivePaymentMethodCodes = array_diff(array_keys($groupConfig['methods']), $activePaymentMethodCodes);
+ foreach ($notActivePaymentMethodCodes as $notActivePaymentMethodCode) {
+ unset($configuration[$paymentGroup]['methods'][$notActivePaymentMethodCode]);
+ }
+ if (empty($configuration[$paymentGroup]['methods'])) {
+ unset($configuration[$paymentGroup]);
+ }
+ }
+
+ return [$jsLayout];
+ }
+}
diff --git a/Test/Unit/Plugin/PaymentConfigurationProcessTest.php b/Test/Unit/Plugin/PaymentConfigurationProcessTest.php
new file mode 100644
index 0000000..646c7bc
--- /dev/null
+++ b/Test/Unit/Plugin/PaymentConfigurationProcessTest.php
@@ -0,0 +1,146 @@
+<?php
+/**
+ * Copyright © 2013-2017 Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+namespace Magento\Payment\Test\Unit\Plugin;
+
+/**
+ * Class PaymentConfigurationProcessTest.
+ */
+class PaymentConfigurationProcessTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $storeManager;
+
+ /**
+ * @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $store;
+
+ /**
+ * @var \Magento\Payment\Api\PaymentMethodListInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $paymentMethodList;
+
+ /**
+ * @var \Magento\Checkout\Block\Checkout\LayoutProcessor|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $layoutProcessor;
+
+ /**
+ * @var \Magento\Payment\Plugin\PaymentConfigurationProcess
+ */
+ private $plugin;
+
+ /**
+ * Set up
+ */
+ protected function setUp()
+ {
+ $this->storeManager = $this
+ ->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['getStore'])
+ ->getMockForAbstractClass();
+ $this->store = $this
+ ->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['getId'])
+ ->getMockForAbstractClass();
+ $this->paymentMethodList = $this
+ ->getMockBuilder(\Magento\Payment\Api\PaymentMethodListInterface::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['getActiveList'])
+ ->getMockForAbstractClass();
+ $this->layoutProcessor = $this
+ ->getMockBuilder(\Magento\Checkout\Block\Checkout\LayoutProcessor::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['process'])
+ ->getMockForAbstractClass();
+
+ $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
+ $this->plugin = $objectManagerHelper->getObject(
+ \Magento\Payment\Plugin\PaymentConfigurationProcess::class,
+ [
+ 'paymentMethodList' => $this->paymentMethodList,
+ 'storeManager' => $this->storeManager
+ ]
+ );
+ }
+
+ /**
+ * @param array $jsLayout
+ * @param array $activePaymentList
+ * @param array $expectedResult
+ * @dataProvider beforeProcessDataProvider
+ */
+ public function testBeforeProcess($jsLayout, $activePaymentList, $expectedResult)
+ {
+ $this->store->expects($this->once())->method('getId')->willReturn(1);
+ $this->storeManager->expects($this->once())->method('getStore')->willReturn($this->store);
+ $this->paymentMethodList->expects($this->once())
+ ->method('getActiveList')
+ ->with(1)
+ ->willReturn($activePaymentList);
+
+ $result = $this->plugin->beforeProcess($this->layoutProcessor, $jsLayout);
+ $this->assertEquals($result[0], $expectedResult);
+ }
+
+ /**
+ * Data provider for BeforeProcess.
+ *
+ * @return array
+ */
+ public function beforeProcessDataProvider()
+ {
+ $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
+ ['children']['payment']['children']['renders']['children'] = [
+ 'braintree' => [
+ 'methods' => [
+ 'braintree_paypal' => [],
+ 'braintree' => []
+ ]
+ ],
+ 'paypal-payments' => [
+ 'methods' => [
+ 'payflowpro' => [],
+ 'payflow_link' => []
+ ]
+ ]
+ ];
+ $result1['components']['checkout']['children']['steps']['children']['billing-step']
+ ['children']['payment']['children']['renders']['children'] = [];
+ $result2['components']['checkout']['children']['steps']['children']['billing-step']
+ ['children']['payment']['children']['renders']['children'] = [
+ 'braintree' => [
+ 'methods' => [
+ 'braintree' => [],
+ 'braintree_paypal' => []
+ ]
+ ]
+ ];
+
+ $braintreePaymentMethod = $this
+ ->getMockBuilder(\Magento\Payment\Api\Data\PaymentMethodInterface::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['getCode'])
+ ->getMockForAbstractClass();
+ $braintreePaypalPaymentMethod = $this
+ ->getMockBuilder(\Magento\Payment\Api\Data\PaymentMethodInterface::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['getCode'])
+ ->getMockForAbstractClass();
+
+ $braintreePaymentMethod->expects($this->any())->method('getCode')->willReturn('braintree');
+ $braintreePaypalPaymentMethod->expects($this->any())->method('getCode')->willReturn('braintree_paypal');
+
+ return [
+ [$jsLayout, [], $result1],
+ [$jsLayout, [$braintreePaymentMethod, $braintreePaypalPaymentMethod], $result2]
+ ];
+ }
+}
diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml
index d86147b..e3d9c35 100644
--- a/etc/frontend/di.xml
+++ b/etc/frontend/di.xml
@@ -19,4 +19,7 @@
</argument>
</arguments>
</type>
+ <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
+ <plugin name="ProcessPaymentConfiguration" type="Magento\Payment\Plugin\PaymentConfigurationProcess"/>
+ </type>
</config>
\ No newline at end of file