DEV Community

Cover image for Send webhooks with your Laravel application
Benjamin Iduwe
Benjamin Iduwe

Posted on

Send webhooks with your Laravel application

Laravel webhook allows businesses to send webhooks to their merchants/clients with ease. This package also introduces a
new artisan command to generate a webhook class.

Requirement

  • Composer v1.0/2.0
  • Php (7.3 and above)
  • Laravel (6 and above).

Installation

You can install the package via composer:

composer require bencoderus/laravel-webhook
Enter fullscreen mode Exit fullscreen mode

Setup

Publish basic components. (migrations and configuration files)

php artisan webhook:install
Enter fullscreen mode Exit fullscreen mode

Run migrations

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Basic usage

Create a new webhook class

php artisan make:webhook PaymentWebhook
Enter fullscreen mode Exit fullscreen mode

Creates a new webhook class in App\Http\Webhooks

You can format your webhook payload like a resource.

public function data(): array
    {
        return [
            'status' => $this->status,
            'amount' => $this->amount,
            'currency' => 'USD',
        ];
    }
Enter fullscreen mode Exit fullscreen mode

Sending a webhook.

$transaction = Transaction::first();

$webhook = new PaymentWebhook($transaction);
$webhook->url('https://httpbin.com')->send();
Enter fullscreen mode Exit fullscreen mode

Sending with an encrypted signature

$transaction = Transaction::first();

$webhook = new PaymentWebhook($transaction);
$webhook->url('https://httpbin.com')
        ->withSignature('x-key', 'value_to_hash')
        ->send();
Enter fullscreen mode Exit fullscreen mode

The default hashing algorithm is sha512 you can change it by passing a different hashing algorithm as the third
parameter for the withSignature method. PHP currently supports over 50 hashing algorithms.

Sending webhooks without using a Queue.
By default, all webhooks are dispatched using a queue to facilitate webhook retrial after failure. You can also send
webhooks without using a Queue by passing false to the send method.

$transaction = Transaction::first();

$webhook = new PaymentWebhook($transaction);
$webhook->URL("https://httpbin.com")->send(false);
Enter fullscreen mode Exit fullscreen mode



Find package on Github.
https://github.com/bencoderus/laravel-webhook

Top comments (0)