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 AmazonPay\ResponseInterface; |
11
|
|
|
use ArrayObject; |
12
|
|
|
use Generated\Shared\Transfer\AddressTransfer; |
13
|
|
|
use Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer; |
14
|
|
|
use Generated\Shared\Transfer\AmazonpayResponseHeaderTransfer; |
15
|
|
|
use Generated\Shared\Transfer\AmazonpayResponseTransfer; |
16
|
|
|
use SprykerEco\Shared\AmazonPay\AmazonPayConfig; |
17
|
|
|
|
18
|
|
|
abstract class AbstractResponseParserConverter extends AbstractConverter implements ResponseParserConverterInterface |
19
|
|
|
{ |
20
|
|
|
public const STATUS_CODE_SUCCESS = 200; |
21
|
|
|
public const ORDER_REFERENCE_DETAILS = 'OrderReferenceDetails'; |
22
|
|
|
public const CONSTRAINTS = 'Constraints'; |
23
|
|
|
public const DESCRIPTION = 'Description'; |
24
|
|
|
public const CONSTRAINT_ID = 'ConstraintID'; |
25
|
|
|
public const REQUEST_ID = 'RequestId'; |
26
|
|
|
public const ERROR = 'Error'; |
27
|
|
|
public const RESPONSE_METADATA = 'ResponseMetadata'; |
28
|
|
|
public const RESPONSE_STATUS = 'ResponseStatus'; |
29
|
|
|
public const MESSAGE = 'Message'; |
30
|
|
|
public const CODE = 'Code'; |
31
|
|
|
public const DESTINATION = 'Destination'; |
32
|
|
|
public const PHYSICAL_DESTINATION = 'PhysicalDestination'; |
33
|
|
|
public const NAME = 'Name'; |
34
|
|
|
public const CITY = 'City'; |
35
|
|
|
public const COUNTRY_CODE = 'CountryCode'; |
36
|
|
|
public const POSTAL_CODE = 'PostalCode'; |
37
|
|
|
public const ADDRESS_LINE_1 = 'AddressLine1'; |
38
|
|
|
public const ADDRESS_LINE_2 = 'AddressLine2'; |
39
|
|
|
public const ADDRESS_LINE_3 = 'AddressLine3'; |
40
|
|
|
public const DISTRICT = 'District'; |
41
|
|
|
public const STATE_OR_REGION = 'StateOrRegion'; |
42
|
|
|
public 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 \AmazonPay\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 $this->extractStatusCode($response) === static::STATUS_CODE_SUCCESS |
196
|
|
|
&& $this->extractConstraints($response)->count() === 0; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $response |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
protected function extractResult(array $response) |
205
|
|
|
{ |
206
|
|
|
$responseType = $this->getResponseType(); |
207
|
|
|
|
208
|
|
|
return $response[$responseType] ?? []; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param array $response |
213
|
|
|
* |
214
|
|
|
* @return \ArrayObject|\Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer[] |
215
|
|
|
*/ |
216
|
|
|
protected function extractConstraints(array $response) |
217
|
|
|
{ |
218
|
|
|
$result = $this->extractResult($response); |
219
|
|
|
|
220
|
|
|
if (empty($result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS])) { |
221
|
|
|
return new ArrayObject(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$constraints = $this->getConstraints($result); |
225
|
|
|
|
226
|
|
|
return $this->buildConstraintTransfers($constraints); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param array $constraints |
231
|
|
|
* |
232
|
|
|
* @return \ArrayObject|\Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer[] |
233
|
|
|
*/ |
234
|
|
|
protected function buildConstraintTransfers(array $constraints) |
235
|
|
|
{ |
236
|
|
|
$constraintTransfers = new ArrayObject(); |
237
|
|
|
|
238
|
|
|
foreach ($constraints as $constraint) { |
239
|
|
|
if ($this->isValidConstraint($constraint)) { |
240
|
|
|
$constraintTransfers[] = $this->buildConstraintTransfer($constraint); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $constraintTransfers; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param array $constraint |
249
|
|
|
* |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
|
|
protected function isValidConstraint(array $constraint) |
253
|
|
|
{ |
254
|
|
|
return !empty($constraint[static::CONSTRAINT_ID]) && !empty($constraint[static::DESCRIPTION]); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param array $result |
259
|
|
|
* |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
protected function getConstraints(array $result) |
263
|
|
|
{ |
264
|
|
|
if (count($result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS]) === 1) { |
265
|
|
|
return array_values($result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS]); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $result[static::ORDER_REFERENCE_DETAILS][static::CONSTRAINTS]; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param array $constraint |
273
|
|
|
* |
274
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer |
275
|
|
|
*/ |
276
|
|
|
protected function buildConstraintTransfer(array $constraint) |
277
|
|
|
{ |
278
|
|
|
$constraintTransfer = new AmazonpayResponseConstraintTransfer(); |
279
|
|
|
$constraintTransfer->setConstraintId($constraint[static::CONSTRAINT_ID]); |
280
|
|
|
$constraintTransfer->setConstraintDescription($constraint[static::DESCRIPTION]); |
281
|
|
|
|
282
|
|
|
return $constraintTransfer; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param array $response |
287
|
|
|
* |
288
|
|
|
* @return \Generated\Shared\Transfer\AddressTransfer |
289
|
|
|
*/ |
290
|
|
|
protected function extractShippingAddress(array $response) |
291
|
|
|
{ |
292
|
|
|
$address = new AddressTransfer(); |
293
|
|
|
|
294
|
|
|
if (!$this->isSuccess($response)) { |
295
|
|
|
return $address; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$aResponseAddress = |
299
|
|
|
$this->extractResult($response)[static::ORDER_REFERENCE_DETAILS][static::DESTINATION][static::PHYSICAL_DESTINATION] ?? null; |
300
|
|
|
|
301
|
|
|
if ($aResponseAddress !== null) { |
302
|
|
|
$address = $this->convertAddressToTransfer($aResponseAddress); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $address; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param array $addressData |
310
|
|
|
* |
311
|
|
|
* @return \Generated\Shared\Transfer\AddressTransfer |
312
|
|
|
*/ |
313
|
|
|
protected function convertAddressToTransfer(array $addressData) |
314
|
|
|
{ |
315
|
|
|
$address = new AddressTransfer(); |
316
|
|
|
|
317
|
|
|
if (!empty($addressData[static::NAME])) { |
318
|
|
|
$address = $this->updateNameData($address, $addressData[static::NAME]); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$addressData = array_map([$this, 'getStringValue'], $addressData); |
322
|
|
|
|
323
|
|
|
$address->setCity($addressData[static::CITY] ?? null); |
324
|
|
|
$address->setIso2Code($addressData[static::COUNTRY_CODE] ?? null); |
325
|
|
|
$address->setZipCode($addressData[static::POSTAL_CODE] ?? null); |
326
|
|
|
$address->setAddress1($addressData[static::ADDRESS_LINE_1] ?? null); |
327
|
|
|
$address->setAddress2($addressData[static::ADDRESS_LINE_2] ?? null); |
328
|
|
|
$address->setAddress3($addressData[static::ADDRESS_LINE_3] ?? null); |
329
|
|
|
$address->setRegion($addressData[static::DISTRICT] ?? null); |
330
|
|
|
$address->setState($addressData[static::STATE_OR_REGION] ?? null); |
331
|
|
|
$address->setPhone($addressData[static::PHONE] ?? null); |
332
|
|
|
|
333
|
|
|
return $address; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param \Generated\Shared\Transfer\AddressTransfer $transfer |
338
|
|
|
* @param string $name |
339
|
|
|
* |
340
|
|
|
* @return \Generated\Shared\Transfer\AddressTransfer |
341
|
|
|
*/ |
342
|
|
|
protected function updateNameData(AddressTransfer $transfer, $name) |
343
|
|
|
{ |
344
|
|
|
$names = $this->getNameData($name); |
345
|
|
|
|
346
|
|
|
$transfer->setFirstName($names[0]); |
347
|
|
|
$transfer->setLastName($names[1]); |
348
|
|
|
|
349
|
|
|
return $transfer; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @param string|array $value |
354
|
|
|
* |
355
|
|
|
* @return string|null |
356
|
|
|
*/ |
357
|
|
|
protected function getStringValue($value) |
358
|
|
|
{ |
359
|
|
|
return empty($value) ? null : (string)$value; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param \Generated\Shared\Transfer\AmazonpayResponseConstraintTransfer $constraint |
364
|
|
|
* |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
|
|
protected function buildErrorMessage(AmazonpayResponseConstraintTransfer $constraint) |
368
|
|
|
{ |
369
|
|
|
return AmazonPayConfig::PREFIX_AMAZONPAY_PAYMENT_ERROR . $constraint->getConstraintId(); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|