@@ -44,6 +44,8 @@ class ApiRequest implements ApiRequestInterface
4444 /** @var string Хост для отправки запросов */
4545 private string $ host = self ::HOST ;
4646
47+ private string $ idempotencyKey ;
48+
4749 /** @inheritdoc */
4850 public function __construct (MerchantInterface $ merchant )
4951 {
@@ -68,6 +70,24 @@ public function setHost(string $host) : self
6870 }
6971 }
7072
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+
7191 /** @deprecated старая версия */
7292 public function sendGetReportRequest (?string $ startDate = null , ?string $ endDate = null , ?array $ orderStatus = null ): string
7393 {
@@ -294,6 +314,24 @@ public function sendPostRequest($data, string $api): array
294314 $ date = (new DateTime ())->format (DateTimeInterface::ATOM );
295315 $ requestHttpVerb = 'POST ' ;
296316
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+
297335 curl_setopt_array ($ curl , [
298336 CURLOPT_URL => $ this ->getHost () . $ api ,
299337 CURLOPT_RETURNTRANSFER => true ,
@@ -303,19 +341,7 @@ public function sendPostRequest($data, string $api): array
303341 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
304342 CURLOPT_CUSTOMREQUEST => $ requestHttpVerb ,
305343 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
319345 ]);
320346
321347 $ response = curl_exec ($ curl );
0 commit comments