20 use Sender\Traits\RussianProvider;
22 public const ID =
'smsru';
31 return Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_NAME');
47 'embed_id' => $this->
getOption(
'embed_id')
49 $apiResult = $this->callExternalMethod(
'my/free',
$params);
54 'available_today' => 0
57 if ($apiResult->isSuccess())
59 $balanceData = $apiResult->getData();
60 $balance[
'total_free'] = (int)$balanceData[
'total_free'];
61 $balance[
'used_today'] = (int)$balanceData[
'used_today'];
62 $balance[
'available_today'] = max(0, $balance[
'total_free'] - $balance[
'used_today']);
71 return is_array($from) ? $from :
array();
76 return ($this->
getOption(
'is_confirmed') ===
true);
81 return ($this->
getOption(
'embed_id') !==
null);
88 'user_phone' => $userPhone,
89 'user_firstname' =>
$fields[
'user_firstname'],
90 'user_lastname' =>
$fields[
'user_lastname'],
91 'user_email' =>
$fields[
'user_email'],
92 'embed_partner' => $this->getEmbedPartner(),
93 'embed_hash' => $this->getEmbedHash($userPhone)
102 $this->
setOption(
'user_phone', $userPhone);
103 if (!empty(
$params[
'user_firstname']))
107 if (!empty(
$params[
'user_lastname']))
111 if (!empty(
$params[
'user_email']))
116 if (!empty(
$data[
'confirmed']))
136 'phone' => $this->
getOption(
'user_phone'),
137 'firstName' => $this->
getOption(
'user_firstname'),
138 'lastName' => $this->
getOption(
'user_lastname'),
139 'email' => $this->
getOption(
'user_email')
151 'embed_id' => $embedId,
152 'confirm' =>
$fields[
'confirm']
159 $callBackResult = $this->callExternalMethod(
'callback/add',
array(
160 'embed_id' => $embedId,
163 if ($callBackResult->isSuccess())
178 'user_phone' => $ownerInfo[
'phone'],
179 'user_firstname' => $ownerInfo[
'firstName'],
180 'user_lastname' => $ownerInfo[
'lastName'],
181 'user_email' => $ownerInfo[
'email'],
187 $result->addError(
new Error(
'Provider is not registered.'));
197 return 'https://sms.ru/?panel=login&action=login&embed_id='.$this->getOption(
'embed_id');
199 return 'https://sms.ru/?panel=login';
207 $result->addError(
new Error(Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_CAN_USE_ERROR')));
214 'embed_id' => $this->
getOption(
'embed_id')
222 if ($messageFields[
'MESSAGE_FROM'])
228 $apiResult = $this->callExternalMethod(
'sms/send',
$params);
229 $result->setServiceRequest($apiResult->getHttpRequest());
230 $result->setServiceResponse($apiResult->getHttpResponse());
232 $resultData = $apiResult->getData();
234 if (!$apiResult->isSuccess())
236 if ((
int)$resultData[
'status_code'] == 206)
239 $result->addError(
new Error($this->getErrorMessage($resultData[
'status_code'])));
243 $result->addErrors($apiResult->getErrors());
248 $smsData = current($resultData[
'sms']);
250 if (isset($smsData[
'sms_id']))
252 $result->setExternalId($smsData[
'sms_id']);
255 if ((
int)$smsData[
'status_code'] !== 100)
257 $result->addError(
new Error($this->getErrorMessage($smsData[
'status_code'])));
259 elseif ((
int)$smsData[
'status_code'] == 206)
262 $result->addError(
new Error($this->getErrorMessage($smsData[
'status_code'])));
281 $result->addError(
new Error(Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_CAN_USE_ERROR')));
286 'sms_id' =>
$result->getExternalId(),
287 'embed_id' => $this->getOption(
'embed_id')
290 $apiResult = $this->callExternalMethod(
'sms/status',
$params);
291 if (!$apiResult->isSuccess())
293 $result->addErrors($apiResult->getErrors());
297 $resultData = $apiResult->getData();
298 $smsData = current($resultData[
'sms']);
300 $result->setStatusCode($smsData[
'status_code']);
301 $result->setStatusText($smsData[
'status_text']);
303 if ((
int)$resultData[
'status_code'] !== 100)
305 $result->addError(
new Error($this->getErrorMessage($smsData[
'status_code'])));
314 $status = parent::resolveStatus($serviceStatus);
316 switch ((
int)$serviceStatus)
349 $this->loadFromList();
356 $url =
'https://sms.ru/'.$method;
359 "socketTimeout" => $this->socketTimeout,
360 "streamTimeout" => $this->streamTimeout,
361 "waitResponse" =>
true,
363 $httpClient->setHeader(
'User-Agent',
'Bitrix24');
364 $httpClient->setCharset(
'UTF-8');
372 'method' => HttpClient::HTTP_POST,
374 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
377 if ($httpClient->query(HttpClient::HTTP_POST,
$url,
$params) && $httpClient->getStatus() ==
'200')
379 $answer = $this->parseExternalAnswer($httpClient->getResult());
382 $answerCode = isset($answer[
'status_code']) ? (int)$answer[
'status_code'] : 0;
384 if ($answerCode !== 100)
386 $result->addError(
new Error($this->getErrorMessage($answerCode, $answer)));
390 'statusCode' => $httpClient->getStatus(),
391 'headers' => $httpClient->getHeaders()->toArray(),
392 'body' => $httpClient->getResult(),
399 private function parseExternalAnswer($httpResult)
403 $answer = Json::decode($httpResult);
405 catch (\
Bitrix\Main\ArgumentException $e)
407 $data = explode(PHP_EOL, $httpResult);
410 $answer[
'status_code'] =
$code;
411 $answer[
'status'] =
$code === 100 ?
'OK' :
'ERROR';
414 if (!is_array($answer) && is_numeric($answer))
417 'status' => $answer === 100 ?
'OK' :
'ERROR',
418 'status_code' => $answer
425 private function getEmbedPartner()
430 private function getSecretKey()
432 return 'P46y811M84W3b4H18SmDpy9KG3pKG3Ok';
435 private function getEmbedHash($phoneNumber)
437 return md5($phoneNumber.$this->getSecretKey());
440 private function getErrorMessage($errorCode, $answer =
null)
442 $message = Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_ERROR_'.$errorCode);
443 if (!
$message && $answer && !empty($answer[
'errors']))
445 $errorCode = $answer[
'errors'][0][
'status_code'];
446 $message = Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_ERROR_'.$errorCode);
449 $message = $answer[
'errors'][0][
'status_text'];
453 return $message ?: Loc::getMessage(
'MESSAGESERVICE_SENDER_SMS_SMSRU_ERROR_OTHER');
456 private function loadFromList()
459 'embed_id' => $this->
getOption(
'embed_id')
466 $resultData =
$result->getData();
467 foreach ($resultData[
'senders'] as
$sender)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)