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