diff --git a/database/migrations/create_sms_messages_table.php.stub b/database/migrations/create_sms_messages_table.php.stub index 5c20e0a..21f0bed 100644 --- a/database/migrations/create_sms_messages_table.php.stub +++ b/database/migrations/create_sms_messages_table.php.stub @@ -10,9 +10,10 @@ return new class extends Migration { Schema::create('sms_messages', function (Blueprint $table) { $table->id(); - $table->string('provider'); // To store the provider name - $table->string('recipient_number'); // To store the recipient's phone number - $table->text('message'); // To store the SMS message + $table->string('provider'); + $table->string('recipient_number'); + $table->json('data')->nullable(); + $table->tinyInteger('status'); $table->timestamps(); }); } diff --git a/src/Chapaar.php b/src/Chapaar.php index ea5c0a3..cff29bb 100755 --- a/src/Chapaar.php +++ b/src/Chapaar.php @@ -5,6 +5,7 @@ use TookanTech\Chapaar\Contracts\DriverConnector; use TookanTech\Chapaar\Contracts\DriverMessage; use TookanTech\Chapaar\Enums\Drivers; +use TookanTech\Chapaar\Events\SmsSent; class Chapaar { @@ -24,12 +25,21 @@ public function getDefaultDriver(): DriverConnector public function send($message): object { - return $this->driver($message->driver)->send($message); + $response = $this->driver($message->getDriver())->send($message); + if(count($response)) { + SmsSent::dispatch($message->driver,$message,$response['status']); + } + return $response; + } public function verify(DriverMessage $message): object { - return $this->driver($message->getDriver())->verify($message); + $response = $this->driver($message->getDriver())->verify($message); + if(count($response)) { + SmsSent::dispatch($message->getDriver(),$message,$response['status']); + } + return $response; } public function account(): object diff --git a/src/Events/SmsSent.php b/src/Events/SmsSent.php index 0b4001f..d139d2d 100644 --- a/src/Events/SmsSent.php +++ b/src/Events/SmsSent.php @@ -11,13 +11,13 @@ class SmsSent use Dispatchable, InteractsWithSockets, SerializesModels; public $provider; - public $recipientNumber; - public $message; + public $data; + public $status; - public function __construct($provider, $recipientNumber, $message) + public function __construct($provider, $data, $status) { $this->provider = $provider; - $this->recipientNumber = $recipientNumber; - $this->message = $message; + $this->data = $data; + $this->status = $status; } } \ No newline at end of file diff --git a/src/Listeners/StoreSmsMessage.php b/src/Listeners/StoreSmsMessage.php index 02dae16..2b2aa52 100644 --- a/src/Listeners/StoreSmsMessage.php +++ b/src/Listeners/StoreSmsMessage.php @@ -13,8 +13,8 @@ public function handle(SmsSent $event) { $smsMessage = new SmsMessage(); $smsMessage->provider = $event->provider; - $smsMessage->recipient_number = $event->recipientNumber; - $smsMessage->message = $event->message; + $smsMessage->data = $event->data; + $smsMessage->status = $event->status; $smsMessage->save(); } } \ No newline at end of file diff --git a/src/Models/SmsMessage.php b/src/Models/SmsMessage.php index da2ae14..4863b88 100644 --- a/src/Models/SmsMessage.php +++ b/src/Models/SmsMessage.php @@ -2,6 +2,7 @@ namespace TookanTech\Chapaar\Models; +use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Model; class SmsMessage extends Model @@ -9,7 +10,11 @@ class SmsMessage extends Model protected $fillable = [ 'provider', - 'recipient_number', - 'message', + 'data', + 'status', ]; + + protected $cast = [ + 'data' => AsCollection::class + ] }