23
SepReceiver is a drop-in webhook handling library for Laravel.
Webhooks are a powerful part of any API lifecycle. Receiver aims to make handling incoming webhooks in your Laravel app a consistent and accessible process.
Out of the box, Receiver has built-in support for:
You can install this package via composer.
composer require hotmeteor/receiver
The Basics
Webhooks require an exposed endpoint to POST to. Receiver aims to make this a one-time setup that supports any of your incoming webhooks.
<?php
namespace App\Http\Controllers\Webhooks;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class WebhooksController extends Controller
{
public function store(Request $request)
{
Receiver::driver('slack')
->receive($request)
->ok();
}
}
driver
that should process the webhookreceive
the request for handling200 ok
responseThe Basics
Now that webhooks are being received they need to be handled. Receiver will look for designated Handler
classes for each event type that comes into the App\Http\Handlers[Driver]
namespace. Receiver does not provide these handlers – they are up to you to provide as needed. If Receiver doesn’t find a matching handler it simply ignores the event and responds with a 200 status code.
For example, a Stripe webhook handler would be App\Http\Handlers\Stripe\CustomerCreated
for the incoming customer.created
event.
Each handler is constructed with the event (name of the webhook event) and data properties.
<?php
namespace App\Http\Handlers\Stripe;
class CustomerCreated
{
public function __construct(public string $event, public array $data)
{
}
public function handle()
{
// Your code here
}
}
Many webhooks have ways of verifying their authenticity as they are received, most commonly through signatures or basic authentication. No matter the strategy, Receiver allows you to write custom verification codes as necessary. Simply implement the verify
method in your provider and return true or false if it passes.
A false
return will result in a 401 response being returned to the webhook sender.
<?php
namespace App\Http\Controllers\Webhooks;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class WebhooksController extends Controller
{
public function verify(Request $request): bool
{
// return result of verification
}
public function store(Request $request, string $driver)
{
Receiver::driver($driver)
->receive($request)
->ok();
}
}
Above are the basic details about this package, If you want to learn more, please visit Github
Published at : 23-09-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project