1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daaner\NovaPoshta; |
4
|
|
|
|
5
|
|
|
use Daaner\NovaPoshta\Contracts\NovaPoshtaInterface; |
6
|
|
|
use Illuminate\Support\Facades\Http; |
7
|
|
|
use Log; |
8
|
|
|
|
9
|
|
|
class NovaPoshta implements NovaPoshtaInterface |
10
|
|
|
{ |
11
|
|
|
protected $baseUri; |
12
|
|
|
protected $point; |
13
|
|
|
|
14
|
|
|
protected $api; |
15
|
|
|
protected $url; |
16
|
|
|
protected $dev; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* NovaPoshta constructor main settings. |
20
|
|
|
*/ |
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->baseUri = config('novaposhta.base_uri'); |
24
|
|
|
$this->point = config('novaposhta.point'); |
25
|
|
|
$this->dev = config('novaposhta.dev'); |
26
|
|
|
$this->getApi(); |
27
|
|
|
$this->url = $this->baseUri.$this->point; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getApi() |
34
|
|
|
{ |
35
|
|
|
if (! $this->api) { |
36
|
|
|
$this->api = config('novaposhta.api_key'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->api; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $api |
44
|
|
|
*/ |
45
|
|
|
public function setApi($api) |
46
|
|
|
{ |
47
|
|
|
$this->api = $api; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $model |
54
|
|
|
* @param string $calledMethod |
55
|
|
|
* @param array $methodProperties |
56
|
|
|
* @param bool $auth |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getResponse($model, $calledMethod, $methodProperties, $auth = true) |
60
|
|
|
{ |
61
|
|
|
$url = $this->url.'/'.$model.'/'.$calledMethod; |
62
|
|
|
$body = []; |
63
|
|
|
$info = ''; |
64
|
|
|
|
65
|
|
|
if ($auth) { |
66
|
|
|
$body['apiKey'] = $this->api; |
67
|
|
|
$body['modelName'] = $model; |
68
|
|
|
$body['calledMethod'] = $calledMethod; |
69
|
|
|
$body['methodProperties'] = $methodProperties; |
70
|
|
|
} else { |
71
|
|
|
$body['modelName'] = $model; |
72
|
|
|
$body['calledMethod'] = $calledMethod; |
73
|
|
|
$body['methodProperties'] = $methodProperties; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$response = Http::timeout(3) |
77
|
|
|
->retry(2, 200) |
78
|
|
|
->withHeaders([ |
79
|
|
|
'Accept' => 'application/json', |
80
|
|
|
'Content-Type' => 'application/json', |
81
|
|
|
]) |
82
|
|
|
->post($url, $body); |
83
|
|
|
|
84
|
|
|
if ($response->failed()) { |
85
|
|
|
return [ |
86
|
|
|
'success' => false, |
87
|
|
|
'result' => null, |
88
|
|
|
'info' => trans('novaposhta::novaposhta.error_data'), |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$answer = $response->json(); |
93
|
|
|
if ($auth === false && isset($answer[0])) { |
94
|
|
|
//костыль для НовойПочты. Спасибо Вам большое :) |
95
|
|
|
$answer = $answer[0]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (! isset($answer['success']) || ! isset($answer['data']) || empty($answer['data'])) { |
99
|
|
|
// что-то не так в ответе |
100
|
|
|
$info = trans('novaposhta::novaposhta.error_answer'); |
101
|
|
|
$success = false; |
102
|
|
|
$result = null; |
103
|
|
|
} else { |
104
|
|
|
$success = $answer['success']; |
105
|
|
|
$result = $answer['data']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// ошибки либо уведомления |
109
|
|
|
if (isset($answer['warnings']) && isset($answer['warnings'])) { |
110
|
|
|
$info = $answer['warnings']; |
111
|
|
|
|
112
|
|
|
if ($answer['errors']) { |
113
|
|
|
$info = $answer['errors']; |
114
|
|
|
if ($answer['errorCodes']) { |
115
|
|
|
$info = []; |
116
|
|
|
foreach ($answer['errorCodes'] as $key => $err) { |
117
|
|
|
$info['StatusCode'] = $err; |
118
|
|
|
$info['StatusLocale'] = __('novaposhta::novaposhta.statusCode.'.$err); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (! $info && isset($answer['info'])) { |
125
|
|
|
$info = $answer['info']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$return = [ |
129
|
|
|
'success' => $success, |
130
|
|
|
'result' => $result, |
131
|
|
|
'info' => $info, |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
if ($this->dev) { |
135
|
|
|
/** |
136
|
|
|
* Test and Dev |
137
|
|
|
*/ |
138
|
|
|
Log::debug('= = = = = = = = = = = = = = = = = = = ='); |
139
|
|
|
Log::debug($model.' / '.$calledMethod.' // apiKey: '.$auth); |
140
|
|
|
Log::debug('--------------------'); |
141
|
|
|
|
142
|
|
|
try { |
143
|
|
|
Log::notice(json_encode($methodProperties)); |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
Log::notice('method json_encode error'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$return['dev'] = $answer; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $return; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|