使用 cancel_at_period_end true 检测 Stripe 订阅的自动取消

Detect automatic cancellation of Stripe subscription with cancel_at_period_end true

在我的 Stripe-Connect 应用程序中,我想处理 Stripe customer.subscription.deleted 尝试付款失败后自动取消订阅时生成的 webhook(基于失败付款的订阅设置)。

Stripe documentation 指出: "You can see that a subscription was canceled automatically–as opposed to by your request–if the customer.subscription.deleted event’s request property is null." 另见

但是,如果订阅 cancel_at_period_end 设置为 "true",即使订阅结束 [=23],customer.subscription.deleted 事件的请求 属性 似乎也是空的=] 在期末,而不是由于付款尝试失败。

所以,我的问题是,我如何检测到 cancel_at_period_end 设置为 true 的订阅因付款尝试失败而被取消?

很遗憾,目前无法区分这两种情况。我的建议是在您这一端存储您已将订阅标记为在当前期间结束时取消。这样当你收到 customer.subscription.deleted 事件时,你可以根据你这边的标志检查它是否来自失败的支付。

另一个解决方案是查看该订阅的最新发票。您可以查看 attempt_count 属性 以查看对其进行了多少次重试,并推断取消是否来自上次尝试。