20 public const ID =
'smscua';
22 private const JSON_API_URL =
'https://smsc.ua/sys/';
28 || Option::get(
'messageservice',
'smscua_enabled',
'N') ===
'Y'
51 return is_array($from) ? $from : [];
63 public function register(array $fields)
65 $login = (string)$fields[
'login'];
66 $psw = (string)$fields[
'psw'];
68 $result = $this->sendApiRequest(
76 if ($result->isSuccess())
99 return 'https://smsc.ua/login/';
107 $result->addError(
new Error(
'Service is unavailable'));
113 'sender' => $messageFields[
'MESSAGE_FROM'],
114 'phones' => str_replace(
'+',
'', $messageFields[
'MESSAGE_TO']),
120 $apiResult = $this->sendApiRequest(
'send', $message);
121 $result->setServiceRequest($apiResult->getHttpRequest());
122 $result->setServiceResponse($apiResult->getHttpResponse());
124 if (!$apiResult->isSuccess())
126 $result->addErrors($apiResult->getErrors());
130 $smsData = $apiResult->getData();
131 $smsId = $smsData[
'id'];
135 $result->addError(
new Error(
'Service error.'));
139 $result->setExternalId($smsId);
140 $result->setAccepted();
150 $result->setId($messageFields[
'ID']);
151 $result->setExternalId($messageFields[
'EXTERNAL_ID']);
155 $result->addError(
new Error(
'Service is unavailable'));
160 $apiResult = $this->sendApiRequest(
'status', [
161 'id' => $result->getExternalId(),
162 'phone' => str_replace(
'+',
'', $messageFields[
'MESSAGE_TO']),
163 'charset' =>
'utf-8',
166 if (!$apiResult->isSuccess())
168 $result->addErrors($apiResult->getErrors());
172 $smsData = $apiResult->getData();
176 $result->addError(
new Error(
'Service error.'));
179 $result->setStatusText($smsData[
'status']);
180 $result->setStatusCode(self::resolveStatus($smsData[
'status']));
189 switch ((
string)$serviceStatus)
194 return MessageService\MessageStatus::ERROR;
200 return MessageService\MessageStatus::QUEUED;
205 return MessageService\MessageStatus::DELIVERED;
211 return MessageService\MessageStatus::READ;
216 return MessageService\MessageStatus::UNDELIVERED;
220 return MessageService\MessageStatus::FAILED;
224 return parent::resolveStatus($serviceStatus);
231 $this->loadFromList();
237 private function loadFromList()
239 $result = $this->sendApiRequest(
'senders', [
'get' => 1]);
241 if ($result->isSuccess())
244 $resultData = $result->getData();
245 foreach ($resultData as
$sender)
257 private function sendApiRequest($path, array $params)
259 if (!isset($params[
'login']))
261 $params[
'login'] = $this->
getOption(
'login');
263 if (!isset($params[
'psw']))
265 $params[
'psw'] = $this->
getOption(
'psw');
270 return $this->sendHttpRequest($method, $path, $params);
273 private function sendHttpRequest($method, $path, array $params): Sender\Result\HttpRequestResult
275 $httpClient =
new HttpClient([
276 "socketTimeout" => $this->socketTimeout,
277 "streamTimeout" => $this->streamTimeout,
278 "waitResponse" =>
true,
280 $httpClient->setCharset(
'UTF-8');
281 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
282 $httpClient->setHeader(
'Content-Type',
'application/json');
284 $result =
new Sender\Result\HttpRequestResult();
285 $answer = [
'error' =>
'Service error'];
287 $url = self::JSON_API_URL.$path.
'.php';
293 $url .=
'?'.http_build_query($params);
297 $result->setHttpRequest(
new MessageService\DTO\Request([
300 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
303 if ($httpClient->query($method, $url, $params))
307 $answer = Json::decode($httpClient->getResult());
308 }
catch (\
Bitrix\Main\ArgumentException $e)
312 $result->setHttpResponse(
new MessageService\DTO\Response([
313 'statusCode' => $httpClient->getStatus(),
314 'headers' => $httpClient->getHeaders()->toArray(),
315 'body' => $httpClient->getResult(),
319 if (isset($answer[
'error']))
321 $result->addError(
new Error($answer[
'error'], $answer[
'error_code']));
324 $result->setData($answer);
static isModuleInstalled($moduleName)