- How do I integrate this Kavenegar SMS package into a Laravel application?
- Install via Composer with `composer require kavenegar/php`, then instantiate the client in a service or controller using your API key from `.env`. For better Laravel integration, bind it to the service container: `$this->app->singleton(KavenegarApi::class, fn($app) => new KavenegarApi(config('services.kavenegar.api_key')));`.
- Does this package support Laravel’s queue system for async SMS sending?
- No, it’s synchronous by default. Wrap `KavenegarApi::Send()` in a Laravel job (e.g., `SendSmsJob`) to queue SMS delivery. Use `ShouldQueue` and dispatch it via `dispatch(new SendSmsJob($phone, $message))` for background processing.
- What Laravel versions and PHP versions are supported?
- The package works with PHP 7.4+ and Laravel 8+. It lacks PHP 8.1+ features like named arguments, so ensure your project aligns with these constraints. Test thoroughly if using Laravel 9+.
- How do I handle failed SMS deliveries or retries?
- The package throws `ApiException` or `HttpException` for errors. For retries, implement a custom job with `ShouldQueue` and use Laravel’s `retryAfter()` or a package like `spatie/laravel-queue-retries` for exponential backoff.
- Can I use this for high-volume SMS campaigns or is it only for OTPs?
- It’s suitable for both, but for high-volume campaigns, queue the SMS jobs to avoid timeouts. Monitor queue backlogs with Laravel Horizon and consider batch processing (e.g., chunking recipients).
- How do I securely store the Kavenegar API key in Laravel?
- Store it in `.env` (e.g., `KAVENEGAR_API_KEY=your_key`) and access it via `config('services.kavenegar.api_key')`. For production, use Laravel’s encrypted `.env` or a secrets manager like AWS Secrets Manager.
- Does this package support inbound SMS or webhook callbacks?
- No, it’s designed for outbound SMS only. For inbound SMS or webhooks, you’ll need to integrate Kavenegar’s webhook endpoints directly or use a middleware to validate and process callbacks.
- How can I mock this SMS API for testing in Laravel?
- Manually mock the `KavenegarApi` class in tests by extending it or using Laravel’s `Mockery` facade. For example: `$mockApi = Mockery::mock(KavenegarApi::class)->shouldReceive('Send')->andReturn([...]);`. No built-in test double exists.
- Are there alternatives if I need more features like templates or scheduling?
- For templates or scheduling, consider extending this package or switching to a provider like Twilio (via `laravel-notification-channels/twilio`) or a Laravel-specific SMS package like `spatie/laravel-sms-notifications`.
- How do I log SMS costs or responses for auditing in Laravel?
- Access the `cost` field in the response object and log it to a database table (e.g., `sms_logs`) using Laravel’s `DB::table()` or Eloquent. Create an observer or job to process `KavenegarApi::Send()` responses post-delivery.