Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Use centralized date format for requests' signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Oct 15, 2019
1 parent 4038f01 commit d11a80b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/CdekClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Requests/Templates/CalculationAuthorizedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

namespace CdekSDK\Requests\Templates;

use CdekSDK\CdekClient;
use CdekSDK\Common\AdditionalService;
use CdekSDK\Contracts\DateAware;
use CdekSDK\Contracts\JsonRequest;
Expand Down Expand Up @@ -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 === '') {
Expand All @@ -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),
]);
}
}
6 changes: 3 additions & 3 deletions tests/Serialization/CalculationRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d11a80b

Please sign in to comment.