diff --git a/README.md b/README.md index 3ab0db7..210d576 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,14 @@ This is a laravel sms package with multi driver support. Supports laravel **v8.0+** and requires php **v7.4+** -> Star! if you used and liked this package. +> Star! :star: if you used and liked this package. ## Supported SMS Providers - [Kavenegar](https://kavenegar.com) - [SMS.ir](https://sms.ir) - [FarazSMS](https://farazsms.com) + ## Installation & Configuration Install using composer diff --git a/config/sms.php b/config/sms.php index 10ed044..2e1bb48 100644 --- a/config/sms.php +++ b/config/sms.php @@ -14,5 +14,6 @@ 'faraz_sms' => [ 'driver_class' => \Omalizadeh\Sms\Drivers\FarazSms\FarazSms::class, 'api_key' => '', + 'default_sender' => env('FARAZ_SMS_DEFAULT_SENDER', '+983000505'), ], ]; diff --git a/src/Drivers/FarazSms/FarazSms.php b/src/Drivers/FarazSms/FarazSms.php index f625a64..92764b4 100644 --- a/src/Drivers/FarazSms/FarazSms.php +++ b/src/Drivers/FarazSms/FarazSms.php @@ -97,12 +97,14 @@ public function getBulkSmsUrl(): string protected function mergeSmsOptions(array $data, array $options): array { - if (!isset($options['sender'])) { - throw new InvalidParameterException('sender parameter is required.'); + $sender = $options['sender'] ?? $this->getConfig('default_sender'); + + if (empty($sender)) { + throw new InvalidParameterException('sender parameter is required for Faraz sms driver.'); } return array_merge($data, [ - 'sender' => $options['sender'], + 'sender' => $sender, 'time' => $options['time'] ?? now()->addSecond(), ]); } @@ -121,7 +123,7 @@ protected function mergeTemplateOptions(array $data, array $options): array protected function callApi(string $url, array $data) { if (empty($apiKey = $this->getConfig('api_key'))) { - throw new InvalidConfigurationException('invalid api_key sms provider config'); + throw new InvalidConfigurationException('invalid api_key faraz sms provider config'); } $response = Http::asJson()->acceptJson()->withHeaders([ diff --git a/tests/FarazSmsTest.php b/tests/FarazSmsTest.php index 058e897..371c44a 100644 --- a/tests/FarazSmsTest.php +++ b/tests/FarazSmsTest.php @@ -3,6 +3,7 @@ namespace Omalizadeh\Sms\Tests; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Http; use Omalizadeh\Sms\Drivers\FarazSms\FarazSms; use Omalizadeh\Sms\Exceptions\SendingSmsFailedException; @@ -20,7 +21,7 @@ protected function setUp(): void $this->farazSms = new FarazSms(config('sms.faraz_sms')); } - public function test_sms_can_be_sent_successfully_by_faraz_driver() + public function test_sms_can_be_sent_successfully_by_faraz_driver(): void { Http::fake([ 'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([ @@ -30,7 +31,7 @@ public function test_sms_can_be_sent_successfully_by_faraz_driver() 'message_id' => '123456789' ], 'error_message' => null - ], 200) + ]) ]); $result = $this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']); @@ -46,14 +47,14 @@ public function test_sms_can_be_sent_successfully_by_faraz_driver() }); } - public function test_sms_can_get_failed_successfully() + public function test_sms_can_get_failed_successfully(): void { Http::fake([ 'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([ 'status' => 'error', 'code' => 5, 'message' => 'اعتبار کافی نیست.' - ], 200) + ]) ]); $this->expectException(SendingSmsFailedException::class); @@ -62,12 +63,12 @@ public function test_sms_can_get_failed_successfully() $this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']); } - public function test_invalid_response_structure_exception() + public function test_invalid_response_structure_exception(): void { Http::fake([ 'https://api2.ippanel.com/api/v1/sms/send/webservice/single' => Http::response([ 'unexpected' => 'response' - ], 200) + ]) ]); $this->expectException(SendingSmsFailedException::class); @@ -75,10 +76,16 @@ public function test_invalid_response_structure_exception() $this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']); } - public function test_missing_sender_option_leads_to_exception() + public function test_missing_sender_option_leads_to_exception(): void { + config([ + 'sms.faraz_sms.default_sender' => '', + ]); + + $this->farazSms = new FarazSms(config('sms.faraz_sms')); + $this->expectException(\Omalizadeh\Sms\Exceptions\InvalidParameterException::class); - $this->expectExceptionMessage('sender parameter is required.'); + $this->expectExceptionMessage('sender parameter is required for Faraz sms driver.'); $this->farazSms->send('09123456789', 'Test message'); }