Laravel Package
The official Wazera Laravel package provides a clean, idiomatic integration for Laravel applications.
Installation
composer require wazera/laravel
Publish Configuration
php artisan vendor:publish --tag=wazera-config
Environment Variables
Add to your .env:
WAZERA_API_KEY=wz_live_your_key_here
WAZERA_BASE_URL=https://app.wazera.com/api/v1
WAZERA_TIMEOUT=30
WAZERA_RETRIES=3
Configuration
The config file config/wazera.php:
return [
'api_key' => env('WAZERA_API_KEY'),
'base_url' => env('WAZERA_BASE_URL', 'https://app.wazera.com/api/v1'),
'timeout' => env('WAZERA_TIMEOUT', 30),
'retries' => env('WAZERA_RETRIES', 3),
'verify_ssl' => env('WAZERA_VERIFY_SSL', true),
'channel_id' => env('WAZERA_CHANNEL_ID'),
'webhook_secret' => env('WAZERA_WEBHOOK_SECRET'),
];
Usage
Using the Facade
use Wazera\Laravel\Facades\Wazera;
// Send a message
$response = Wazera::sendMessage([
'to' => '201234567890',
'message' => 'Hello!'
]);
// Send a template
$response = Wazera::sendTemplate([
'to' => '201234567890',
'template' => 'welcome',
'params' => ['name' => 'John']
]);
// Create a contact
$contact = Wazera::createContact([
'phone' => '201234567890',
'name' => 'John Doe',
'email' => 'john@example.com',
'tags' => ['customer', 'vip']
]);
// Get delivery status
$status = Wazera::getDeliveryStatus('msg_abc123');
// Trigger automation
Wazera::triggerAutomation([
'automation_id' => 'auto_xyz',
'contact_phone' => '201234567890',
'data' => ['order_id' => '1234']
]);
Using Dependency Injection
use Wazera\Laravel\WazeraClient;
class NotificationService
{
public function __construct(
private WazeraClient $wazera
) {}
public function notifyCustomer(Customer $customer, string $message): void
{
$this->wazera->sendMessage([
'to' => $customer->phone,
'message' => $message,
]);
}
}
Laravel Notifications
use Wazera\Laravel\Notifications\WhatsAppChannel;
use Wazera\Laravel\Notifications\WhatsAppMessage;
class OrderShipped extends Notification
{
public function via($notifiable)
{
return [WhatsAppChannel::class, 'mail'];
}
public function toWhatsApp($notifiable)
{
return (new WhatsAppMessage)
->to($notifiable->phone)
->template('order_shipped')
->params([
'name' => $notifiable->name,
'tracking' => $this->order->tracking_number,
]);
}
}
Queue Support
// Dispatch to queue
Wazera::queue()->sendMessage([
'to' => '201234567890',
'message' => 'This will be sent via queue'
]);
Error Handling
use Wazera\Laravel\Exceptions\WazeraException;
use Wazera\Laravel\Exceptions\ValidationException;
use Wazera\Laravel\Exceptions\RateLimitException;
use Wazera\Laravel\Exceptions\AuthenticationException;
try {
Wazera::sendMessage([...]);
} catch (RateLimitException $e) {
// Retry after $e->retryAfter() seconds
Log::warning('Rate limited', ['retry_after' => $e->retryAfter()]);
} catch (ValidationException $e) {
// Handle validation errors
Log::error('Validation failed', ['errors' => $e->errors()]);
} catch (WazeraException $e) {
// Handle any other Wazera error
Log::error('Wazera error', [
'code' => $e->getCode(),
'message' => $e->getMessage(),
]);
}
Testing
The package includes a fake for testing:
use Wazera\Laravel\Facades\Wazera;
public function test_sends_order_notification()
{
Wazera::fake();
// ... trigger your code ...
Wazera::assertSent(function ($message) {
return $message['to'] === '201234567890'
&& str_contains($message['message'], 'Order confirmed');
});
Wazera::assertSentCount(1);
}