Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
smscua.php
1<?php
2
4
11
15
17
19{
20 public const ID = 'smscua';
21
22 private const JSON_API_URL = 'https://smsc.ua/sys/';
23
24 public static function isSupported()
25 {
26 return (
28 || Option::get('messageservice', 'smscua_enabled', 'N') === 'Y'
29 );
30 }
31
32 public function getId()
33 {
34 return static::ID;
35 }
36
37 public function getName()
38 {
39 return 'smsc.ua';
40 }
41
42 public function getShortName()
43 {
44 return 'smsc.ua';
45 }
46
47 public function getFromList()
48 {
49 $from = $this->getOption('from_list');
50
51 return is_array($from) ? $from : [];
52 }
53
54 public function isRegistered()
55 {
56 return (
57 $this->getOption('login') !== null
58 &&
59 $this->getOption('psw') !== null
60 );
61 }
62
63 public function register(array $fields)
64 {
65 $login = (string)$fields['login'];
66 $psw = (string)$fields['psw'];
67
68 $result = $this->sendApiRequest(
69 'balance',
70 [
71 'login' => $login,
72 'psw' => $psw,
73 ]
74 );
75
76 if ($result->isSuccess())
77 {
78 $this->setOption('login', $login);
79 $this->setOption('psw', $psw);
80 }
81
82 return $result;
83 }
84
90 public function getOwnerInfo()
91 {
92 return [
93 'login' => $this->getOption('login'),
94 ];
95 }
96
97 public function getExternalManageUrl()
98 {
99 return 'https://smsc.ua/login/';
100 }
101
102 public function sendMessage(array $messageFields)
103 {
104 if (!$this->canUse())
105 {
106 $result = new SendMessage();
107 $result->addError(new Error('Service is unavailable'));
108
109 return $result;
110 }
111
112 $message = [
113 'sender' => $messageFields['MESSAGE_FROM'],
114 'phones' => str_replace('+', '', $messageFields['MESSAGE_TO']),
115 'mes' => $this->prepareMessageBodyForSend($messageFields['MESSAGE_BODY']),
116 'charset' => 'utf-8'
117 ];
118
119 $result = new SendMessage();
120 $apiResult = $this->sendApiRequest('send', $message);
121 $result->setServiceRequest($apiResult->getHttpRequest());
122 $result->setServiceResponse($apiResult->getHttpResponse());
123
124 if (!$apiResult->isSuccess())
125 {
126 $result->addErrors($apiResult->getErrors());
127 }
128 else
129 {
130 $smsData = $apiResult->getData();
131 $smsId = $smsData['id'];
132
133 if (!$smsId)
134 {
135 $result->addError(new Error('Service error.'));
136 }
137 else
138 {
139 $result->setExternalId($smsId);
140 $result->setAccepted();
141 }
142 }
143
144 return $result;
145 }
146
147 public function getMessageStatus(array $messageFields)
148 {
149 $result = new MessageStatus();
150 $result->setId($messageFields['ID']);
151 $result->setExternalId($messageFields['EXTERNAL_ID']);
152
153 if (!$this->canUse())
154 {
155 $result->addError(new Error('Service is unavailable'));
156
157 return $result;
158 }
159
160 $apiResult = $this->sendApiRequest('status', [
161 'id' => $result->getExternalId(),
162 'phone' => str_replace('+', '', $messageFields['MESSAGE_TO']),
163 'charset' => 'utf-8',
164 ]);
165
166 if (!$apiResult->isSuccess())
167 {
168 $result->addErrors($apiResult->getErrors());
169 }
170 else
171 {
172 $smsData = $apiResult->getData();
173
174 if (!$smsData)
175 {
176 $result->addError(new Error('Service error.'));
177 }
178
179 $result->setStatusText($smsData['status']);
180 $result->setStatusCode(self::resolveStatus($smsData['status']));
181 }
182
183 return $result;
184 }
185
186 public static function resolveStatus($serviceStatus)
187 {
188
189 switch ((string)$serviceStatus)
190 {
191 case '-3':
192 case '-2':
193
194 return MessageService\MessageStatus::ERROR;
195 break;
196
197 case '-1':
198 case '0':
199
200 return MessageService\MessageStatus::QUEUED;
201 break;
202
203 case '1':
204
205 return MessageService\MessageStatus::DELIVERED;
206 break;
207
208 case '2':
209 case '4':
210
211 return MessageService\MessageStatus::READ;
212 break;
213
214 case '3':
215
216 return MessageService\MessageStatus::UNDELIVERED;
217 break;
218
219 default:
220 return MessageService\MessageStatus::FAILED;
221 break;
222 }
223
224 return parent::resolveStatus($serviceStatus);
225 }
226
227 public function sync()
228 {
229 if ($this->isRegistered())
230 {
231 $this->loadFromList();
232 }
233
234 return $this;
235 }
236
237 private function loadFromList()
238 {
239 $result = $this->sendApiRequest('senders', ['get' => 1]);
240
241 if ($result->isSuccess())
242 {
243 $from = [];
244 $resultData = $result->getData();
245 foreach ($resultData as $sender)
246 {
247 $from[] = [
248 'id' => $sender['sender'],
249 'name' => $sender['sender'],
250 ];
251 }
252
253 $this->setOption('from_list', $from);
254 }
255 }
256
257 private function sendApiRequest($path, array $params)
258 {
259 if (!isset($params['login']))
260 {
261 $params['login'] = $this->getOption('login');
262 }
263 if (!isset($params['psw']))
264 {
265 $params['psw'] = $this->getOption('psw');
266 }
267
268 $method = $path === 'balance' ? HttpClient::HTTP_GET : HttpClient::HTTP_POST;
269
270 return $this->sendHttpRequest($method, $path, $params);
271 }
272
273 private function sendHttpRequest($method, $path, array $params): Sender\Result\HttpRequestResult
274 {
275 $httpClient = new HttpClient([
276 "socketTimeout" => $this->socketTimeout,
277 "streamTimeout" => $this->streamTimeout,
278 "waitResponse" => true,
279 ]);
280 $httpClient->setCharset('UTF-8');
281 $httpClient->setHeader('User-Agent', 'Bitrix24');
282 $httpClient->setHeader('Content-Type', 'application/json');
283
284 $result = new Sender\Result\HttpRequestResult();
285 $answer = ['error' => 'Service error'];
286
287 $url = self::JSON_API_URL.$path.'.php';
288
289 $params['fmt'] = 3;
290
291 //if ($method === HttpClient::HTTP_GET)
292 {
293 $url .= '?'.http_build_query($params);
294 $params = null;
295 }
296
297 $result->setHttpRequest(new MessageService\DTO\Request([
298 'method' => $method,
299 'uri' => $url,
300 'headers' => method_exists($httpClient, 'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
301 'body' => $params,
302 ]));
303 if ($httpClient->query($method, $url, $params))
304 {
305 try
306 {
307 $answer = Json::decode($httpClient->getResult());
308 } catch (\Bitrix\Main\ArgumentException $e)
309 {
310 }
311 }
312 $result->setHttpResponse(new MessageService\DTO\Response([
313 'statusCode' => $httpClient->getStatus(),
314 'headers' => $httpClient->getHeaders()->toArray(),
315 'body' => $httpClient->getResult(),
316 'error' => Sender\Util::getHttpClientErrorString($httpClient)
317 ]));
318
319 if (isset($answer['error']))
320 {
321 $result->addError(new Error($answer['error'], $answer['error_code']));
322 }
323
324 $result->setData($answer);
325
326 return $result;
327 }
328}
static isModuleInstalled($moduleName)
getOption($optionName, $defaultValue=null)
prepareMessageBodyForSend(string $text)
Definition base.php:176
Providers Sender $sender
Definition base.php:13
sendMessage(array $messageFields)
Definition smscua.php:102
static resolveStatus($serviceStatus)
Definition smscua.php:186
getMessageStatus(array $messageFields)
Definition smscua.php:147
static getHttpClientErrorString(HttpClient $httpClient)
Definition util.php:24