You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Allow more flexibility in when webhooks are fired
- Allow implementors to use custom models
- Improved documentation
- Provide model contracts
- No longer need to manually implement webhook payload methods, can now use DeliversWebhooks trait
BREAKING: Changed existing model namespaces, and method names. Changed ShouldDeliverWebhooks contract.
A wrapper for [spatie/laravel-webhook-server](https://github.com/spatie/laravel-webhook-server) that provides a simple & flexible webhook registry, allows you to expose any Laravel Event as a webhook, and provides out-of-the-box logging of webhook results.
17
15
18
16
## Installation
19
17
@@ -22,23 +20,6 @@ Install via composer
22
20
composer require custom-d/webhook-registry
23
21
```
24
22
25
-
### Register Service Provider
26
-
27
-
**Note! This and next step are optional if you use laravel>=5.5 with package
28
-
auto discovery feature.**
29
-
30
-
Add service provider to `config/app.php` in `providers` section
31
-
```php
32
-
CustomD\WebhookRegistry\ServiceProvider::class,
33
-
```
34
-
35
-
### Register Facade
36
-
37
-
Register package facade in `config/app.php` in `aliases` section
Implement the `CustomD\WebhookRegistry\Contracts\ShouldDeliverWebhooks` contract on any event you'd like to trigger a web-hook. This contract requires a `getWebhookPayload` method to be defined on your event, which will provide the payload details.
31
+
### 1. Trigger registered webhooks via events
51
32
52
-
Your payload must define a `body`, but can also define `tags` and `meta` information for passing to `Spatie\WebhookServer\WebhookCall`.
33
+
Implement the `CustomD\WebhookRegistry\Contracts\ShouldDeliverWebhooks` contract on any Laravel Event, and you'll be able to register webhooks that will be fired on this event.
53
34
54
-
```
35
+
36
+
This contract defines several methods that need to be defined on your event, which will help provide the payload details.
37
+
38
+
Method | Purpose
39
+
-----|-----
40
+
`getWebhookPayload` | Returns the payload to send to the Spatie Webhook call. Useful if you want to completely override the payload.
41
+
`getWebhookContext` | Returns the payload `context` field. Useful if you want to set a different property. Defaults to returning the `->context` property from the event. If this property is `Arrayable`, we'll call `->toArray()`. You can overload this method on your event if you want to customise this behaviour.
42
+
`getWebhookEventName` | Returns the event name you want to expose in the payload. Useful for endpoints who recieve multiple different types of event to determine how to handle calls. By default, this is the value of the `->webhookEventName` property from the event, otherwise we fall back to the event class name.
43
+
`shouldDeliverWebhook` | Whether to deliver webhooks for this event. Defaults to true.
44
+
45
+
Your payload must define a `body`, but can also define [tags](https://github.com/spatie/laravel-webhook-server#adding-tags) and [meta](https://github.com/spatie/laravel-webhook-server#adding-meta-information) information for passing to `Spatie\WebhookServer\WebhookCall`.
46
+
47
+
```php
55
48
/**
56
49
* Get the payload for this webhook event
57
50
*/
58
51
public function getWebhookPayload(): array
59
52
{
60
-
$user = request()->user();
61
-
62
53
return [
63
54
'body' => [
64
55
'status' => $this->status,
65
-
'triggered_by' => $user && $user->toArray()
66
56
]
67
57
];
68
58
}
69
59
```
70
60
71
-
Create webhook endpoints uing `CustomD\WebhookRegistry\Model\WebhookEndpoint` or using the facade `WebhookRegistry::registerEndpoint` method, and associate a webhook event to an endpoint with the `CustomD\WebhookRegistry\Model\WebhookEvent` model, or using the facade `WebhookRegistry::registerEvent`.
61
+
### 2. Register webooks
62
+
63
+
Create webhook endpoints direction using the `CustomD\WebhookRegistry\Models\WebhookEndpoint` model, or use the facade `WebhookRegistry::registerEndpoint`.
64
+
65
+
```php
66
+
$endpoint = WebhookRegistry::registerEndpoint(
67
+
'https://webhook.site/custom/endpoint',
68
+
'My webhook endpoint name'
69
+
);
70
+
```
71
+
72
+
By default, we'll verify SSL certificates on outgoing webhook connections. If you want to disable SSL verification, you can pass `false` to the `registerEndpoint` function.
73
+
74
+
```php
75
+
$endpoint = WebhookRegistry::registerEndpoint(
76
+
'https://localhost/webhook-test',
77
+
'My insecure endpoint',
78
+
false
79
+
);
80
+
```
81
+
82
+
### 3. Bind events to an endpoint
83
+
84
+
Associate a webhook event to an endpoint with the `CustomD\WebhookRegistry\Model\WebhookEvent` model, or using the facade `WebhookRegistry::registerEvent`.
Customise the WebhookEvent model in order to add logic about when events should fire. In `config/webhook-registry.php` you will see the model name being used in `models` -> `events`. You can make your own model by implementing the `CustomD\WebhookRegistry\Models\Contracts\WebhookEventContract`.
93
+
94
+
To specify which events are dispatchable in a given run, you must set a `scopeWhereDispatchable` on the model. This scope will be called before firing webhooks to endpoints with this event.
0 commit comments