@@ -44,6 +44,8 @@ class ApiRequest implements ApiRequestInterface
44
44
/** @var string Хост для отправки запросов */
45
45
private string $ host = self ::HOST ;
46
46
47
+ private string $ idempotencyKey ;
48
+
47
49
/** @inheritdoc */
48
50
public function __construct (MerchantInterface $ merchant )
49
51
{
@@ -68,6 +70,24 @@ public function setHost(string $host) : self
68
70
}
69
71
}
70
72
73
+ /** @inheritdoc */
74
+ public function getIdempotencyKey (): string
75
+ {
76
+ return $ this ->idempotencyKey ;
77
+ }
78
+
79
+ /** @inheritdoc */
80
+ public function setIdempotencyKey (string $ idempotencyKey ): self
81
+ {
82
+ if (mb_strlen ($ idempotencyKey ) <= 36 ) {
83
+ $ this ->idempotencyKey = $ idempotencyKey ;
84
+
85
+ return $ this ;
86
+ } else {
87
+ throw new PaymentException ('Ключ идемпотентности должен быть не длинее 36 символов, подробнее: https://ypmn.ru/ru/documentation/#tag/idempotency ' );
88
+ }
89
+ }
90
+
71
91
/** @deprecated старая версия */
72
92
public function sendGetReportRequest (?string $ startDate = null , ?string $ endDate = null , ?array $ orderStatus = null ): string
73
93
{
@@ -294,6 +314,24 @@ public function sendPostRequest($data, string $api): array
294
314
$ date = (new DateTime ())->format (DateTimeInterface::ATOM );
295
315
$ requestHttpVerb = 'POST ' ;
296
316
317
+ $ headers = [
318
+ 'Accept: application/json ' ,
319
+ 'Content-Type: application/json ' ,
320
+ 'X-Header-Date: ' . $ date ,
321
+ 'X-Header-Merchant: ' . $ this ->merchant ->getCode (),
322
+ 'X-Header-Signature: ' . $ this ->getSignature (
323
+ $ this ->merchant ,
324
+ $ date ,
325
+ $ this ->getHost () . $ api ,
326
+ $ requestHttpVerb ,
327
+ $ encodedJsonDataHash
328
+ )
329
+ ];
330
+
331
+ if ($ this ->getIdempotencyKey ()) {
332
+ $ headers [] = 'X-Header-Idempotency-Key: ' . $ this ->getIdempotencyKey ();
333
+ }
334
+
297
335
curl_setopt_array ($ curl , [
298
336
CURLOPT_URL => $ this ->getHost () . $ api ,
299
337
CURLOPT_RETURNTRANSFER => true ,
@@ -303,19 +341,7 @@ public function sendPostRequest($data, string $api): array
303
341
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
304
342
CURLOPT_CUSTOMREQUEST => $ requestHttpVerb ,
305
343
CURLOPT_POSTFIELDS => $ encodedJsonData ,
306
- CURLOPT_HTTPHEADER => [
307
- 'Accept: application/json ' ,
308
- 'Content-Type: application/json ' ,
309
- 'X-Header-Date: ' . $ date ,
310
- 'X-Header-Merchant: ' . $ this ->merchant ->getCode (),
311
- 'X-Header-Signature: ' . $ this ->getSignature (
312
- $ this ->merchant ,
313
- $ date ,
314
- $ this ->getHost () . $ api ,
315
- $ requestHttpVerb ,
316
- $ encodedJsonDataHash
317
- )
318
- ]
344
+ CURLOPT_HTTPHEADER => $ headers
319
345
]);
320
346
321
347
$ response = curl_exec ($ curl );
0 commit comments