Stripe webhook 最佳实践

Stripe webhook best practice

收到 webhook 后,我们应该 return 2xx 响应,然后再在后端执行一些额外的逻辑。

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.

他们在这里建议首先 return 成功 2xx 响应,然后在我们的后端做额外的事情。他们认为这是最佳实践,因此不会出现请求超时。

我不明白这种最佳做法如何,因为很明显我们的系统可能无法更新数据库,例如,我们已经 return 成功响应。这意味着 Stripe 稍后将不会尝试重新发送 webhook,并且我们将丢失此信息,因为我们的系统没有保存它。

是否有针对此类问题的现有解决方案?

我联系了 Stripe 支持,他们说 webhook 请求的超时是 20 秒。

The default timeout is 20 seconds. To determine if it's a complex query you would want to evaluate how long would writing to your DB take.

我在文档中找不到此信息,所以它真的很有帮助。 20 秒足以将某些内容保存到数据库和 return 2xx 响应。