From d11a80b2ade98d8edf6a9b45d705dfd91a04410b Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Tue, 15 Oct 2019 01:55:18 +0900 Subject: [PATCH] Use centralized date format for requests' signatures --- src/CdekClient.php | 9 ++++++++- src/Requests/Templates/CalculationAuthorizedRequest.php | 5 +++-- tests/Serialization/CalculationRequestTest.php | 6 +++--- .../CalculationWithTariffListRequestTest.php | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/CdekClient.php b/src/CdekClient.php index eda4683..82596e3 100644 --- a/src/CdekClient.php +++ b/src/CdekClient.php @@ -77,6 +77,13 @@ final class CdekClient implements Contracts\Client, LoggerAwareInterface const PACKAGE_NAME = 'Cdek-SDK'; const VERSION_INFO = '$Format:%h%d by %an +%ae$'; + /** + * Формат даты, который используется для создания подписи запроса. + * + * @var string + */ + const SECURE_DATE_FORMAT = 'Y-m-d\TH:i:sP'; + /** @var ClientInterface */ private $http; @@ -264,7 +271,7 @@ private function serialize(XmlRequest $request): string */ private function getSecure(\DateTimeInterface $date): string { - return \md5($date->format('Y-m-d\TH:i:sP')."&{$this->password}"); + return \md5($date->format(self::SECURE_DATE_FORMAT)."&{$this->password}"); } private function hasAttachment(ResponseInterface $response): bool diff --git a/src/Requests/Templates/CalculationAuthorizedRequest.php b/src/Requests/Templates/CalculationAuthorizedRequest.php index d8ec6d7..93388b4 100644 --- a/src/Requests/Templates/CalculationAuthorizedRequest.php +++ b/src/Requests/Templates/CalculationAuthorizedRequest.php @@ -28,6 +28,7 @@ namespace CdekSDK\Requests\Templates; +use CdekSDK\CdekClient; use CdekSDK\Common\AdditionalService; use CdekSDK\Contracts\DateAware; use CdekSDK\Contracts\JsonRequest; @@ -280,7 +281,7 @@ public function jsonSerialize() 'receiverCityId' => $this->receiverCityId, 'receiverCityPostCode' => $this->receiverCityPostCode, 'currency' => $this->currency, - 'dateExecute' => $this->dateExecute instanceof \DateTimeInterface ? $this->dateExecute->format('Y-m-d') : null, + 'dateExecute' => $this->dateExecute instanceof \DateTimeInterface ? $this->dateExecute->format(CdekClient::SECURE_DATE_FORMAT) : null, ]); if ($this->account === '') { @@ -290,7 +291,7 @@ public function jsonSerialize() return \array_merge($result, [ 'secure' => $this->secure, 'authLogin' => $this->account, - 'dateExecute' => $this->date->format('Y-m-d'), + 'dateExecute' => $this->date->format(CdekClient::SECURE_DATE_FORMAT), ]); } } diff --git a/tests/Serialization/CalculationRequestTest.php b/tests/Serialization/CalculationRequestTest.php index cb46a96..8099c02 100644 --- a/tests/Serialization/CalculationRequestTest.php +++ b/tests/Serialization/CalculationRequestTest.php @@ -143,7 +143,7 @@ public function test_with_authorization() 'receiverCityPostCode' => '652632', 'secure' => 'bar', 'authLogin' => 'foo', - 'dateExecute' => '2018-01-01', + 'dateExecute' => '2018-01-01T00:00:00+00:00', ], $request->getBody()); } @@ -194,7 +194,7 @@ public function test_with_currency_and_date() $this->assertSame([ 'version' => '1.0', 'currency' => 'EUR', - 'dateExecute' => '2019-04-08', + 'dateExecute' => '2019-04-08T00:00:00+00:00', 'secure' => 'bar', 'authLogin' => 'foo', ], $request->jsonSerialize()); @@ -212,7 +212,7 @@ public function test_anonymous_request() $this->assertSame([ 'version' => '1.0', 'currency' => 'EUR', - 'dateExecute' => '2019-04-08', + 'dateExecute' => '2019-04-08T00:00:00+00:00', ], $request->jsonSerialize()); } diff --git a/tests/Serialization/CalculationWithTariffListRequestTest.php b/tests/Serialization/CalculationWithTariffListRequestTest.php index cd80119..6c8e502 100644 --- a/tests/Serialization/CalculationWithTariffListRequestTest.php +++ b/tests/Serialization/CalculationWithTariffListRequestTest.php @@ -57,7 +57,7 @@ public function test_example() $this->assertEquals(\json_decode('{ "version":"1.0", - "dateExecute":"2019-04-01", + "dateExecute":"2019-04-01T00:00:00+00:00", "senderCityId":"44", "receiverCityId":"269", "currency":"RUB",