|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Daaner\NovaPoshta; |
|
4
|
|
|
|
|
5
|
|
|
use Daaner\NovaPoshta\Contracts\NovaPoshtaInterface; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Illuminate\Support\Facades\Http; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
|
|
10
|
|
|
class NovaPoshta implements NovaPoshtaInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected $baseUri; |
|
13
|
|
|
protected $point; |
|
14
|
|
|
|
|
15
|
|
|
protected $api; |
|
16
|
|
|
protected $url; |
|
17
|
|
|
protected $dev; |
|
18
|
|
|
|
|
19
|
|
|
protected $return; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* NovaPoshta constructor main settings. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->baseUri = config('novaposhta.base_uri'); |
|
28
|
|
|
$this->point = config('novaposhta.point'); |
|
29
|
|
|
$this->dev = config('novaposhta.dev'); |
|
30
|
|
|
$this->getApi(); |
|
31
|
|
|
$this->url = $this->baseUri.$this->point; |
|
32
|
|
|
|
|
33
|
|
|
$this->return = [ |
|
34
|
|
|
'success' => false, |
|
35
|
|
|
'result' => null, |
|
36
|
|
|
'info' => [], |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getApi(): string |
|
44
|
|
|
{ |
|
45
|
|
|
if (! $this->api) { |
|
46
|
|
|
$this->api = config('novaposhta.api_key'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->api; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Устанавливаем API токен отличный от значения в конфиге. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $api API токен |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setApi(string $api): self |
|
59
|
|
|
{ |
|
60
|
|
|
$this->api = $api; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $model Модель Новой Почты |
|
67
|
|
|
* @param string $calledMethod Метод модели |
|
68
|
|
|
* @param array|null $methodProperties Тело запроса |
|
69
|
|
|
* @param bool|null $auth Использовать ли аутентификацию токеном или нет |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getResponse( |
|
73
|
|
|
string $model, |
|
74
|
|
|
string $calledMethod, |
|
75
|
|
|
?array $methodProperties, |
|
76
|
|
|
?bool $auth = true |
|
77
|
|
|
): array { |
|
78
|
|
|
$url = $this->url.'/'.$model.'/'.$calledMethod; |
|
79
|
|
|
|
|
80
|
|
|
$body= []; |
|
81
|
|
|
$body['modelName'] = $model; |
|
82
|
|
|
$body['calledMethod'] = $calledMethod; |
|
83
|
|
|
$body['methodProperties'] = $methodProperties; |
|
84
|
|
|
|
|
85
|
|
|
if ($auth) { |
|
86
|
|
|
$body['apiKey'] = $this->api; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$response = Http::timeout(config('novaposhta.http_response_timeout', 3)) |
|
90
|
|
|
->retry( |
|
91
|
|
|
config('novaposhta.http_retry_max_time', 2), |
|
92
|
|
|
config('novaposhta.http_retry_delay', 200) |
|
93
|
|
|
) |
|
94
|
|
|
->withHeaders([ |
|
95
|
|
|
'Accept' => 'application/json', |
|
96
|
|
|
'Content-Type' => 'application/json', |
|
97
|
|
|
]) |
|
98
|
|
|
->post($url, $body); |
|
99
|
|
|
|
|
100
|
|
|
if ($response->failed() || $response->json() === null) { |
|
101
|
|
|
|
|
102
|
|
|
$this->return['info']['error'] = trans('novaposhta::novaposhta.error_data'); |
|
103
|
|
|
|
|
104
|
|
|
if ($this->dev) { |
|
105
|
|
|
$this->development($model, $calledMethod, $auth, $methodProperties, ''); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$answer = $response->json(); |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Development |
|
115
|
|
|
*/ |
|
116
|
|
|
if ($this->dev) { |
|
117
|
|
|
$this->development($model, $calledMethod, $auth, $methodProperties, $answer); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// TODO Возможно, исправлено |
|
121
|
|
|
if ($auth === false && isset($answer[0])) { |
|
122
|
|
|
/** |
|
123
|
|
|
* Костыль для Новой Почты. |
|
124
|
|
|
* Спасибо Вам большое, что нормально не выдаете ответ :). |
|
125
|
|
|
*/ |
|
126
|
|
|
$answer = $answer[0]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (isset($answer['success'])) { |
|
130
|
|
|
$this->return['success'] = $answer['success']; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (isset($answer['data'])) { |
|
134
|
|
|
$this->return['result'] = $answer['data']; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Формирование info |
|
139
|
|
|
*/ |
|
140
|
|
|
$this->return['info'] = $this->addInfo($answer); |
|
141
|
|
|
|
|
142
|
|
|
return $this->return; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Формирование информации. |
|
147
|
|
|
* Ошибки, уведомления. |
|
148
|
|
|
* |
|
149
|
|
|
* @param mixed $answer Ответ от НП |
|
150
|
|
|
*/ |
|
151
|
|
|
public function addInfo($answer): array |
|
152
|
|
|
{ |
|
153
|
|
|
$info = []; |
|
154
|
|
|
|
|
155
|
|
|
if (isset($answer['warnings']) && $answer['warnings']) { |
|
156
|
|
|
$info['warnings'] = $answer['warnings']; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if (isset($answer['errors']) && $answer['errors']) { |
|
160
|
|
|
$info['errors'] = $answer['errors']; |
|
161
|
|
|
if ($answer['errorCodes']) { |
|
162
|
|
|
foreach ($answer['errorCodes'] as $err) { |
|
163
|
|
|
$info['StatusCode'] = $err; |
|
164
|
|
|
} |
|
165
|
|
|
if (isset($answer['errorCodes'][0])) { |
|
166
|
|
|
$info['StatusLocale'] = __('novaposhta::novaposhta.statusCode.'.$answer['errorCodes'][0]); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (empty($info) && isset($answer['info']) && $answer['info']) { |
|
172
|
|
|
$info['info'] = $answer['info']; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $info; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Логирование запроса. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $model Модель |
|
183
|
|
|
* @param string $calledMethod Метод |
|
184
|
|
|
* @param bool $auth Аутентификация |
|
185
|
|
|
* @param mixed $methodProperties Тело запроса |
|
186
|
|
|
* @param mixed $answer Ответ |
|
187
|
|
|
* @return void |
|
188
|
|
|
*/ |
|
189
|
|
|
public function development( |
|
190
|
|
|
string $model, |
|
191
|
|
|
string $calledMethod, |
|
192
|
|
|
bool $auth, |
|
193
|
|
|
$methodProperties, |
|
194
|
|
|
$answer |
|
195
|
|
|
): void |
|
196
|
|
|
{ |
|
197
|
|
|
Log::debug('= = = = = = = = = = = = = = = = = = = ='); |
|
198
|
|
|
Log::debug($model.' / '.$calledMethod.' // apiKey: '. (int) $auth); |
|
199
|
|
|
Log::debug('--------------------'); |
|
200
|
|
|
|
|
201
|
|
|
if (! empty($methodProperties)) { |
|
202
|
|
|
try { |
|
203
|
|
|
Log::notice(json_encode($methodProperties)); |
|
204
|
|
|
} catch (Exception $e) { |
|
205
|
|
|
Log::notice('method json_encode error'); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$this->return['dev'] = $answer; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|