devaspid/laravel-whatsapp-gateway

Laravel package for WhatsApp Gateway API (wa-api-by-asp / https://wa-gateway.asp.web.id). Supports text messages, media, device management, and Laravel Notification Channel.

Maintainers

Package info

github.com/dev-asp-id/laravel-whatsapp-gateway

pkg:composer/devaspid/laravel-whatsapp-gateway

Transparency log

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-08-26 05:17 UTC

This package is auto-updated.

Last update: 2026-08-26 05:21:28 UTC


README

Latest Version on Packagist License

Package Laravel untuk integrasi dengan WhatsApp Gateway API wa-api-by-asp. Package ini menyediakan antarmuka yang bersih, modular, dan modern untuk mengirim pesan WhatsApp, mengelola device, serta integrasi penuh dengan Laravel Notification System.

โœจ Fitur

  • ๐Ÿ“จ Kirim Pesan Teks โ€” Quick send & Fluent API builder
  • ๐Ÿ–ผ๏ธ Kirim Media โ€” Gambar, Audio/Voice Note, Dokumen (PDF/Excel/ZIP)
  • ๐Ÿ“ฑ Device Management โ€” List, Create, QR Login, Pairing Code, Logout, Reconnect, Status
  • ๐Ÿ”” Laravel Notification Channel โ€” Kirim notifikasi via $user->notify()
  • ๐Ÿ›ก๏ธ Typed Exceptions โ€” AuthenticationException, ValidationException, DeviceNotFoundException, dll.
  • ๐Ÿ“ฆ Strongly-typed DTOs โ€” MessageResult, DeviceData, QrLoginResult
  • โšก Auto-discovery โ€” Service Provider & Facade otomatis terdaftar

๐Ÿ“‹ Persyaratan

  • PHP 8.2+
  • Laravel 10 / 11 / 12

๐Ÿš€ Instalasi

composer require devaspid/laravel-whatsapp-gateway

Publish config file:

php artisan vendor:publish --tag=whatsapp-gateway-config

Tambahkan environment variables ke .env:

WA_GATEWAY_BASE_URL=https://wa-gateway.asp.web.id/api/v1
WA_GATEWAY_CLIENT_ID=your-client-id
WA_GATEWAY_API_KEY=your-api-key
WA_GATEWAY_DEFAULT_DEVICE_ID=       # Opsional
WA_GATEWAY_TIMEOUT=15
WA_GATEWAY_RETRY_TIMES=2
WA_GATEWAY_RETRY_SLEEP=500

๐Ÿ“– Penggunaan

Validasi Koneksi (Ping)

use Devaspid\WhatsappGateway\Facades\Whatsapp;

if (Whatsapp::ping()) {
    echo 'API terhubung!';
}

Kirim Pesan Teks

use Devaspid\WhatsappGateway\Facades\Whatsapp;

// Quick send
$result = Whatsapp::send('6281234567890', 'Halo! Pesanan Anda telah dikonfirmasi.');

// Fluent API
$result = Whatsapp::to('6281234567890')
    ->usingDevice('dev_01m0xyz...')  // opsional
    ->replyTo('3EB0B430B6F...')      // opsional, quote reply
    ->message('Terima kasih telah berbelanja!')
    ->sendMessage();

// Cek hasil
if ($result->successful()) {
    echo 'Message ID: ' . $result->messageId();
}

Kirim Media

Gambar dengan Caption

Whatsapp::to('6281234567890')
    ->image('https://example.com/nota.png')
    ->caption('Bukti Pembayaran #INV-12345')
    ->viewOnce(false)
    ->sendMessage();

Dokumen PDF

Whatsapp::to('6281234567890')
    ->file('https://example.com/laporan.pdf')
    ->filename('Laporan_Tahunan_2026.pdf')
    ->caption('Silakan unduh dokumen terlampir.')
    ->sendMessage();

Audio / Voice Note

Whatsapp::to('6281234567890')
    ->audio('https://example.com/voice-greeting.mp3')
    ->sendMessage();

Device Management

// List semua devices
$devices = Whatsapp::devices()->list();

// Buat device baru
$device = Whatsapp::devices()->create('Customer Service WA');

// Detail device
$device = Whatsapp::devices()->find('dev_01m0xyz...');

// QR Code login
$qr = Whatsapp::devices()->getQrCode($device->deviceId);
echo $qr->toImgTag(); // <img src="data:image/png;base64,..." />

// Pairing Code
$code = Whatsapp::devices()->getPairingCode($device->deviceId, '6281234567890');
echo "Kode Pairing: {$code}";

// Status koneksi
$status = Whatsapp::devices()->getStatus($device->deviceId);
echo $status->isConnected() ? 'Online' : 'Offline';

// Reconnect, Logout, Delete
Whatsapp::devices()->reconnect($device->deviceId);
Whatsapp::devices()->logout($device->deviceId);
Whatsapp::devices()->delete($device->deviceId);

Laravel Notification Channel

Buat notification class:

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Devaspid\WhatsappGateway\Channels\WhatsappChannel;
use Devaspid\WhatsappGateway\Messages\WhatsappMessage;

class InvoicePaidNotification extends Notification
{
    public function via($notifiable): array
    {
        return [WhatsappChannel::class];
    }

    public function toWhatsapp($notifiable): WhatsappMessage
    {
        return WhatsappMessage::create()
            ->to($notifiable->phone_number)
            ->message("Halo {$notifiable->name}, pembayaran Anda sebesar Rp 150.000 telah kami terima.");
    }
}

Gunakan dari User model:

$user->notify(new InvoicePaidNotification());

Tip: Anda bisa menambahkan method routeNotificationForWhatsapp() pada model Notifiable untuk mengkustomisasi nomor tujuan:

public function routeNotificationForWhatsapp(): string
{
    return $this->whatsapp_number;
}

โš ๏ธ Exception Handling

Package ini melempar exception terstruktur yang dapat di-catch:

use Devaspid\WhatsappGateway\Exceptions\AuthenticationException;
use Devaspid\WhatsappGateway\Exceptions\ValidationException;
use Devaspid\WhatsappGateway\Exceptions\DeviceNotFoundException;
use Devaspid\WhatsappGateway\Exceptions\GatewayConnectionException;
use Devaspid\WhatsappGateway\Exceptions\RateLimitException;
use Devaspid\WhatsappGateway\Exceptions\WhatsappGatewayException;

try {
    Whatsapp::send('6281234567890', 'Hello!');
} catch (AuthenticationException $e) {
    // 401 โ€” API key salah
} catch (ValidationException $e) {
    // 422 โ€” Validasi gagal
    $errors = $e->getErrors(); // ['phone' => ['Format nomor tidak valid']]
} catch (DeviceNotFoundException $e) {
    // 404 โ€” Device tidak ditemukan
} catch (RateLimitException $e) {
    // 429 โ€” Terlalu banyak request
} catch (GatewayConnectionException $e) {
    // 502/503 โ€” WA engine offline
} catch (WhatsappGatewayException $e) {
    // Catch-all untuk error lainnya
}

๐Ÿงช Testing

Jalankan test suite:

composer test

Atau langsung:

./vendor/bin/phpunit

Dalam project Anda, gunakan Http::fake() untuk mocking:

use Illuminate\Support\Facades\Http;

Http::fake([
    'wa-gateway.asp.web.id/api/v1/messages' => Http::response([
        'success' => true,
        'data' => ['message_id' => 'FAKE_MSG_001', 'status' => 'success'],
    ]),
]);

$result = Whatsapp::send('6281234567890', 'Test message');
assertTrue($result->successful());

๐Ÿ“ Struktur Package

src/
โ”œโ”€โ”€ Channels/
โ”‚   โ””โ”€โ”€ WhatsappChannel.php             # Laravel Notification Channel
โ”œโ”€โ”€ Contracts/
โ”‚   โ””โ”€โ”€ WhatsappGatewayInterface.php    # Interface kontrak
โ”œโ”€โ”€ DTOs/
โ”‚   โ”œโ”€โ”€ DeviceData.php                  # DTO Device
โ”‚   โ”œโ”€โ”€ DeviceStatusData.php            # DTO Status Device
โ”‚   โ”œโ”€โ”€ MessageResult.php               # DTO Hasil Pengiriman
โ”‚   โ””โ”€โ”€ QrLoginResult.php               # DTO QR Code
โ”œโ”€โ”€ Exceptions/
โ”‚   โ”œโ”€โ”€ AuthenticationException.php     # 401
โ”‚   โ”œโ”€โ”€ DeviceNotFoundException.php     # 404
โ”‚   โ”œโ”€โ”€ GatewayConnectionException.php  # 502/503
โ”‚   โ”œโ”€โ”€ RateLimitException.php          # 429
โ”‚   โ”œโ”€โ”€ ValidationException.php         # 422
โ”‚   โ””โ”€โ”€ WhatsappGatewayException.php    # Base Exception
โ”œโ”€โ”€ Facades/
โ”‚   โ””โ”€โ”€ Whatsapp.php                    # Facade
โ”œโ”€โ”€ Messages/
โ”‚   โ”œโ”€โ”€ WhatsappMessage.php             # Fluent builder teks
โ”‚   โ””โ”€โ”€ WhatsappMediaMessage.php        # Fluent builder media
โ”œโ”€โ”€ Services/
โ”‚   โ”œโ”€โ”€ DeviceService.php               # Device management
โ”‚   โ””โ”€โ”€ MessageService.php              # Pengiriman pesan
โ”œโ”€โ”€ WhatsappClient.php                  # HTTP Client wrapper
โ”œโ”€โ”€ WhatsappGateway.php                 # Core Manager
โ””โ”€โ”€ WhatsappServiceProvider.php         # Service Provider
config/
โ””โ”€โ”€ whatsapp-gateway.php                # File konfigurasi

๐Ÿ”ง Konfigurasi

File config/whatsapp-gateway.php:

Key Env Variable Default Keterangan
base_url WA_GATEWAY_BASE_URL https://wa-gateway.asp.web.id/api/v1 Base URL API
client_id WA_GATEWAY_CLIENT_ID '' Client ID dari dashboard
api_key WA_GATEWAY_API_KEY '' API Key dari dashboard
default_device_id WA_GATEWAY_DEFAULT_DEVICE_ID null Device ID default
timeout WA_GATEWAY_TIMEOUT 15 Timeout HTTP (detik)
retry.times WA_GATEWAY_RETRY_TIMES 2 Jumlah retry
retry.sleep WA_GATEWAY_RETRY_SLEEP 500 Jeda retry (ms)

๐Ÿ“„ Lisensi

MIT License. Lihat file LICENSE untuk detail.