Skip to content

Commit 6dafce1

Browse files
committed
Merge branch 'YP-1351-dobavit-v-biblioteku-php-api-client-opczionalnyj-setter-idempotency-key' into 'main'
Add idempotency key See merge request ypmn/php-api-client!2
2 parents 53e50cb + d576500 commit 6dafce1

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/ApiRequest.php

+39-13
Original file line numberDiff line numberDiff line change
@@ -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);

src/ApiRequestInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ public function getHost() : string;
128128
*/
129129
public function setHost(string $host) : self;
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 PodeliMerchant $merchant

0 commit comments

Comments
 (0)