8
8
9
9
namespace CM \Payments \Service ;
10
10
11
- use CM \Payments \Api \Data \IssuerInterface ;
12
- use CM \Payments \Api \Data \IssuerInterfaceFactory ;
13
- use CM \Payments \Api \Data \PaymentMethodAdditionalDataInterface ;
14
11
use CM \Payments \Api \Data \PaymentMethodAdditionalDataInterfaceFactory ;
12
+ use CM \Payments \Api \Service \Method \ExtendMethodInterface ;
15
13
use CM \Payments \Api \Service \MethodServiceInterface ;
16
14
use CM \Payments \Api \Service \OrderRequestBuilderInterface ;
17
15
use CM \Payments \Client \Api \OrderInterface as OrderClientInterface ;
18
- use CM \Payments \Client \Model \Response \Method \IdealIssuer ;
16
+ use CM \Payments \Client \Model \Response \OrderCreate ;
17
+ use CM \Payments \Client \Model \Response \PaymentMethod ;
19
18
use CM \Payments \Client \Request \OrderGetMethodsRequestFactory ;
20
19
use CM \Payments \Config \Config as ConfigService ;
21
20
use CM \Payments \Logger \CMPaymentsLogger ;
24
23
use Magento \Checkout \Api \Data \PaymentDetailsExtensionInterfaceFactory ;
25
24
use Magento \Checkout \Api \Data \PaymentDetailsInterface ;
26
25
use Magento \Framework \Exception \LocalizedException ;
26
+ use Magento \Framework \Exception \NoSuchEntityException ;
27
27
use Magento \Quote \Api \Data \CartInterface ;
28
28
29
29
class MethodService implements MethodServiceInterface
@@ -54,9 +54,9 @@ class MethodService implements MethodServiceInterface
54
54
private $ paymentMethodAdditionalDataFactory ;
55
55
56
56
/**
57
- * @var IssuerInterfaceFactory
57
+ * @var ExtendMethodInterface[]
58
58
*/
59
- private $ issuerFactory ;
59
+ private $ methods ;
60
60
61
61
/**
62
62
* @var PaymentDetailsExtensionInterfaceFactory
@@ -76,7 +76,7 @@ class MethodService implements MethodServiceInterface
76
76
* @param OrderRequestBuilderInterface $orderRequestBuilder
77
77
* @param OrderGetMethodsRequestFactory $orderGetMethodsRequestFactory
78
78
* @param PaymentMethodAdditionalDataInterfaceFactory $paymentMethodAdditionalDataFactory
79
- * @param IssuerInterfaceFactory $issuerFactory
79
+ * @param ExtendMethodInterface[] $methods
80
80
* @param PaymentDetailsExtensionInterfaceFactory $paymentDetailsExtensionFactory
81
81
* @param CMPaymentsLogger $cmPaymentsLogger
82
82
*/
@@ -86,7 +86,7 @@ public function __construct(
86
86
OrderRequestBuilderInterface $ orderRequestBuilder ,
87
87
OrderGetMethodsRequestFactory $ orderGetMethodsRequestFactory ,
88
88
PaymentMethodAdditionalDataInterfaceFactory $ paymentMethodAdditionalDataFactory ,
89
- IssuerInterfaceFactory $ issuerFactory ,
89
+ array $ methods ,
90
90
PaymentDetailsExtensionInterfaceFactory $ paymentDetailsExtensionFactory ,
91
91
CMPaymentsLogger $ cmPaymentsLogger
92
92
) {
@@ -95,7 +95,7 @@ public function __construct(
95
95
$ this ->orderRequestBuilder = $ orderRequestBuilder ;
96
96
$ this ->orderGetMethodsRequestFactory = $ orderGetMethodsRequestFactory ;
97
97
$ this ->paymentMethodAdditionalDataFactory = $ paymentMethodAdditionalDataFactory ;
98
- $ this ->issuerFactory = $ issuerFactory ;
98
+ $ this ->methods = $ methods ;
99
99
$ this ->paymentDetailsExtensionFactory = $ paymentDetailsExtensionFactory ;
100
100
$ this ->logger = $ cmPaymentsLogger ;
101
101
}
@@ -107,121 +107,103 @@ public function addMethodAdditionalData(
107
107
CartInterface $ quote ,
108
108
PaymentDetailsInterface $ paymentDetails
109
109
): PaymentDetailsInterface {
110
- $ availablePaymentMethods = $ paymentDetails ->getPaymentMethods ();
111
- $ availableProfileMethods = $ this ->getAvailablePaymentMethods ($ quote );
112
- $ issuers = [];
113
-
114
- $ paymentDetailsExtension = $ paymentDetails ->getExtensionAttributes ();
115
- if ($ paymentDetailsExtension == null ) {
116
- /** @var PaymentDetailsExtensionInterface $paymentDetailsExtension */
117
- $ paymentDetailsExtension = $ this ->paymentDetailsExtensionFactory ->create ();
118
- }
119
-
120
- foreach ($ availablePaymentMethods as $ id => $ paymentMethod ) {
121
- if (strpos ($ paymentMethod ->getCode (), ConfigProvider::CODE . '_ ' ) === false ) {
122
- continue ;
123
- }
124
-
125
- if (!isset ($ availableProfileMethods [$ paymentMethod ->getCode ()])) {
126
- unset($ availablePaymentMethods [$ id ]);
127
- }
128
-
129
- if ($ paymentMethod ->getCode () == ConfigProvider::CODE_IDEAL ) {
130
- if (isset ($ availableProfileMethods [$ paymentMethod ->getCode ()])) {
131
- $ methodData = $ availableProfileMethods [$ paymentMethod ->getCode ()];
132
- if (!empty ($ methodData ['ideal_details ' ]['issuers ' ])) {
133
- foreach ($ methodData ['ideal_details ' ]['issuers ' ] as $ issuer ) {
134
- /** @var IssuerInterface $issuerObject */
135
- $ issuerObject = $ this ->issuerFactory ->create ();
136
- $ issuerObject ->addData ($ issuer );
137
- $ issuers [] = $ issuerObject ;
138
- }
139
- }
140
- }
141
-
142
- /** @var PaymentMethodAdditionalDataInterface $paymentMethodAdditionalData */
143
- $ paymentMethodAdditionalData = $ this ->paymentMethodAdditionalDataFactory ->create ();
144
- $ paymentMethodAdditionalData ->setIssuers ($ issuers );
145
- $ paymentDetailsExtension ->setData ($ paymentMethod ->getCode (), $ paymentMethodAdditionalData );
110
+ try {
111
+ $ availablePaymentMethods = $ paymentDetails ->getPaymentMethods ();
112
+ $ cmOrder = $ this ->createCmOrder ($ quote );
113
+ if (empty ($ cmOrder ->getOrderKey ())) {
114
+ throw new LocalizedException (
115
+ __ ("The Methods were not requested properly because of CM Order creation problem. " )
116
+ );
146
117
}
147
- }
148
118
149
- $ paymentDetails ->setExtensionAttributes ($ paymentDetailsExtension );
150
- $ paymentDetails ->setPaymentMethods ($ availablePaymentMethods );
151
-
152
- return $ paymentDetails ;
153
- }
154
-
155
- /**
156
- * @inheritDoc
157
- */
158
- public function getAvailablePaymentMethods (CartInterface $ quote ): array
159
- {
160
- $ availableMethods = [];
161
- try {
162
- $ orderCreateRequest = $ this ->orderRequestBuilder ->createByQuote ($ quote , true );
163
- $ response = $ this ->orderClient ->create (
164
- $ orderCreateRequest
119
+ $ cmPaymentMethods = $ this ->orderClient ->getMethods (
120
+ $ cmOrder ->getOrderKey ()
165
121
);
122
+ $ cmPaymentMethods = $ this ->getMappedCmPaymentMethods ($ cmPaymentMethods );
166
123
167
- if (!empty ($ response ->getOrderKey ())) {
168
- $ quote ->setData ('cm_order_key ' , $ response ->getOrderKey ());
169
-
170
- $ availableProfileMethods = $ this ->orderClient ->getMethods (
171
- $ response ->getOrderKey ()
172
- );
173
-
174
- foreach ($ availableProfileMethods as $ availableProfileMethod ) {
175
- $ availableProfileMethodCode = $ availableProfileMethod ->getMethod ();
124
+ $ paymentDetailsExtension = $ paymentDetails ->getExtensionAttributes ();
125
+ if ($ paymentDetailsExtension == null ) {
126
+ /** @var PaymentDetailsExtensionInterface $paymentDetailsExtension */
127
+ $ paymentDetailsExtension = $ this ->paymentDetailsExtensionFactory ->create ();
128
+ }
176
129
177
- if (!isset (self ::METHODS_MAPPING [$ availableProfileMethodCode ])) {
178
- continue ;
179
- }
130
+ foreach ($ availablePaymentMethods as $ id => $ paymentMethod ) {
131
+ if (! $ this ->isCmPaymentsMethod ($ paymentMethod ->getCode ())) {
132
+ continue ;
133
+ }
180
134
181
- $ mappedMethodCode = self ::METHODS_MAPPING [$ availableProfileMethodCode ];
182
- if ($ this ->configService ->isPaymentMethodActive ($ mappedMethodCode )) {
183
- $ methodData = [];
184
- if (!empty ($ availableProfileMethod ->getIdealIssuers ())) {
185
- $ methodData ['ideal_details ' ]['issuers ' ]
186
- = $ this ->prepareIdealIssuers ($ availableProfileMethod ->getIdealIssuers ());
187
- }
135
+ if (!isset ($ cmPaymentMethods [$ paymentMethod ->getCode ()])) {
136
+ unset($ availablePaymentMethods [$ id ]);
137
+ }
188
138
189
- $ availableMethods [$ mappedMethodCode ] = $ methodData ;
139
+ foreach ($ this ->methods as $ method ) {
140
+ if (isset ($ cmPaymentMethods [$ paymentMethod ->getCode ()])) {
141
+ $ paymentDetailsExtension = $ method ->extend (
142
+ $ paymentMethod ->getCode (),
143
+ $ cmPaymentMethods [$ paymentMethod ->getCode ()],
144
+ $ paymentDetailsExtension
145
+ );
190
146
}
191
147
}
192
- } else {
193
- throw new LocalizedException (
194
- __ ("The Methods were not requested properly because of CM Order creation problem. " )
195
- );
196
148
}
149
+
150
+ $ paymentDetails ->setExtensionAttributes ($ paymentDetailsExtension );
151
+ $ paymentDetails ->setPaymentMethods ($ availablePaymentMethods );
197
152
} catch (LocalizedException $ e ) {
198
- $ this ->logger ->info (
153
+ $ this ->logger ->error (
199
154
'CM Get Available Methods request ' ,
200
155
[
201
156
'error ' => $ e ->getMessage (),
202
157
]
203
158
);
204
159
}
205
160
206
- return $ availableMethods ;
161
+ return $ paymentDetails ;
207
162
}
208
163
209
164
/**
210
- * @param IdealIssuer[] $issuerList
211
- * @return array
165
+ * @param PaymentMethod[] $cmPaymentMethods
166
+ * @return array<string, PaymentMethod>
167
+ *
168
+ * @throws NoSuchEntityException
212
169
*/
213
- private function prepareIdealIssuers (array $ issuerList ): array
170
+ private function getMappedCmPaymentMethods (array $ cmPaymentMethods ): array
214
171
{
215
- $ issuers = $ resultIssuerList = [];
216
- foreach ($ issuerList as $ issuer ) {
217
- $ issuers [ $ issuer -> getId ()] = $ issuer -> getName ();
218
- }
219
- asort ( $ issuers , SORT_NATURAL | SORT_FLAG_CASE );
172
+ $ methods = [];
173
+ foreach ($ cmPaymentMethods as $ cmPaymentMethod ) {
174
+ if (! isset ( self :: METHODS_MAPPING [ $ cmPaymentMethod -> getMethod ()])) {
175
+ continue ;
176
+ }
220
177
221
- foreach ($ issuers as $ code => $ title ) {
222
- $ resultIssuerList [] = ['code ' => $ code , 'title ' => $ title ];
178
+ $ mappedMethodCode = self ::METHODS_MAPPING [$ cmPaymentMethod ->getMethod ()];
179
+ if ($ this ->configService ->isPaymentMethodActive ($ mappedMethodCode )) {
180
+ $ methods [$ mappedMethodCode ] = $ cmPaymentMethod ;
181
+ }
223
182
}
224
183
225
- return $ resultIssuerList ;
184
+ return $ methods ;
185
+ }
186
+
187
+ /**
188
+ * @param CartInterface $quote
189
+ * @return OrderCreate
190
+ * @throws LocalizedException
191
+ */
192
+ private function createCmOrder (CartInterface $ quote ): OrderCreate
193
+ {
194
+ $ orderCreateRequest = $ this ->orderRequestBuilder ->createByQuote ($ quote , true );
195
+ $ cmOrder = $ this ->orderClient ->create (
196
+ $ orderCreateRequest
197
+ );
198
+ return $ cmOrder ;
199
+ }
200
+
201
+ /**
202
+ * @param string $paymentMethodCode
203
+ * @return bool
204
+ */
205
+ private function isCmPaymentsMethod (string $ paymentMethodCode ): bool
206
+ {
207
+ return strpos ($ paymentMethodCode , ConfigProvider::CODE . '_ ' ) !== false ;
226
208
}
227
209
}
0 commit comments