Skip to content

Commit a78f200

Browse files
committed
Merge branch 'add-idempotency-key' into 'main'
Add idempotency key See merge request ypmn-public/php-api-client!8
2 parents 7cc51dc + c227768 commit a78f200

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/ApiRequest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,34 @@ class ApiRequest implements ApiRequestInterface
5353
/** @var string Хост для отправки запросов */
5454
private string $host = self::HOST;
5555

56+
/** @var string Ключ идемпотентности */
57+
private string $idempotencyKey;
58+
5659
/** @inheritdoc */
5760
public function __construct(MerchantInterface $merchant)
5861
{
5962
$this->merchant = $merchant;
6063
}
6164

65+
/** @inheritdoc */
66+
public function getIdempotencyKey(): string
67+
{
68+
return $this->idempotencyKey;
69+
}
70+
71+
/** @inheritdoc */
72+
public function setIdempotencyKey(string $idempotencyKey): self
73+
{
74+
if (mb_strlen($idempotencyKey) <= 36) {
75+
$this->idempotencyKey = $idempotencyKey;
76+
77+
return $this;
78+
} else {
79+
throw new PaymentException('Ключ идемпотентности должен быть не длинее 36 символов, подробнее: https://ypmn.ru/ru/documentation/#tag/idempotency');
80+
}
81+
}
82+
83+
6284
/** @inheritdoc */
6385
public function getHost() : string
6486
{
@@ -343,6 +365,10 @@ public function sendPostRequest($data, string $api): array
343365
$this->addCurlOptHeaderFunction($setOptArray, $headers);
344366
}
345367

368+
if ($this->getIdempotencyKey()) {
369+
$headers[] = 'X-Header-Idempotency-Key: ' . $this->getIdempotencyKey();
370+
}
371+
346372
curl_setopt_array($curl, $setOptArray);
347373

348374
$response = curl_exec($curl);

src/ApiRequestInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ public function sendReportGeneralRequest(array $params);
128128
*/
129129
public function sendReportOrderRequest(array $params);
130130

131+
/** @return string Ключ идемпотентности https://ypmn.ru/ru/documentation/#tag/idempotency */
132+
public function getIdempotencyKey() : string;
133+
134+
/**
135+
* @param string $idempotencyKey Ключ идемпотентности https://ypmn.ru/ru/documentation/#tag/idempotency
136+
* @return $this
137+
* @throws PaymentException
138+
*/
139+
public function setIdempotencyKey(string $idempotencyKey) : self;
140+
131141
/**
132142
* Отправить запрос для получения детального отчета по заказу
133143
* @param array $params

0 commit comments

Comments
 (0)