diff --git a/app/Channels/SmsChannel.php b/app/Channels/SmsChannel.php index 49d72d4..c54e34a 100644 --- a/app/Channels/SmsChannel.php +++ b/app/Channels/SmsChannel.php @@ -3,6 +3,7 @@ namespace App\Channels; +use App\Channels\Messages\SmsMessage; use Illuminate\Notifications\Notification; use GuzzleHttp\Client as HttpClient; @@ -48,21 +49,68 @@ class SmsChannel $message->to($notifiable->routeNotificationFor('sms', $notification)); - if (! $message->to) { + 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'=>self::getToken() + 'x-sms-ir-secure-token'=> $this->getToken() ], 'connect_timeout' => 30, - 'json' => ['data' => $message->data], + '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' => config('smsirlaravel.api-key'), + 'SecretKey' => config('smsirlaravel.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 + ]; + } + } } diff --git a/app/Notifications/SmsNotification.php b/app/Notifications/SmsNotification.php index f44abcb..44d5852 100644 --- a/app/Notifications/SmsNotification.php +++ b/app/Notifications/SmsNotification.php @@ -2,6 +2,7 @@ namespace App\Notifications; +use App\Channels\Messages\SmsMessage; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; @@ -14,6 +15,12 @@ class SmsNotification extends Notification public $message; + /** + * Show type of sms notification. + * [ultraFastSend or sendVerification ] + * + * @var mixed|string + */ public $type; /** @@ -21,7 +28,7 @@ class SmsNotification extends Notification * * @return void */ - public function __construct($message, $type = 'MessageSend') + public function __construct($message, $type = 'sendVerification') { $this->message = $message; $this->type = $type; @@ -46,9 +53,11 @@ class SmsNotification extends Notification */ public function toSms($notifiable) { - return [ + return (new SmsMessage()) + ->params($this->message['params'] ?? null) + ->templateId($this->message['template_id'] ?? null) + ->verificationCode($this->message['verification_code'] ?? null); - ]; } public function getType()