21 use Sender\Traits\RussianProvider;
23 public const ID =
'smsru';
48 'embed_id' => $this->
getOption(
'embed_id')
50 $apiResult = $this->callExternalMethod(
'my/free', $params);
55 'available_today' => 0
58 if ($apiResult->isSuccess())
60 $balanceData = $apiResult->getData();
61 $balance[
'total_free'] = (int)$balanceData[
'total_free'];
62 $balance[
'used_today'] = (int)$balanceData[
'used_today'];
63 $balance[
'available_today'] = max(0, $balance[
'total_free'] - $balance[
'used_today']);
72 return is_array($from) ? $from : array();
77 return ($this->
getOption(
'is_confirmed') ===
true);
82 return ($this->
getOption(
'embed_id') !==
null);
85 public function register(array $fields)
87 $userPhone = \NormalizePhone($fields[
'user_phone']);
89 'user_phone' => $userPhone,
90 'user_firstname' => $fields[
'user_firstname'],
91 'user_lastname' => $fields[
'user_lastname'],
92 'user_email' => $fields[
'user_email'],
93 'embed_partner' => $this->getEmbedPartner(),
94 'embed_hash' => $this->getEmbedHash($userPhone)
97 $result = $this->callExternalMethod(
'embed/register', $params);
98 if ($result->isSuccess())
100 $data = $result->getData();
102 $this->
setOption(
'embed_id', $data[
'embed_id']);
103 $this->
setOption(
'user_phone', $userPhone);
104 if (!empty($params[
'user_firstname']))
106 $this->
setOption(
'user_firstname', $params[
'user_firstname']);
108 if (!empty($params[
'user_lastname']))
110 $this->
setOption(
'user_lastname', $params[
'user_lastname']);
112 if (!empty($params[
'user_email']))
114 $this->
setOption(
'user_email', $params[
'user_email']);
117 if (!empty($data[
'confirmed']))
137 'phone' => $this->
getOption(
'user_phone'),
138 'firstName' => $this->
getOption(
'user_firstname'),
139 'lastName' => $this->
getOption(
'user_lastname'),
140 'email' => $this->
getOption(
'user_email')
152 'embed_id' => $embedId,
153 'confirm' => $fields[
'confirm']
155 $result = $this->callExternalMethod(
'embed/confirm', $params);
157 if ($result->isSuccess())
160 $callBackResult = $this->callExternalMethod(
'callback/add', array(
161 'embed_id' => $embedId,
164 if ($callBackResult->isSuccess())
178 $result = $this->
register(array(
179 'user_phone' => $ownerInfo[
'phone'],
180 'user_firstname' => $ownerInfo[
'firstName'],
181 'user_lastname' => $ownerInfo[
'lastName'],
182 'user_email' => $ownerInfo[
'email'],
188 $result->addError(
new Error(
'Provider is not registered.'));
198 return 'https://sms.ru/?panel=login&action=login&embed_id='.$this->getOption(
'embed_id');
200 return 'https://sms.ru/?panel=login';
213 'to' => $messageFields[
'MESSAGE_TO'],
215 'embed_id' => $this->
getOption(
'embed_id')
220 $params[
'to'] = $this->
getOption(
'user_phone');
223 if ($messageFields[
'MESSAGE_FROM'])
225 $params[
'from'] = $messageFields[
'MESSAGE_FROM'];
229 $apiResult = $this->callExternalMethod(
'sms/send', $params);
230 $result->setServiceRequest($apiResult->getHttpRequest());
231 $result->setServiceResponse($apiResult->getHttpResponse());
233 $resultData = $apiResult->getData();
235 if (!$apiResult->isSuccess())
237 if ((
int)$resultData[
'status_code'] == 206)
240 $result->addError(
new Error($this->getErrorMessage($resultData[
'status_code'])));
244 $result->addErrors($apiResult->getErrors());
249 $smsData = current($resultData[
'sms']);
251 if (isset($smsData[
'sms_id']))
253 $result->setExternalId($smsData[
'sms_id']);
256 if ((
int)$smsData[
'status_code'] !== 100)
258 $result->addError(
new Error($this->getErrorMessage($smsData[
'status_code'])));
260 elseif ((
int)$smsData[
'status_code'] == 206)
263 $result->addError(
new Error($this->getErrorMessage($smsData[
'status_code'])));
267 $result->setAccepted();
277 $result->setId($messageFields[
'ID']);
278 $result->setExternalId($messageFields[
'EXTERNAL_ID']);
287 'sms_id' => $result->getExternalId(),
288 'embed_id' => $this->getOption(
'embed_id')
291 $apiResult = $this->callExternalMethod(
'sms/status', $params);
292 if (!$apiResult->isSuccess())
294 $result->addErrors($apiResult->getErrors());
298 $resultData = $apiResult->getData();
299 $smsData = current($resultData[
'sms']);
301 $result->setStatusCode($smsData[
'status_code']);
302 $result->setStatusText($smsData[
'status_text']);
304 if ((
int)$resultData[
'status_code'] !== 100)
306 $result->addError(
new Error($this->getErrorMessage($smsData[
'status_code'])));
315 $status = parent::resolveStatus($serviceStatus);
317 switch ((
int)$serviceStatus)
320 return MessageService\MessageStatus::ACCEPTED;
323 return MessageService\MessageStatus::SENDING;
326 return MessageService\MessageStatus::SENT;
329 return MessageService\MessageStatus::DELIVERED;
336 return MessageService\MessageStatus::UNDELIVERED;
339 return MessageService\MessageStatus::READ;
350 $this->loadFromList();
357 $url =
'https://sms.ru/'.$method;
360 "socketTimeout" => $this->socketTimeout,
361 "streamTimeout" => $this->streamTimeout,
362 "waitResponse" =>
true,
364 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
365 $httpClient->setCharset(
'UTF-8');
371 $params = \Bitrix\Main\Text\Encoding::convertEncoding($params, SITE_CHARSET,
'UTF-8');
378 $result->setHttpRequest(
new MessageService\DTO\
Request([
381 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
386 $answer = $this->parseExternalAnswer($httpClient->getResult());
389 $answerCode = isset($answer[
'status_code']) ? (int)$answer[
'status_code'] : 0;
391 if ($answerCode !== 100)
393 $result->addError(
new Error($this->getErrorMessage($answerCode, $answer)));
395 $result->setData($answer);
396 $result->setHttpResponse(
new MessageService\DTO\Response([
397 'statusCode' => $httpClient->getStatus(),
398 'headers' => $httpClient->getHeaders()->toArray(),
399 'body' => $httpClient->getResult(),
406 private function parseExternalAnswer($httpResult)
410 $answer = Json::decode($httpResult);
412 catch (\
Bitrix\Main\ArgumentException $e)
414 $data = explode(PHP_EOL, $httpResult);
415 $code = (int)array_shift($data);
417 $answer[
'status_code'] = $code;
418 $answer[
'status'] = $code === 100 ?
'OK' :
'ERROR';
421 if (!is_array($answer) && is_numeric($answer))
424 'status' => $answer === 100 ?
'OK' :
'ERROR',
425 'status_code' => $answer
432 private function getEmbedPartner()
437 private function getSecretKey()
439 return 'P46y811M84W3b4H18SmDpy9KG3pKG3Ok';
442 private function getEmbedHash($phoneNumber)
444 return md5($phoneNumber.$this->getSecretKey());
447 private function getErrorMessage($errorCode, $answer =
null)
449 $message =
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_ERROR_'.$errorCode);
450 if (!$message && $answer && !empty($answer[
'errors']))
452 $errorCode = $answer[
'errors'][0][
'status_code'];
453 $message =
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_ERROR_'.$errorCode);
456 $message = $answer[
'errors'][0][
'status_text'];
460 return $message ?:
Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_ERROR_OTHER');
463 private function loadFromList()
466 'embed_id' => $this->
getOption(
'embed_id')
468 $result = $this->callExternalMethod(
'my/senders', $params);
470 if ($result->isSuccess())
473 $resultData = $result->getData();
474 foreach ($resultData[
'senders'] as
$sender)
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)