Skip to content

Webhook Channel ​

The webhook channel delivers HTTP JSON payloads to external systems and partner endpoints with automated HMAC-SHA256 signature verification.


The WebhookMessage Builder ​

Define a toWebhook() method returning a Jengo\Notifications\Messages\WebhookMessage:

php
namespace App\Notifications;

use Jengo\Notifications\Messages\WebhookMessage;
use Jengo\Notifications\Notification;

class SubscriptionRenewed extends Notification
{
    public function __construct(public array $subscription)
    {
        parent::__construct();
    }

    public function via(object $notifiable): array
    {
        return ['webhook'];
    }

    public function toWebhook(object $notifiable): WebhookMessage
    {
        return (new WebhookMessage())
            ->data([
                'event'        => 'subscription.renewed',
                'subscription' => $this->subscription,
                'occurred_at'  => date(DATE_ATOM),
            ])
            ->secret($notifiable->webhook_signing_secret)
            ->header('X-Customer-ID', (string) $notifiable->id)
            ->timeout(10);
    }
}

Security & Signature Verification ​

When a secret is provided, the driver automatically generates an HMAC-SHA256 digest of the raw JSON body and attaches it in the X-Jengo-Signature header:

http
POST /api/webhooks HTTP/1.1
Host: partner.com
Content-Type: application/json
User-Agent: Jengo-Notifications-Webhook/1.0
X-Jengo-Signature: 5d41402abc4b2a76b9719d911017c592...

The receiving endpoint can verify payload authenticity in PHP:

php
$rawBody = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_JENGO_SIGNATURE'] ?? '';

$expectedSignature = hash_hmac('sha256', $rawBody, $sharedSecret);

if (!hash_equals($expectedSignature, $signature)) {
    // Reject untrusted webhook payload
    http_response_code(401);
    exit('Invalid signature');
}

Recipient Routing ​

Define routeNotificationForWebhook() on the notifiable entity:

php
public function routeNotificationForWebhook(): ?string
{
    return $this->webhook_url;
}

Or dispatch on-demand:

php
Notification::route('webhook', 'https://partner.com/api/webhooks')
    ->notify(new SubscriptionRenewed($sub));

Released under the MIT License.