puz/laravel-mailgun-webhooks

Handle Mailgun webhooks in a Laravel applications

dev-master 2019-10-24 18:16 UTC

This package is auto-updated.

Last update: 2024-03-25 05:11:27 UTC


README

A clone of spatie/laravel-stripe-webhooks

Latest Version on Packagist Total Downloads

Mailgun can notify your application of events using webhooks. This package can help you handle those webhooks. Out of the box it will verify the Mailgun signature of all incoming requests. All valid calls will be logged to the database. You can easily define jobs or events that should be dispatched when specific events hit your app.

This package will not handle what should be done after the webhook request has been validated and the right job or event is called. You should still code up any work yourself.

Installation

You can install the package via composer:

composer require puz/laravel-mailgun-webhooks

The service provider will automatically register itself.

You must publish the config file with:

php artisan vendor:publish --provider="Puz\MailgunWebhooks\MailgunWebhooksServiceProvider" --tag="config"

In the webhook_secret key of the config file you should add a valid webhook secret. You can find the secret used at the API security settings on the Mailgun dashboard.

Next, you must publish the migration with:

php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="migrations"

After the migration has been published you can create the webhook_calls table by running the migrations:

php artisan migrate

Finally, take care of the routing: At each domain on your Mailgun account, you must add the callback URL as a webhook for each event you want to listen too. In the routes file of your app you must pass that route to Route::mailgunWebhooks:

Route::mailgunWebhooks('webhook-route-configured-in-mailgun');

Behind the scenes this will register a POST route to a controller provided by this package. Because Mailgun has no way of getting a csrf-token, you must add that route to the except array of the VerifyCsrfToken middleware:

protected $except = [
    'webhook-route-configured-in-mailgun',
];

Usage

This package is a complete copy of spatie/laravel-stripe-webhooks and you can read more on how it works over there. There is some great code snippets to see how you can use it

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.