Commit 4179bf3
fix(plugin-stripe): supports webhooks within serverless environments (#10890)
Fixes #10639. When using the Stripe Plugin within a serverless
environment, the process that handles running webhooks will immediately
close before these webhooks finish executing.
The reason for this is that the webhook handler that this plugin opens
processes webhooks asynchronously. This is because Stripe places a
timeout on these requests, and if the request takes longer than 10-20
seconds to respond with a 2xx status code, it assumes that the event has
failed and will retry at a later date. Stripe expects an immediate
response from your application. If your webhooks interact with the
database or perform other slow API requests, for example, this can lead
to multiple, duplicative events, and potentially data inconsistencies.
From the Stripe docs:
> Your endpoint must quickly return a successful status code (2xx) prior
to any complex logic that could cause a timeout. For example, you must
return a 200 response before updating a customer’s invoice as paid in
your accounting system.
Reference:
https://docs.stripe.com/webhooks#acknowledge-events-immediately
For Vercel specifically, the fix here is to conditionally import
`waitUntil` from the `@vercel/functions` package. This will add without
affecting other serverless environments.
If you're on Vercel, you can install the `@vercel/functions` package.
When detected, this plugin wraps all webhook handlers with the
`waitUntil` function provided by this module. This will process the
function in another thread that stays open even after the webhook
handler has sent its response.
From the Vercel docs:
> The waitUntil() method enqueues an asynchronous task to be performed
during the lifecycle of the request. You can use it for anything that
can be done after the response is sent, such as logging, sending
analytics, or updating a cache, without blocking the response from being
sent.
Reference:
https://vercel.com/docs/functions/functions-api-reference#waituntil
---------
Co-authored-by: Paul Popus <paul@payloadcms.com>1 parent b44135e commit 4179bf3
File tree
3 files changed
+73
-22
lines changed- docs/plugins
- packages/plugin-stripe/src
- routes
- webhooks
3 files changed
+73
-22
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
181 | 203 | | |
182 | 204 | | |
183 | 205 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 45 | + | |
| 46 | + | |
56 | 47 | | |
57 | 48 | | |
58 | 49 | | |
59 | 50 | | |
60 | 51 | | |
61 | 52 | | |
62 | 53 | | |
63 | | - | |
64 | 54 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
69 | 58 | | |
70 | 59 | | |
71 | 60 | | |
| |||
74 | 63 | | |
75 | 64 | | |
76 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
77 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
78 | 107 | | |
79 | 108 | | |
80 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
0 commit comments