PHP Client

Gunakan PHP Client resmi BaBlast untuk mengirim pesan Standard WA, mengirim template Official WABA, dan memverifikasi webhook dari aplikasi PHP.

Package ini kompatibel dengan PHP >=8.1.


Instalasi Composer

hljs bash
composer require bablast/client

Raw PHP / CodeIgniter

1. Legacy / Device API Key

Legacy API Key sudah terikat ke satu sender. Argumen $senderCode pada sendText() diisi null, sedangkan WABA dapat memakai wrapper sendTemplate() secara langsung.

hljs php
use Bablast\Client;

$legacyBablast = new Client([
    'apiKey' => 'YOUR_LEGACY_DEVICE_API_KEY',
]);

$response = $legacyBablast->wa->sendText(
    '6281234567890',
    'Halo dari Legacy API Key!',
    null,
    [
        'reference_id' => 'REF-LEGACY-001',
        'order_id' => 'INV-2026-9901'
    ]
);

$transactionId = $response['data']['id'];

$legacyBablast->waba->sendTemplate(
    '6281234567890',
    'promo_agustus',
    'id',
    ['Budi']
);

2. Global API Key (Terbaru)

Global API Key memilih sender Standard WA melalui argumen $senderCode. Untuk media dan WABA dengan payload terbaru, gunakan Client::request().

hljs php
use Bablast\Client;

$globalBablast = new Client([
    'apiKey' => 'bk_live_YOUR_GLOBAL_API_KEY',
]);

$response = $globalBablast->wa->sendText(
    '6281234567890',
    'Halo dari Global API Key!',
    'SND-A1B2C3',
    [
        'reference_id' => 'REF-GLOBAL-001',
        'order_id' => 'INV-2026-9901',
        'customer_tier' => 'VIP'
    ]
);

$globalBablast->request('POST', 'send', [
    'sender_code' => 'SND-A1B2C3',
    'phone' => '6281234567890',
    'message' => 'Faktur Anda',
    'file' => 'https://example.com/invoice.pdf',
    'type' => 'file',
    'reference_id' => 'REF-GLOBAL-002',
    'metadata' => [
        'order_id' => 'INV-2026-9901',
    ],
]);

$wabaResponse = $globalBablast->request('POST', 'waba/send-template', [
    'sender_code' => 'SND-WABA01',
    'phone' => '6281234567890',
    'template_name' => 'promo_agustus',
    'language' => 'id',
    'parameters' => ['Budi'],
    'reference_id' => 'REF-GLOBAL-003',
    'metadata' => [
        'customer_tag' => 'VIP',
    ],
]);

$metaMessageId = $wabaResponse['data']['messageId'] ?? null;

Untuk sendText(), reference_id diletakkan di dalam metadata karena PHP Client belum menyediakan argumen khusus. API tetap memisahkannya menjadi data.reference_id pada webhook.

Method sendMedia() saat ini mengirim field mediaUrl, sedangkan endpoint /send membutuhkan file dan type. Karena itu contoh Global API Key memakai Client::request().


Laravel

Environment

Tambahkan konfigurasi berikut pada file .env:

hljs env
BABLAST_API_KEY=your_api_key_here
BABLAST_BASE_URL=https://api.bablast.id/v2/openapi
BABLAST_WEBHOOK_SECRET=your_secret_token

Publish Config

Jika Anda ingin mengubah konfigurasi package secara eksplisit:

hljs bash
php artisan vendor:publish --tag=bablast-config

1. Legacy / Device API Key

Implementasi Client mengekspos wa, waba, contacts, dan webhooks sebagai property. Karena itu, gunakan dependency injection atau app(Client::class) pada Laravel:

hljs php
use Bablast\Client;

$legacyBablast = app(Client::class);

$legacyBablast->wa->sendText(
    '6281234567890',
    'Halo dari Laravel Legacy!',
    null,
    ['reference_id' => 'REF-LARAVEL-LEGACY-001']
);

$legacyBablast->waba->sendTemplate(
    '6281234567890',
    'promo_agustus',
    'id',
    ['Diskon 50%']
);

2. Global API Key (Terbaru)

Pastikan BABLAST_API_KEY berisi Global API Key pada environment Laravel.

hljs php
use Bablast\Client;

$globalBablast = app(Client::class);

$globalBablast->wa->sendText(
    '6281234567890',
    'Halo dari Laravel Global!',
    'SND-A1B2C3',
    ['reference_id' => 'REF-LARAVEL-GLOBAL-001']
);

$globalBablast->request('POST', 'waba/send-template', [
    'sender_code' => 'SND-WABA01',
    'phone' => '6281234567890',
    'template_name' => 'promo_agustus',
    'language' => 'id',
    'parameters' => ['Diskon 50%'],
    'reference_id' => 'REF-LARAVEL-GLOBAL-002',
    'metadata' => ['source' => 'laravel'],
]);

Laravel Notification Channel

Gunakan notification channel ketika pesan WhatsApp menjadi bagian dari event aplikasi seperti invoice paid, order shipped, atau OTP. Contoh berikut menggunakan Global API Key:

hljs php
use Illuminate\Notifications\Notification;
use Bablast\Laravel\Channels\BablastChannel;
use Bablast\Laravel\Messages\BablastMessage;

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

    public function toBablast($notifiable)
    {
        return (new BablastMessage())
            ->content('Pembayaran invoice sudah diterima.')
            ->to($notifiable->phone)
            ->sender('SND-A1B2C3');
    }
}

Walaupun property internal pada BablastMessage masih bernama senderId, method sender() meneruskan nilainya sebagai argumen $senderCode ke WAModule.


Webhook Verification

Verifikasi webhook berlaku sama untuk Legacy API Key maupun Global API Key.

hljs php
use Bablast\Client;

$webhookClient = new Client([
    'apiKey' => getenv('BABLAST_API_KEY'),
]);

$rawPayload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
$secretToken = getenv('BABLAST_WEBHOOK_SECRET');

$isValid = $webhookClient->webhooks->verifySignature(
    $rawPayload,
    $signature,
    $secretToken
);

if (!$isValid) {
    http_response_code(401);
    exit('Invalid signature');
}

$event = $webhookClient->webhooks->parseEvent($rawPayload);
$payload = json_decode($rawPayload, true, flags: JSON_THROW_ON_ERROR);

$senderCode = $payload['sender_code'] ?? null;
$referenceId = $event['data']['reference_id'] ?? null;
$metadata = $event['data']['metadata'] ?? null;

parseEvent() versi PHP Client saat ini belum memetakan top-level sender_code; ambil field tersebut dari payload hasil decode seperti contoh di atas.