1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Apache OSL-2 |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\AmazonPay\Business\Api\Converter; |
9
|
|
|
|
10
|
|
|
use ArrayObject; |
11
|
|
|
use Generated\Shared\Transfer\AddressTransfer; |
|
|
|
|
12
|
|
|
use Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer; |
|
|
|
|
13
|
|
|
use Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer; |
|
|
|
|
14
|
|
|
use Generated\Shared\Transfer\AmazonpayResponseTransfer; |
|
|
|
|
15
|
|
|
use PayWithAmazon\ResponseInterface; |
16
|
|
|
use SprykerEco\Shared\AmazonPay\AmazonPayConfig; |
17
|
|
|
|
18
|
|
|
abstract class AbstractResponseParserConverter extends AbstractConverter implements ResponseParserConverterInterface |
19
|
|
|
{ |
20
|
|
|
const STATUS_CODE_SUCCESS = 200; |
21
|
|
|
const ORDER_REFERENCE_DETAILS = 'OrderReferenceDetails'; |
22
|
|
|
const CONSTRAINTS = 'Constraints'; |
23
|
|
|
const DESCRIPTION = 'Description'; |
24
|
|
|
const CONSTRAINT_ID = 'ConstraintID'; |
25
|
|
|
const REQUEST_ID = 'RequestId'; |
26
|
|
|
const ERROR = 'Error'; |
27
|
|
|
const RESPONSE_METADATA = 'ResponseMetadata'; |
28
|
|
|
const RESPONSE_STATUS = 'ResponseStatus'; |
29
|
|
|
const MESSAGE = 'Message'; |
30
|
|
|
const CODE = 'Code'; |
31
|
|
|
const DESTINATION = 'Destination'; |
32
|
|
|
const PHYSICAL_DESTINATION = 'PhysicalDestination'; |
33
|
|
|
const NAME = 'Name'; |
34
|
|
|
const CITY = 'City'; |
35
|
|
|
const COUNTRY_CODE = 'CountryCode'; |
36
|
|
|
const POSTAL_CODE = 'PostalCode'; |
37
|
|
|
const ADDRESS_LINE_1 = 'AddressLine1'; |
38
|
|
|
const ADDRESS_LINE_2 = 'AddressLine2'; |
39
|
|
|
const ADDRESS_LINE_3 = 'AddressLine3'; |
40
|
|
|
const DISTRICT = 'District'; |
41
|
|
|
const STATE_OR_REGION = 'StateOrRegion'; |
42
|
|
|
const PHONE = 'Phone'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $resultKeyName; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
abstract protected function getResponseType(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseTransfer |
56
|
|
|
*/ |
57
|
|
|
protected function createTransferObject() |
58
|
|
|
{ |
59
|
|
|
return new AmazonpayResponseTransfer(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayResponseTransfer $responseTransfer |
64
|
|
|
* @param array $response |
65
|
|
|
* |
66
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseTransfer |
67
|
|
|
*/ |
68
|
|
|
protected function setBody(AmazonpayResponseTransfer $responseTransfer, array $response) |
69
|
|
|
{ |
70
|
|
|
return $responseTransfer; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayResponseTransfer $responseTransfer |
75
|
|
|
* @param array $response |
76
|
|
|
* |
77
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseTransfer |
78
|
|
|
*/ |
79
|
|
|
protected function mapResponseDataToTransfer(AmazonpayResponseTransfer $responseTransfer, array $response) |
80
|
|
|
{ |
81
|
|
|
$responseTransfer->setResponseHeader($this->extractHeader($response)); |
82
|
|
|
|
83
|
|
|
if ($responseTransfer->getResponseHeader()->getIsSuccess()) { |
84
|
|
|
return $this->setBody($responseTransfer, $response); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $responseTransfer; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param \PayWithAmazon\ResponseInterface $responseParser |
92
|
|
|
* |
93
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseTransfer |
94
|
|
|
*/ |
95
|
|
|
public function convert(ResponseInterface $responseParser) |
96
|
|
|
{ |
97
|
|
|
return $this->mapResponseDataToTransfer($this->createTransferObject(), $responseParser->toArray()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $response |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
protected function extractMetadata(array $response) |
106
|
|
|
{ |
107
|
|
|
return $response[static::RESPONSE_METADATA] ?? []; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $response |
112
|
|
|
* |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
protected function extractStatusCode(array $response) |
116
|
|
|
{ |
117
|
|
|
return (int)$response[static::RESPONSE_STATUS]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $response |
122
|
|
|
* |
123
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer |
124
|
|
|
*/ |
125
|
|
|
protected function extractHeader(array $response) |
126
|
|
|
{ |
127
|
|
|
$header = new AmazonpayResponseHeaderTransfer(); |
128
|
|
|
$header->setIsSuccess($this->isSuccess($response)); |
129
|
|
|
|
130
|
|
|
$statusCode = $this->extractStatusCode($response); |
131
|
|
|
$header->setStatusCode($statusCode); |
132
|
|
|
|
133
|
|
|
$this->extractRequest($header, $response); |
134
|
|
|
|
135
|
|
|
$constraints = $this->extractConstraints($response); |
136
|
|
|
$header->setConstraints($constraints); |
137
|
|
|
$this->extractErrorFromConstraints($header); |
138
|
|
|
|
139
|
|
|
return $header; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $header |
144
|
|
|
* @param array $response |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
protected function extractRequest(AmazonpayResponseHeaderTransfer $header, array $response) |
149
|
|
|
{ |
150
|
|
|
$metadata = $this->extractMetadata($response); |
151
|
|
|
if (isset($metadata[static::REQUEST_ID])) { |
152
|
|
|
$header->setRequestId($metadata[static::REQUEST_ID]); |
153
|
|
|
|
154
|
|
|
if (!empty($response[static::ERROR])) { |
155
|
|
|
$this->updateHeaderWithError($header, $response); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $header |
162
|
|
|
* @param array $response |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
protected function updateHeaderWithError(AmazonpayResponseHeaderTransfer $header, array $response) |
167
|
|
|
{ |
168
|
|
|
$header->setErrorMessage($response[static::ERROR][static::MESSAGE]); |
169
|
|
|
$header->setErrorCode($response[static::ERROR][static::CODE]); |
170
|
|
|
$header->setRequestId($response[static::REQUEST_ID]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer $header |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
protected function extractErrorFromConstraints(AmazonpayResponseHeaderTransfer $header) |
179
|
|
|
{ |
180
|
|
|
if ($header->getConstraints()->count() === 0) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$constraint = $header->getConstraints()[0]; |
185
|
|
|
$header->setErrorMessage($this->buildErrorMessage($constraint)); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $response |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
protected function isSuccess(array $response) |
194
|
|
|
{ |
195
|
|
|
return |
196
|
|
|
$this->extractStatusCode($response) === static::STATUS_CODE_SUCCESS |
197
|
|
|
&& $this->extractConstraints($response)->count() === 0; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param array $response |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
|
|
protected function extractResult(array $response) |
206
|
|
|
{ |
207
|
|
|
$responseType = $this->getResponseType(); |
208
|
|
|
|
209
|
|
|
return $response[$responseType] ?? []; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param array $response |
214
|
|
|
* |
215
|
|
|
* @return \ArrayObject|\Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer[] |
216
|
|
|
*/ |
217
|
|
|
protected function extractConstraints(array $response) |
218
|
|
|
{ |
219
|
|
|
$result = $this->extractResult($response); |
220
|
|
|
|
221
|
|
|
if (empty($result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS])) { |
222
|
|
|
return new ArrayObject(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$constraints = $this->getConstraints($result); |
226
|
|
|
|
227
|
|
|
return $this->buildConstraintTransfers($constraints); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param array $constraints |
232
|
|
|
* |
233
|
|
|
* @return \ArrayObject|\Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer[] |
234
|
|
|
*/ |
235
|
|
|
protected function buildConstraintTransfers(array $constraints) |
236
|
|
|
{ |
237
|
|
|
$constraintTransfers = new ArrayObject(); |
238
|
|
|
|
239
|
|
|
foreach ($constraints as $constraint) { |
240
|
|
|
if ($this->isValidConstraint($constraint)) { |
241
|
|
|
$constraintTransfers[] = $this->buildConstraintTransfer($constraint); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $constraintTransfers; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param array $constraint |
250
|
|
|
* |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
protected function isValidConstraint(array $constraint) |
254
|
|
|
{ |
255
|
|
|
return !empty($constraint[static::CONSTRAINT_ID]) && !empty($constraint[static::DESCRIPTION]); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param array $result |
260
|
|
|
* |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
protected function getConstraints(array $result) |
264
|
|
|
{ |
265
|
|
|
if (count($result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS]) === 1) { |
266
|
|
|
return array_values($result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS]); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS]; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param array $constraint |
274
|
|
|
* |
275
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer |
276
|
|
|
*/ |
277
|
|
|
protected function buildConstraintTransfer(array $constraint) |
278
|
|
|
{ |
279
|
|
|
$constraintTransfer = new AmazonpayResponseConstraintTransfer(); |
280
|
|
|
$constraintTransfer->setConstraintId($constraint[static::CONSTRAINT_ID]); |
281
|
|
|
$constraintTransfer->setConstraintDescription($constraint[static::DESCRIPTION]); |
282
|
|
|
|
283
|
|
|
return $constraintTransfer; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param array $response |
288
|
|
|
* |
289
|
|
|
* @return \Generated\Shared\Transfer\AddressTransfer |
290
|
|
|
*/ |
291
|
|
|
protected function extractShippingAddress(array $response) |
292
|
|
|
{ |
293
|
|
|
$address = new AddressTransfer(); |
294
|
|
|
|
295
|
|
|
if (!$this->isSuccess($response)) { |
296
|
|
|
return $address; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$aResponseAddress = |
300
|
|
|
$this->extractResult($response)[static::ORDER_REFERENCE_DETAILS][static::DESTINATION][static::PHYSICAL_DESTINATION] ?? null; |
301
|
|
|
|
302
|
|
|
if ($aResponseAddress !== null) { |
303
|
|
|
$address = $this->convertAddressToTransfer($aResponseAddress); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $address; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param array $addressData |
311
|
|
|
* |
312
|
|
|
* @return \Generated\Shared\Transfer\AddressTransfer |
313
|
|
|
*/ |
314
|
|
|
protected function convertAddressToTransfer(array $addressData) |
315
|
|
|
{ |
316
|
|
|
$address = new AddressTransfer(); |
317
|
|
|
|
318
|
|
|
if (!empty($addressData[static::NAME])) { |
319
|
|
|
$address = $this->updateNameData($address, $addressData[static::NAME]); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$addressData = array_map([$this, 'getStringValue'], $addressData); |
323
|
|
|
|
324
|
|
|
$address->setCity($addressData[static::CITY] ?? null); |
325
|
|
|
$address->setIso2Code($addressData[static::COUNTRY_CODE] ?? null); |
326
|
|
|
$address->setZipCode($addressData[static::POSTAL_CODE] ?? null); |
327
|
|
|
$address->setAddress1($addressData[static::ADDRESS_LINE_1] ?? null); |
328
|
|
|
$address->setAddress2($addressData[static::ADDRESS_LINE_2] ?? null); |
329
|
|
|
$address->setAddress3($addressData[static::ADDRESS_LINE_3] ?? null); |
330
|
|
|
$address->setRegion($addressData[static::DISTRICT] ?? null); |
331
|
|
|
$address->setState($addressData[static::STATE_OR_REGION] ?? null); |
332
|
|
|
$address->setPhone($addressData[static::PHONE] ?? null); |
333
|
|
|
|
334
|
|
|
return $address; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param string|array $value |
339
|
|
|
* |
340
|
|
|
* @return string|null |
341
|
|
|
*/ |
342
|
|
|
protected function getStringValue($value) |
343
|
|
|
{ |
344
|
|
|
return empty($value) ? null : (string)$value; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer $constraint |
349
|
|
|
* |
350
|
|
|
* @return string |
351
|
|
|
*/ |
352
|
|
|
protected function buildErrorMessage(AmazonpayResponseConstraintTransfer $constraint) |
353
|
|
|
{ |
354
|
|
|
return AmazonPayConfig::PREFIX_AMAZONPAY_PAYMENT_ERROR . $constraint->getConstraintId(); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths