crazzzzy/laravel-sms-sender

There is no license information available for the latest version (dev-dev) of this package.

SMS sender

This package's canonical repository appears to be gone and the package has been frozen as a result.

dev-dev 2019-03-12 09:11 UTC

This package is not auto-updated.

Last update: 2019-03-12 09:21:13 UTC


README

Send sms to phone number by notifications

composer require crazzzzy/laravel-sms-send

Add provider to config/app.php

CraZZzzY\SmsSender\SmsSenderServiceProvider::class,

Controller, example:

<?php

namespace App\Http\Controllers\API\Password\Reset;

use Notification;

use App\Models\User;
use App\Notifications\Sms;

use CraZZzzY\SmsSender\Helpers\SmsHelper;
use CraZZzzY\SmsSender\Helpers\ResponseStatusHelper;
use CraZZzzY\SmsSender\Http\Responses\API\StatusResponse;
use CraZZzzY\SmsSender\Http\Controllers\API\Password\Reset\Phone as ResetPhoneController;

class Phone extends ResetPhoneController
{
    protected function reset(array $data) : StatusResponse
    {
        $user = User::where('phone_number', '=', $data['phone_number'])->first();

        if (!($user instanceof User)) {
            return new StatusResponse(ResponseStatusHelper::STATUS_ERROR, [
                'message' => 'Phone not found.'
            ]);
        }

        // Create reset code.
        $code = $this->createResetCode($user);

        // Send your notification.
        Notification::send($user, new Sms(SmsHelper::TYPE_RESET_CODE, $code->code));

        return new StatusResponse(ResponseStatusHelper::STATUS_SUCCESS);
    }
}

Add route, example:

Route::group(['namespace' => 'Password', 'as' => 'api.'], function () {
    Route::post('phone/password/reset/generate_code', 'Reset\Phone')->name('phone.password.reset.generate_code');
});

Create notification file with extend:

Not queue

// your-notification-class.php

use CraZZzzY\SmsSender\Notifications\SmsSend

class YourNotificationClass extends SmsSend

With queue

// your-notification-class.php

use CraZZzzY\SmsSender\Notifications\SmsSendByQueue

class YourNotificationClass extends SmsSendByQueue

Add in service your sms provider settings.

Example for Clickatell:

// config/services.php
...
'clickatell' => [
    'user'  => env('CLICKATELL_USER'),
    'pass' => env('CLICKATELL_PASS'),
    'api_id' => env('CLICKATELL_API_ID'),
],

Add in your notification methods for your sms provider:

Example for Clickatell:

use NotificationChannels\Clickatell\ClickatellMessage;
use NotificationChannels\Clickatell\ClickatellChannel;

use CraZZzzY\SmsSender\Notifications\Traits\SmsSend as TraitSmsSend;

...

use TraitSmsSend;

public function via($notifiable)
{
    return [ClickatellChannel::class];
}

public function toClickatell($notifiable)
{
    return (new ClickatellMessage())
        ->content($this->getMessageByType())
    ;
}

Add in your model method get phone field for yor sms provider:

Example for Clickatell:

public function routeNotificationForClickatell()
{
    return [$this->(your-phone-field)];
}

How to use:

TYPE_MESSAGE:

use CraZZzzY\SmsSender\Helpers\SmsHelper;

...

Notification::send($user, new YourNotificationClass(SmsHelper::TYPE_MESSAGE, 'your-message'));

TYPE_RESET_CODE:

use CraZZzzY\SmsSender\Helpers\SmsHelper;

...

Notification::send($user, new YourNotificationClass(SmsHelper::TYPE_RESET_CODE));

TYPE_ACCOUNT_APPROVED:

use CraZZzzY\SmsSender\Helpers\SmsHelper;

... 

Notification::send($user, new YourNotificationClass(SmsHelper::TYPE_ACCOUNT_APPROVED));

TYPE_ORDER_APPROVED:

use CraZZzzY\SmsSender\Helpers\SmsHelper;

... 

Notification::send($user, new YourNotificationClass(SmsHelper::TYPE_ORDER_APPROVED, 'order-number'));

TYPE_ORDER_DECLINED:

use CraZZzzY\SmsSender\Helpers\SmsHelper;

... 

Notification::send($user, new YourNotificationClass(SmsHelper::TYPE_ORDER_DECLINED, 'order-number'));