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

  1. <?php
  2. namespace App\Channels;
  3. use App\Channels\Messages\SmsMessage;
  4. use Illuminate\Notifications\Notification;
  5. use GuzzleHttp\Client as HttpClient;
  6. class SmsChannel
  7. {
  8. /**
  9. * The API URL for sms.
  10. *
  11. * @var string
  12. */
  13. protected $sms_url;
  14. /**
  15. * The api key for sms inside sms.ir panel.
  16. *
  17. * @var string
  18. */
  19. protected $api_key;
  20. /**
  21. * The secret key for sms inside sms.ir panel.
  22. *
  23. * @var string
  24. */
  25. protected $secret_key;
  26. /**
  27. * The HTTP client instance.
  28. *
  29. * @var \GuzzleHttp\Client
  30. */
  31. protected $http;
  32. /**
  33. * Create a new Socket channel instance.
  34. *
  35. * @param \GuzzleHttp\Client $http
  36. * @return void
  37. */
  38. public function __construct(HttpClient $http, string $sms_url, string $api_key, string $secret_key)
  39. {
  40. $this->http = $http;
  41. $this->sms_url = $sms_url;
  42. $this->api_key = $api_key;
  43. $this->secret_key = $secret_key;
  44. }
  45. /**
  46. * Send the given notification.
  47. *
  48. * @param mixed $notifiable
  49. * @param \Illuminate\Notifications\Notification $notification
  50. * @return void
  51. */
  52. public function send($notifiable, Notification $notification)
  53. {
  54. $message = $notification->toSms($notifiable);
  55. $type = $notification->getType();
  56. $message->to($notifiable->routeNotificationFor('sms', $notification));
  57. if (! $message->to || (! ($message->params && $message->template_id) && ! $message->verification_code)) {
  58. return;
  59. }
  60. try {
  61. $this->http->post($this->sms_url . 'api/' . $type , [
  62. 'headers' => [
  63. 'x-sms-ir-secure-token'=> $this->getToken()
  64. ],
  65. 'connect_timeout' => 30,
  66. 'json' => $this->buildJsonPayload($message, $type),
  67. ]);
  68. } catch (\GuzzleHttp\Exception\ConnectException $e) {
  69. report($e);
  70. }
  71. }
  72. /**
  73. * This method used in every request to get the token at first.
  74. *
  75. * @return mixed - the Token for use api
  76. */
  77. protected function getToken()
  78. {
  79. $body = [
  80. 'UserApiKey' => $this->api_key,
  81. 'SecretKey' => $this->secret_key,
  82. 'System' => 'laravel_v_1_4'
  83. ];
  84. try {
  85. $result = $this->http->post( $this->sms_url . 'api/Token',['json'=>$body,'connect_timeout'=>30]);
  86. } catch (\GuzzleHttp\Exception\ConnectException $e) {
  87. report($e);
  88. return;
  89. }
  90. return json_decode($result->getBody(),true)['TokenKey'];
  91. }
  92. /**
  93. * Create sms json payload based on type
  94. *
  95. * @param SmsMessage $message
  96. * @param $type
  97. * @return array
  98. */
  99. protected function buildJsonPayload(SmsMessage $message, $type) {
  100. if ($type === 'UltraFastSend') {
  101. return[
  102. 'ParameterArray' => $message->params,
  103. 'TemplateId' => $message->template_id,
  104. 'Mobile' => $message->to
  105. ];
  106. }
  107. if ($type === 'VerificationCode') {
  108. return[
  109. 'Code' => $message->data,
  110. 'MobileNumber' => $message->to
  111. ];
  112. }
  113. }
  114. }