|
|
@ -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 |
|
|
|
]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |