You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.2 KiB
132 lines
3.2 KiB
<?php
|
|
|
|
|
|
namespace App\Channels;
|
|
|
|
use App\Channels\Messages\SmsMessage;
|
|
use Illuminate\Notifications\Notification;
|
|
use GuzzleHttp\Client as HttpClient;
|
|
|
|
class SmsChannel
|
|
{
|
|
/**
|
|
* The API URL for sms.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $sms_url;
|
|
|
|
/**
|
|
* The api key for sms inside sms.ir panel.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $api_key;
|
|
|
|
/**
|
|
* The secret key for sms inside sms.ir panel.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $secret_key;
|
|
|
|
/**
|
|
* The HTTP client instance.
|
|
*
|
|
* @var \GuzzleHttp\Client
|
|
*/
|
|
protected $http;
|
|
|
|
/**
|
|
* Create a new Socket channel instance.
|
|
*
|
|
* @param \GuzzleHttp\Client $http
|
|
* @return void
|
|
*/
|
|
public function __construct(HttpClient $http, string $sms_url, string $api_key, string $secret_key)
|
|
{
|
|
$this->http = $http;
|
|
$this->sms_url = $sms_url;
|
|
$this->api_key = $api_key;
|
|
$this->secret_key = $secret_key;
|
|
}
|
|
|
|
/**
|
|
* Send the given notification.
|
|
*
|
|
* @param mixed $notifiable
|
|
* @param \Illuminate\Notifications\Notification $notification
|
|
* @return void
|
|
*/
|
|
public function send($notifiable, Notification $notification)
|
|
{
|
|
$message = $notification->toSms($notifiable);
|
|
$type = $notification->getType();
|
|
|
|
$message->to($notifiable->routeNotificationFor('sms', $notification));
|
|
|
|
if (! $message->to || (! ($message->params && $message->template_id) && ! $message->verification_code)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$this->http->post($this->sms_url . 'api/' . $type , [
|
|
'headers' => [
|
|
'x-sms-ir-secure-token'=> $this->getToken()
|
|
],
|
|
'connect_timeout' => 30,
|
|
'json' => $this->buildJsonPayload($message, $type),
|
|
]);
|
|
} catch (\GuzzleHttp\Exception\ConnectException $e) {
|
|
report($e);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* This method used in every request to get the token at first.
|
|
*
|
|
* @return mixed - the Token for use api
|
|
*/
|
|
protected function getToken()
|
|
{
|
|
$body = [
|
|
'UserApiKey' => $this->api_key,
|
|
'SecretKey' => $this->secret_key,
|
|
'System' => 'laravel_v_1_4'
|
|
];
|
|
|
|
try {
|
|
$result = $this->http->post( $this->sms_url . 'api/Token',['json'=>$body,'connect_timeout'=>30]);
|
|
} catch (\GuzzleHttp\Exception\ConnectException $e) {
|
|
report($e);
|
|
return;
|
|
}
|
|
|
|
return json_decode($result->getBody(),true)['TokenKey'];
|
|
}
|
|
|
|
/**
|
|
* Create sms json payload based on type
|
|
*
|
|
* @param SmsMessage $message
|
|
* @param $type
|
|
* @return array
|
|
*/
|
|
protected function buildJsonPayload(SmsMessage $message, $type) {
|
|
if ($type === 'UltraFastSend') {
|
|
return[
|
|
'ParameterArray' => $message->params,
|
|
'TemplateId' => $message->template_id,
|
|
'Mobile' => $message->to
|
|
];
|
|
}
|
|
|
|
if ($type === 'VerificationCode') {
|
|
return[
|
|
'Code' => $message->data,
|
|
'MobileNumber' => $message->to
|
|
];
|
|
}
|
|
}
|
|
}
|