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
A webhook is a way for an app to provide information to another app about a specific event. The way the two apps communicate is with a simple HTTP request.
9
+
A webhook is a way for an app to provide information to another app about a specific event. The way the two apps communicate is with a simple HTTP request.
10
10
11
11
This package allows you to receive webhooks in a Laravel app. It has support for [verifying signed calls](#verifying-the-signature-of-incoming-webhooks), [storing payloads and processing the payloads](#storing-and-processing-webhooks) in a queued job.
12
12
13
13
If you need to send webhooks, take a look at our [laravel-webhook-server](https://github.com/spatie/laravel-webhook-server) package.
14
14
15
15
## Support us
16
16
17
-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
17
+
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
18
18
19
19
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
* The classname of the model to be used to store call. The class should be equal
74
79
* or extend Spatie\WebhookClient\Models\WebhookCall.
@@ -87,7 +92,7 @@ return [
87
92
88
93
In the `signing_secret` key of the config file, you should add a valid webhook secret. This value should be provided by the app that will send you webhooks.
89
94
90
-
This package will try to store and respond to the webhook as fast as possible. Processing the payload of the request is done via a queued job. It's recommended to not use the `sync` driver but a real queue driver. You should specify the job that will handle processing webhook requests in the `process_webhook_job` of the config file. A valid job is any class that extends `Spatie\WebhookClient\ProcessWebhookJob` and has a `handle` method.
95
+
This package will try to store and respond to the webhook as fast as possible. Processing the payload of the request is done via a queued job. It's recommended to not use the `sync` driver but a real queue driver. You should specify the job that will handle processing webhook requests in the `process_webhook_job` of the config file. A valid job is any class that extends `Spatie\WebhookClient\ProcessWebhookJob` and has a `handle` method.
91
96
92
97
### Preparing the database
93
98
@@ -122,13 +127,13 @@ protected $except = [
122
127
123
128
## Usage
124
129
125
-
With the installation out of the way, let's take a look at how this package handles webhooks. First, it will verify if the signature of the request is valid. If it is not, we'll throw an exception and fire off the `InvalidSignatureEvent` event. Requests with invalid signatures will not be stored in the database.
130
+
With the installation out of the way, let's take a look at how this package handles webhooks. First, it will verify if the signature of the request is valid. If it is not, we'll throw an exception and fire off the `InvalidSignatureEvent` event. Requests with invalid signatures will not be stored in the database.
126
131
127
132
Next, the request will be passed to a webhook profile. A webhook profile is a class that determines if a request should be stored and processed by your app. It allows you to filter out webhook requests that are of interest to your app. You can easily create [your own webhook profile](#determining-which-webhook-requests-should-be-stored-and-processed).
128
133
129
134
If the webhook profile determines that request should be stored and processed, we'll first store it in the `webhook_calls` table. After that, we'll pass that newly created `WebhookCall` model to a queued job. Most webhook sending apps expect you to respond very quickly. Offloading the real processing work allows for speedy responses. You can specify which job should process the webhook in the `process_webhook_job` in the `webhook-client` config file. Should an exception be thrown while queueing the job, the package will store that exception in the `exception` attribute on the `WebhookCall` model.
130
135
131
-
After the job has been dispatched, the controller will respond with a `200` status code.
136
+
After the job has been dispatched, the request will be passed to a webhook response. A webhook response is a class that determines the HTTP response for the request. An 'ok' message response with `200` status code is returned by default, but you can easily create [your own webhook response](#creating-your-own-webhook-response).
132
137
133
138
### Verifying the signature of incoming webhooks
134
139
@@ -142,7 +147,7 @@ If the `$computedSignature` does match the value, the request will be [passed to
142
147
143
148
### Creating your own signature validator
144
149
145
-
A signature validator is any class that implements `Spatie\WebhookClient\SignatureValidator\SignatureValidator`. Here's what that interface looks like.
150
+
A signature validator is any class that implements `Spatie\WebhookClient\SignatureValidator\SignatureValidator`. Here's what that interface looks like.
146
151
147
152
```php
148
153
use Illuminate\Http\Request;
@@ -154,7 +159,7 @@ interface SignatureValidator
154
159
}
155
160
```
156
161
157
-
`WebhookConfig` is a data transfer object that lets you easily pull up the config (containing the header name that contains the signature and the secret) for the webhook request.
162
+
`WebhookConfig` is a data transfer object that lets you easily pull up the config (containing the header name that contains the signature and the secret) for the webhook request.
158
163
159
164
After creating your own `SignatureValidator` you must register it in the `signature_validator` in the `webhook-client` config file.
160
165
@@ -183,9 +188,9 @@ After creating your own `WebhookProfile` you must register it in the `webhook_pr
183
188
184
189
### Storing and processing webhooks
185
190
186
-
After the signature is validated and the webhook profile has determined that the request should be processed, the package will store and process the request.
191
+
After the signature is validated and the webhook profile has determined that the request should be processed, the package will store and process the request.
187
192
188
-
The request will first be stored in the `webhook_calls` table. This is done using the `WebhookCall` model.
193
+
The request will first be stored in the `webhook_calls` table. This is done using the `WebhookCall` model.
189
194
190
195
Should you want to customize the table name or anything on the storage behavior, you can let the package use an alternative model. A webhook storing model can be specified in the `webhook_model`. Make sure you model extends `Spatie\WebhookClient\Models\WebhookCall`.
191
196
@@ -203,13 +208,32 @@ class ProcessWebhookJob extends SpatieProcessWebhookJob
203
208
public function handle()
204
209
{
205
210
// $this->webhookCall // contains an instance of `WebhookCall`
206
-
211
+
207
212
// perform the work here
208
213
}
209
214
}
210
215
```
211
216
212
-
You should specify the class name of your job in the `process_webhook_job` of the `webhook-client` config file.
217
+
You should specify the class name of your job in the `process_webhook_job` of the `webhook-client` config file.
218
+
219
+
### Creating your own webhook response
220
+
221
+
A webhook response is any class that implements `\Spatie\WebhookClient\WebhookResponse\WebhookResponse`. This is what that interface looks like:
222
+
223
+
```php
224
+
namespace Spatie\WebhookClient\WebhookResponse;
225
+
226
+
use Illuminate\Http\Request;
227
+
use Spatie\WebhookClient\WebhookConfig;
228
+
229
+
interface WebhookResponse
230
+
{
231
+
public function respondToValidWebhookRequest(Request $request, WebhookConfig $config);
232
+
}
233
+
```
234
+
235
+
After creating your own `WebhookResponse` you must register it in the `webhook_response` key in the `webhook-client` config file.
236
+
213
237
214
238
### Handling incoming webhook request for multiple apps
@@ -28,6 +29,13 @@ public static function invalidWebhookProfile(string $webhookProfile): InvalidCon
28
29
returnnewstatic("`{$webhookProfile}` is not a valid webhook profile class. A valid web hook profile is a class that implements `{$webhookProfileInterface}`.");
returnnewstatic("`{$webhookResponse}` is not a valid webhook response class. A valid webhook response is a class that implements `{$webhookResponseInterface}`.");
0 commit comments