diff --git a/app/Channels/Messages/SmsMessage.php b/app/Channels/Messages/SmsMessage.php new file mode 100644 index 0000000..36f5214 --- /dev/null +++ b/app/Channels/Messages/SmsMessage.php @@ -0,0 +1,115 @@ +to = $to; + + return $this; + } + + /** + * Set the data of the sms message. + * + * @param array $data + * @return $this + */ + public function data(array $data) + { + $this->data = $data; + + return $this; + } + + /** + * Set the params of the sms message when we use ultraFastSend method + * + * @param array|mixed $params + * @return $this + */ + public function params($params) + { + foreach ($params as $key => $value) { + $this->params[] = ['Parameter' => $key, 'ParameterValue' => $value]; + } + return $this; + } + + /** + * Set the template_id of the sms message when we use ultraFastSend method + * + * @param string $template_id + * @return $this + */ + public function templateId($template_id) + { + $this->template_id = $template_id; + + return $this; + } + + /** + * Set verification_code of sms message when we use sendVerification method + * + * @param string $verification_code + * @return $this + */ + public function verificationCode(string $verification_code) + { + $this->verification_code = $verification_code; + + return $this; + } + +} diff --git a/app/Channels/SmsChannel.php b/app/Channels/SmsChannel.php index 49d72d4..47ef5d4 100644 --- a/app/Channels/SmsChannel.php +++ b/app/Channels/SmsChannel.php @@ -3,18 +3,33 @@ namespace App\Channels; +use App\Channels\Messages\SmsMessage; use Illuminate\Notifications\Notification; use GuzzleHttp\Client as HttpClient; class SmsChannel { /** - * The API URL for Socket. + * 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. * @@ -28,10 +43,12 @@ class SmsChannel * @param \GuzzleHttp\Client $http * @return void */ - public function __construct(HttpClient $http, string $sms_url) + 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; } /** @@ -48,21 +65,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' => $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 + ]; + } + } } diff --git a/app/Enums/sms.php b/app/Enums/sms.php new file mode 100644 index 0000000..0741360 --- /dev/null +++ b/app/Enums/sms.php @@ -0,0 +1,12 @@ + [ + 'verification_code' => [ + 'name' => 'VerificationCode' + ], + 'ultra_fast_send' => [ + 'name' => 'ultraFastSend' + ] + ], +]; diff --git a/app/Listeners/NotifHandler.php b/app/Listeners/NotifHandler.php index 1fed4db..c53def9 100644 --- a/app/Listeners/NotifHandler.php +++ b/app/Listeners/NotifHandler.php @@ -3,8 +3,10 @@ namespace App\Listeners; use App\Events\ModelSaved; +use App\Notifications\SocketNotification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Support\Facades\Notification; class NotifHandler { @@ -29,8 +31,12 @@ class NotifHandler $message = json_decode($event->message); $event_class = 'App\Events\\'.enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'); if (class_exists($event_class)) { -// event(new ('App\Events\\'.$event_class($message))); $event_class::dispatch($message); } + Notification::send(auth()->user(), new SocketNotification( + [ + 'message' => enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'), + 'payload'=>$business_info + ])); } } diff --git a/app/Models/Business.php b/app/Models/Business.php index 80e869f..e308bad 100644 --- a/app/Models/Business.php +++ b/app/Models/Business.php @@ -5,6 +5,8 @@ namespace App\Models; use App\Models\File; use App\Models\Model; use App\Models\SoftDeletes; +use App\Notifications\SocketNotification; +use Illuminate\Support\Facades\Notification; use Illuminate\Validation\Rule; use Illuminate\Http\UploadedFile; use Spatie\MediaLibrary\HasMedia; @@ -234,6 +236,8 @@ class Business extends Model implements HasMedia Cache::put('business_info'.$businessId , $business_info, config('app.cache_ttl')); + Notification::send(auth()->user(), new SocketNotification(['message' => 'business info update','payload'=>$business_info])); + return $business_info; } diff --git a/app/Models/User.php b/app/Models/User.php index ea04af2..aa0357c 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -60,6 +60,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac return request('_business_info')['id'] ?? null; } + /** + * Specifies the user's phone number + * + * @return string + */ + public function routeNotificationForSms() + { + return $this->mobile; + } + public function updateRelations() { // projects relations diff --git a/app/Notifications/DBNotification.php b/app/Notifications/DBNotification.php index 1711b56..61a553f 100644 --- a/app/Notifications/DBNotification.php +++ b/app/Notifications/DBNotification.php @@ -29,7 +29,7 @@ class DBNotification extends Notification */ public function via($notifiable) { - return ['db']; + return ['database']; } /** diff --git a/app/Notifications/SmsNotification.php b/app/Notifications/SmsNotification.php index f44abcb..773b6ae 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,10 +28,10 @@ class SmsNotification extends Notification * * @return void */ - public function __construct($message, $type = 'MessageSend') + public function __construct($message, $type = null) { $this->message = $message; - $this->type = $type; + $this->type = $type ?? enum('sms.types.verification_code.name'); } /** @@ -46,9 +53,11 @@ class SmsNotification extends Notification */ public function toSms($notifiable) { - return [ + return (new SmsMessage()) + ->params($this->message['params'] ?? []) + ->templateId($this->message['template_id'] ?? null) + ->verificationCode($this->message['verification_code'] ?? null); - ]; } public function getType() diff --git a/app/Notifications/SocketNotification.php b/app/Notifications/SocketNotification.php index a734ba9..9a6a733 100644 --- a/app/Notifications/SocketNotification.php +++ b/app/Notifications/SocketNotification.php @@ -45,8 +45,8 @@ class SocketNotification extends Notification { return (new SocketMessage()) ->data([ - 'title' => $this->message['title'], - 'body' => $this->message['body'], + 'message' => $this->message['message'], + 'payload' => $this->message['payload'], ]); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d25685e..d757d16 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,6 +4,7 @@ namespace App\Providers; use App\Channels\DBChannel; use App\Channels\FcmChannel; +use App\Channels\SmsChannel; use App\Channels\SocketChannel; use Illuminate\Notifications\ChannelManager; use Illuminate\Support\Facades\Notification; @@ -26,8 +27,16 @@ class AppServiceProvider extends ServiceProvider $service->extend('socket', function ($app) { return new SocketChannel(new HttpClient, config('socket.url')); }); - $service->extend('db', function ($app) { - return new DBChannel(); +// $service->extend('db', function ($app) { +// return new DBChannel(); +// }); + $service->extend('sms', function ($app) { + return new SmsChannel( + new HttpClient, + config('smsirlaravel.webservice-url'), + config('smsirlaravel.api-key'), + config('smsirlaravel.secret-key'), + ); }); }); } diff --git a/app/Utilities/HelperClass/NotificationHelper.php b/app/Utilities/HelperClass/NotificationHelper.php index d128e9c..f668871 100644 --- a/app/Utilities/HelperClass/NotificationHelper.php +++ b/app/Utilities/HelperClass/NotificationHelper.php @@ -8,11 +8,14 @@ use App\Notifications\DBNotification; use App\Notifications\FcmNotification; use App\Notifications\MailNotification; use App\Notifications\SocketNotification; +use App\Notifications\SmsNotification; use Illuminate\Support\Facades\Notification; class NotificationHelper { protected $notif; + + protected $sms_notif; /** * Make notification object * @@ -33,6 +36,12 @@ class NotificationHelper return $this; } + public function makeSmsNotif($template_name, $options = []) + { + $this->sms_notif = __('notification.sms.templates.'.$template_name, $options); + return $this; + } + /** * Fetch message from notifications lang file * @@ -56,7 +65,7 @@ class NotificationHelper public function sendNotifications($users, $level = null) { switch ($level) { case "emergency": -// Notification::send($users, new SmsNotification($notif)); + Notification::send($users, new SmsNotification($this->sms_notif, enum('sms.types.ultra_fast_send.name'))); case "critical": Notification::send($users, new MailNotification($this->notif)); case "high": @@ -64,10 +73,10 @@ class NotificationHelper case "medium": Notification::send($users, new FcmNotification($this->notif)); case "low": - Notification::send($users, new SocketNotification($this->notif)); +// Notification::send($users, new SocketNotification($this->notif)); break; default: - Notification::send($users, new SocketNotification($this->notif)); +// Notification::send($users, new SocketNotification($this->notif)); } } diff --git a/config/smsirlaravel.php b/config/smsirlaravel.php new file mode 100644 index 0000000..3ef586a --- /dev/null +++ b/config/smsirlaravel.php @@ -0,0 +1,16 @@ + env('SMSIR_WEBSERVICE_URL','https://ws.sms.ir/'), + // SMS.ir Api Key + 'api-key' => env('SMSIR_API_KEY',null), + // SMS.ir Secret Key + 'secret-key' => env('SMSIR_SECRET_KEY',null), + // Your sms.ir line number + 'line-number' => env('SMSIR_LINE_NUMBER',null), + // ====================================================================== +]; diff --git a/database/migrations/2021_03_08_114700_create_notifications_table.php b/database/migrations/2021_03_08_114700_create_notifications_table.php index 16b08ad..9797596 100644 --- a/database/migrations/2021_03_08_114700_create_notifications_table.php +++ b/database/migrations/2021_03_08_114700_create_notifications_table.php @@ -17,7 +17,6 @@ class CreateNotificationsTable extends Migration $table->uuid('id')->primary(); $table->string('type'); $table->morphs('notifiable'); - $table->unsignedBigInteger('business_id'); $table->text('data'); $table->timestamp('read_at')->nullable(); $table->timestamps(); diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index 856ea0d..39d08d2 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -85,4 +85,16 @@ return [ 'suspended' => 'حساب مسدود شد.', ], + 'sms' => [ + 'templates' => [ + 'template_name' => [ + 'template_id' => 'asdasd', + 'params' => [ + 'user' => ':user', + 'business' => ':business', + ] + ] + ] + ] + ]; diff --git a/routes/api.php b/routes/api.php index adb8f8f..087aeb0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -8,7 +8,9 @@ $router->get('/lab', function () { $router->get('/ntest', function () { $user = \App\Models\User::find(1); - \Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SocketNotification(['title' => "hello!!!", 'body' => 'sss'])); + \Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SmsNotification(['verification_code' => "1234"])); +// (new \App\Utilities\HelperClass\NotificationHelper()) +// ->makeSmsNotif('template_name', ['user' => 'myUser', 'business' => 'myBusiness']); })->middleware('bindBusiness'); $router->group(['prefix' => 'actions'], function () use ($router) { $router->group(['prefix' => 'businesses'], function () use ($router) {