1 | <?php |
||
2 | |||
3 | namespace Daaner\TurboSMS; |
||
4 | |||
5 | use Daaner\TurboSMS\Contracts\TurboSMSInterface; |
||
6 | use Daaner\TurboSMS\Traits\StartTimeAddition; |
||
7 | use Daaner\TurboSMS\Traits\ViberAddition; |
||
8 | use Illuminate\Support\Collection; |
||
9 | use Illuminate\Support\Facades\Http; |
||
10 | |||
11 | class TurboSMS implements TurboSMSInterface |
||
12 | { |
||
13 | use ViberAddition, StartTimeAddition; |
||
14 | |||
15 | protected $api; |
||
16 | protected $viberSender; |
||
17 | protected $smsSender; |
||
18 | protected $startTime; |
||
19 | protected $isTest; |
||
20 | |||
21 | protected $baseUri = 'https://api.turbosms.ua/'; |
||
22 | |||
23 | /** |
||
24 | * TurboSMS constructor main settings. |
||
25 | */ |
||
26 | public function __construct() |
||
27 | { |
||
28 | $this->getApi(); |
||
29 | $this->getViberSender(); |
||
30 | $this->getSMSSender(); |
||
31 | $this->isTest = config('turbosms.test_mode'); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return int|null |
||
36 | */ |
||
37 | public function getBalance(): ?int |
||
38 | { |
||
39 | $balance = null; |
||
40 | $module = 'user'; |
||
41 | $method = 'balance.json'; |
||
42 | |||
43 | /** Для проверки баланса снимаем режим DEV */ |
||
44 | $this->isTest = false; |
||
45 | |||
46 | $url = $this->baseUri.$module.'/'.$method; |
||
47 | $body = []; |
||
48 | |||
49 | $answers = $this->getResponse($url, $body); |
||
50 | |||
51 | if (isset($answers['result']['balance'])) { |
||
52 | $balance = $answers['result']['balance']; |
||
53 | } |
||
54 | |||
55 | return $balance; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return array |
||
60 | */ |
||
61 | public function getBalanceJson(): array |
||
62 | { |
||
63 | $module = 'user'; |
||
64 | $method = 'balance.json'; |
||
65 | |||
66 | /** Для проверки баланса снимаем режим DEV */ |
||
67 | $this->isTest = false; |
||
68 | |||
69 | $url = $this->baseUri.$module.'/'.$method; |
||
70 | $body = []; |
||
71 | |||
72 | return $this->getResponse($url, $body); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string|array $messageId |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getItemsStatus($messageId): array |
||
80 | { |
||
81 | $module = 'message'; |
||
82 | $method = 'status.json'; |
||
83 | |||
84 | $messages = collect($messageId)->values()->all(); |
||
85 | |||
86 | $url = $this->baseUri.$module.'/'.$method; |
||
87 | $body = [ |
||
88 | 'messages' => $messages, |
||
89 | ]; |
||
90 | |||
91 | return $this->getResponse($url, $body); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string|array $recipients |
||
96 | * @param string $text |
||
97 | * @param string|null $type |
||
98 | * @return array |
||
99 | */ |
||
100 | public function sendMessages($recipients, string $text, string $type = null): array |
||
101 | { |
||
102 | $module = 'message'; |
||
103 | $method = 'send.json'; |
||
104 | |||
105 | if (! $text) { |
||
106 | return [ |
||
107 | 'success' => false, |
||
108 | 'result' => null, |
||
109 | 'info' => __('turbosms::turbosms.empty_text'), |
||
110 | ]; |
||
111 | } |
||
112 | |||
113 | $phone = collect($recipients); |
||
114 | $phones = $this->phonesTrim($phone); |
||
115 | $phones = $phones->values()->all(); |
||
116 | |||
117 | $url = $this->baseUri.$module.'/'.$method; |
||
118 | $body = [ |
||
119 | 'recipients' => $phones, |
||
120 | ]; |
||
121 | |||
122 | //SMS |
||
123 | if ($type == 'sms' || ! $type) { |
||
124 | $body = $this->bodySMS($body, $text); |
||
125 | } |
||
126 | //VIBER |
||
127 | if ($type == 'viber') { |
||
128 | $body = $this->bodyViber($body, $text); |
||
129 | } |
||
130 | //Гибридная доставка |
||
131 | if ($type == 'both') { |
||
132 | $body = $this->bodySMS($body, $text); |
||
133 | $body = $this->bodyViber($body, $text); |
||
134 | } |
||
135 | |||
136 | //Доставка в определенное время |
||
137 | if ($this->startTime) { |
||
138 | $body['start_time'] = $this->startTime; |
||
139 | } |
||
140 | |||
141 | return $this->getResponse($url, $body); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string $url |
||
146 | * @param array $body |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getResponse(string $url, array $body): array |
||
150 | { |
||
151 | if ($this->isTest) { |
||
152 | return [ |
||
153 | 'success' => false, |
||
154 | 'result' => [ |
||
155 | 'url' => $url, |
||
156 | 'body' => $body, |
||
157 | ], |
||
158 | 'info' => __('turbosms::turbosms.test_mode'), |
||
159 | ]; |
||
160 | } |
||
161 | |||
162 | $response = Http::timeout(config('turbosms.http_response_timeout', 3)) |
||
163 | ->retry(config('turbosms.http_retry_max_time', 2), config('turbosms.http_retry_delay', 200)) |
||
164 | ->withHeaders([ |
||
165 | 'Accept' => 'application/json', |
||
166 | 'Authorization' => 'Bearer '.$this->api, |
||
167 | 'Content-Type' => 'application/json', |
||
168 | ])->post($url, $body); |
||
169 | |||
170 | if ($response->failed()) { |
||
171 | return [ |
||
172 | 'success' => false, |
||
173 | 'result' => null, |
||
174 | 'info' => __('turbosms::turbosms.error_data'), |
||
175 | ]; |
||
176 | } |
||
177 | |||
178 | $answer = $response->json(); |
||
179 | if (! $answer || ! isset($answer['response_result']) || ! $answer['response_result']) { |
||
180 | $error = __('turbosms::turbosms.response_status.FAILED_CONVERT_RESULT2JSON'); |
||
181 | |||
182 | if (isset($answer['response_status']) && $answer['response_status']) { |
||
183 | $error = __('turbosms::turbosms.response_status.'.$answer['response_status']); |
||
184 | } |
||
185 | |||
186 | return [ |
||
187 | 'success' => false, |
||
188 | 'result' => null, |
||
189 | 'info' => $error, |
||
190 | ]; |
||
191 | } |
||
192 | |||
193 | $info = __('turbosms::turbosms.response_status.'.$answer['response_status']); |
||
194 | |||
195 | return [ |
||
196 | 'success' => true, |
||
197 | 'result' => $answer['response_result'], |
||
198 | 'info' => $info, |
||
199 | ]; |
||
200 | } |
||
201 | |||
202 | // =================== SUPPORT =================== |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getApi(): string |
||
208 | { |
||
209 | if (is_null($this->api)) { |
||
210 | $this->api = (string) config('turbosms.api_key'); |
||
211 | } |
||
212 | |||
213 | return $this->api; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param string $api |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function setApi(string $api): self |
||
221 | { |
||
222 | $this->api = $api; |
||
223 | |||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getViberSender(): string |
||
231 | { |
||
232 | if (is_null($this->viberSender)) { |
||
233 | $this->viberSender = (string) config('turbosms.viber_sender'); |
||
234 | } |
||
235 | |||
236 | return $this->viberSender; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param string $viberSender |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function setViberSender(string $viberSender): self |
||
244 | { |
||
245 | $this->viberSender = $viberSender; |
||
246 | |||
247 | return $this; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getSMSSender(): ?string |
||
254 | { |
||
255 | if (is_null($this->smsSender)) { |
||
256 | $this->smsSender = config('turbosms.sms_sender'); |
||
257 | } |
||
258 | |||
259 | return $this->smsSender; |
||
0 ignored issues
–
show
|
|||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @param string $smsSender |
||
264 | * @return $this |
||
265 | */ |
||
266 | public function setSMSSender(string $smsSender): self |
||
267 | { |
||
268 | $this->smsSender = $smsSender; |
||
269 | |||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Убираем у телефонов пробелы, скобки, минусы и плюсы. |
||
275 | * |
||
276 | * @param Collection $phones |
||
277 | * @return Collection $phones |
||
278 | */ |
||
279 | public function phonesTrim(Collection $phones): Collection |
||
280 | { |
||
281 | $phones->transform(function ($item) { |
||
282 | return preg_replace('/[^0-9]/', '', $item); |
||
283 | }); |
||
284 | |||
285 | return $phones; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Формируем $body для SMS. |
||
290 | * |
||
291 | * @param array $body |
||
292 | * @param string $text |
||
293 | * @return array $body |
||
294 | */ |
||
295 | public function bodySMS(array $body, string $text): array |
||
296 | { |
||
297 | $body['sms'] = [ |
||
298 | 'sender' => $this->smsSender, |
||
299 | 'text' => $text, |
||
300 | ]; |
||
301 | |||
302 | return $body; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Формируем $body для Viber. |
||
307 | * |
||
308 | * @param array $body |
||
309 | * @param string $text |
||
310 | * @return array $body |
||
311 | */ |
||
312 | public function bodyViber(array $body, string $text): array |
||
313 | { |
||
314 | $msg = $text; |
||
315 | if ($this->viberReplaceText) { |
||
316 | $msg = $this->viberReplaceText; |
||
317 | } |
||
318 | |||
319 | $body['viber'] = [ |
||
320 | 'sender' => $this->viberSender, |
||
321 | 'text' => $msg, |
||
322 | ]; |
||
323 | |||
324 | if ($this->ttl) { |
||
325 | $body['viber']['ttl'] = $this->ttl; |
||
326 | } |
||
327 | if ($this->imageUrl) { |
||
328 | $body['viber']['image_url'] = $this->imageUrl; |
||
329 | } |
||
330 | if ($this->caption) { |
||
331 | $body['viber']['caption'] = $this->caption; |
||
332 | } |
||
333 | if ($this->action) { |
||
334 | $body['viber']['action'] = $this->action; |
||
335 | } |
||
336 | if ($this->countClicks) { |
||
337 | $body['viber']['count_clicks'] = $this->countClicks; |
||
338 | } |
||
339 | if ($this->isTransactional) { |
||
340 | $body['viber']['is_transactional'] = $this->isTransactional; |
||
341 | } |
||
342 | |||
343 | return $body; |
||
344 | } |
||
345 | } |
||
346 |