Stripe Checkout webhook - 监听哪个事件

Stripe Checkout webhook - which event to listen for

背景:

我已经实施 Stripe Checkout in a React app, and a webhook 成功侦听事件。

这些是 Webhook 在成功结帐后听到的事件:

checkout.session.completed
charge.succeeded
payment_method.attached
customer.created
customer.updated
invoice.created
invoice.finalized
customer.subscription.created
invoice.updated
customer.subscription.updated
invoice.paid
invoice.payment_succeeded
payment_intent.succeeded
payment_intent.created

我想做什么:

我需要在客户成功创建订阅后将一个值保存到数据库中。此值有助于启用付费功能,因此我必须确保 webhook 捕获所有成功的事件。


问题:

我能否始终只依赖其中一个 Webhook 事件来指示成功?如果不是,哪些是最重要的?

我认为对我的用例来说最重要的事件可能是 checkout.session.completedcharge.succeededpayment_intent.succeeded,但我不太确定它们有何不同 if/when有些可能不会出现

docs表示:

There are three ways to accept payments on Stripe today:

  • Stripe Checkout
  • Charges API
  • Payment Intents API

and:

  • checkout.session.completed: Occurs when a Checkout Session has been successfully completed.
  • charge.succeeded: Occurs whenever a charge is successful.
  • payment_intent.succeeded: Occurs when a PaymentIntent has successfully completed payment.

所以我的猜测是checkout.session.completedcharge.succeededpayment_intent.succeeded都表示成功,三个都返回的原因是对应当前的三种方式在 Stripe 上接受付款。

如果是这种情况,我是否可以可靠地仅监听 checkout.session.completed 以指示成功?


其他信息:

stripe listen --forward-to localhost:4242/myWebHook 结帐会话成功后的输出:

(为简单起见替换了实际 evt_ 值)

2022-05-08   --> checkout.session.completed [evt_123456]
2022-05-08   --> charge.succeeded [evt_123456]
2022-05-08   --> payment_method.attached [evt_123456]
2022-05-08   --> customer.created [evt_123456]
2022-05-08   --> customer.updated [evt_123456]
2022-05-08   --> invoice.created [evt_123456]
2022-05-08   --> invoice.finalized [evt_123456]
2022-05-08   --> customer.subscription.created [evt_123456]
2022-05-08   --> invoice.updated [evt_123456]
2022-05-08   --> customer.subscription.updated [evt_123456]
2022-05-08   --> invoice.paid [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   --> invoice.payment_succeeded [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   <--  [200] POST http://localhost:4242/myWebHook  [evt_123456]
2022-05-08   --> payment_intent.succeeded [evt_123456]
2022-05-08   <--  [200] POST http://localhost:3000/myWebHook  [evt_123456]
2022-05-08   --> payment_intent.created [evt_123456]
2022-05-08   <--  [200] POST http://localhost:3000/myWebHook  [evt_123456]

大多数用户通常为成功创建订阅而监听的事件是:

没有单一的正确答案,因为您想要收听哪个事件取决于多种因素,例如

  • 如果您正在侦听的事件包含您需要的信息
  • 对于 checkout.session.completed,如果您只接受卡支付(没有异步支付方式),那么是的,您可以依靠 checkout.session.completed 在客户之后将值保存到数据库成功创建订阅。但是,如果您确实也接受异步支付方式,您可能希望监听和处理此处列出的其他事件:https://stripe.com/docs/payments/checkout/fulfill-orders#delayed-notification
  • 一些用户希望将所有已支付的发票(包括订阅续订的发票)保存到他们的数据库中。在这种情况下,监听 invoice.paid 事件可能更有意义。

有理由不使用 charge.succeededpayment_intent.succeeded :

  • 如果发票金额=0,例如试验,将不会创建 PaymentIntent / Charge。
  • 如果您将发票标记为带外支付,则 PaymentIntent 将被取消,您将永远不会收到这两个事件。

我建议通读 https://stripe.com/docs/billing/subscriptions/overview 以更好地了解订阅的工作原理。