Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Commit

Permalink
Исправление алгоритма генерации secretKey для многомерных массивов
Browse files Browse the repository at this point in the history
Signed-off-by: Vitaly Baev <[email protected]>
  • Loading branch information
Vitaly Baev committed Nov 21, 2016
1 parent 002c7e8 commit 5ad1aff
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Vitalybaev\YandexDelivery;

/**
*
* Библиотека для выполнения запросов к API Яндекс.Доставки.
*/
class Client
{
Expand Down Expand Up @@ -65,7 +65,7 @@ function __construct($clientId, $senderId, $apiVersion = '1.0', $methodKeys = []
}

/**
* @return
* @return string
*/
public function getSenderId()
{
Expand All @@ -84,7 +84,7 @@ public function setSenderId($senderId)
}

/**
* @return
* @return string
*/
public function getClientId()
{
Expand All @@ -108,6 +108,8 @@ public function setClientId($clientId)
* @param string $method
* @param string $parameters
* @return array
* @throws Exception\InvalidJsonException
* @throws Exception\MethodKeysNotExistsException
*/
public function call($method, $parameters)
{
Expand All @@ -121,16 +123,8 @@ public function call($method, $parameters)
'sender_id' => $this->senderId,
]);

ksort($httpParameters);

// Получаем secret_key
$secretKeyBase = '';
foreach ($httpParameters as $value) {
$secretKeyBase .= $value;
}
$secretKeyBase .= $this->methodKeys[$method];

$httpParameters['secret_key'] = md5($secretKeyBase);
$secretKey = md5($this->getPostValues($httpParameters) . $this->methodKeys[$method]);
$httpParameters['secret_key'] = $secretKey;

// Выполняем запрос
$httpResponse = $this->httpClient->request('POST', "https://delivery.yandex.ru/api/$this->apiVersion/$method", [
Expand All @@ -146,4 +140,15 @@ public function call($method, $parameters)

return $json;
}

private function getPostValues($data)
{
if (!is_array($data)) {
return $data;
}
ksort($data);
return join('', array_map(function($k) {
return $this->getPostValues($k);
}, $data));
}
}

0 comments on commit 5ad1aff

Please sign in to comment.