1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace alekciy\ofd\providers\yandex; |
4
|
|
|
|
5
|
|
|
use alekciy\ofd\Json; |
6
|
|
|
use alekciy\ofd\Request; |
7
|
|
|
use Exception; |
8
|
|
|
use GuzzleHttp\Client as httpClient; |
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
class Client |
13
|
|
|
{ |
14
|
|
|
/** @var httpClient HTTP клиент для низкоуровневой работы с API */ |
15
|
|
|
protected $httpClient = null; |
16
|
|
|
|
17
|
|
|
/** @var Credentials Реквизиты доступа */ |
18
|
|
|
protected $credentials = null; |
19
|
|
|
|
20
|
|
|
/** @var string Версия клиента */ |
21
|
|
|
private $version = '0.1.0'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Credentials $credentials Реквизиты доступа. |
25
|
|
|
* @param array $clientConfig Дополнительную настройки для Guzzle клиента. |
26
|
|
|
* @throws Exception |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Credentials $credentials, array $clientConfig = []) |
29
|
|
|
{ |
30
|
|
|
$this->credentials = $credentials; |
31
|
|
|
|
32
|
|
|
$httpClientConfig = [ |
33
|
|
|
'base_uri' => 'https://' . $credentials->domain, |
34
|
|
|
'defaults' => [ |
35
|
|
|
'headers' => [ |
36
|
|
|
'Content-Type' => 'application/json', |
37
|
|
|
'Accept' => 'application/json', |
38
|
|
|
'X-Yandex-Key' => $credentials->authenticationKey, |
39
|
|
|
'X-OFD-Key' => $credentials->authorizationKey, |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
$this->httpClient = new httpClient(array_merge($clientConfig, $httpClientConfig)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Отправить запрос. |
48
|
|
|
* |
49
|
|
|
* @param Request $endpoint |
50
|
|
|
* @return ResponseInterface |
51
|
|
|
* @throws GuzzleException |
52
|
|
|
* @throws Exception |
53
|
|
|
*/ |
54
|
|
|
protected function sendRequest(Request $endpoint): ResponseInterface |
55
|
|
|
{ |
56
|
|
|
return $this->httpClient->request( |
57
|
|
|
$endpoint->method, |
58
|
|
|
$endpoint->getPath(), |
59
|
|
|
[ |
60
|
|
|
'debug' => $endpoint->debug, |
61
|
|
|
'exceptions' => false, |
62
|
|
|
'headers' => [ |
63
|
|
|
'Accept' => 'application/json', |
64
|
|
|
'User-Agent' => 'PHP-OFD-SDK/' . $this->version, |
65
|
|
|
'X-Yandex-Key' => $this->credentials->authenticationKey, |
66
|
|
|
'X-OFD-Key' => $this->credentials->authorizationKey, |
67
|
|
|
], |
68
|
|
|
'query' => $endpoint->getQuery(), |
69
|
|
|
'json' => $endpoint->getBody(), |
70
|
|
|
] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Шлет запрос и обрабатывает результаты. |
76
|
|
|
* |
77
|
|
|
* @param Request $endpoint |
78
|
|
|
* @return array |
79
|
|
|
* @throws GuzzleException |
80
|
|
|
* @throws Exception |
81
|
|
|
* @see https://lk-ofd.taxcom.ru/ApiHelp/index.html?3___.htm Обработка ошибок |
82
|
|
|
*/ |
83
|
|
|
public function request($endpoint): array |
84
|
|
|
{ |
85
|
|
|
$response = $this->sendRequest($endpoint); |
86
|
|
|
$body = Json::decode($response->getBody()->getContents(), true); |
87
|
|
|
$errorCode = $body['code'] ?? 0; |
88
|
|
|
|
89
|
|
|
if (!empty($errorCode)) { |
90
|
|
|
$msg = $body['message'] ?? $body['description']; |
91
|
|
|
throw new Exception(sprintf('При запросе адреса %s возникла ошибка (status=%d): %s', |
92
|
|
|
$endpoint->getPath(), |
93
|
|
|
$response->getStatusCode(), |
94
|
|
|
$msg), |
95
|
|
|
$errorCode |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
return $body; |
99
|
|
|
} |
100
|
|
|
} |