检测订阅是否自动取消 [英] Detect if subscription is cancelled automatically
问题描述
我已将 Stripe 订阅设置为在 3 次付款尝试失败后自动取消,并且我有 customer.subscription.deleted 网络钩子来记录取消的订阅.
I have setup my Stripe subscriptions to be automatically cancelled after 3 failed payment attempts and I have customer.subscription.deleted webhook to record the cancelled subscription.
是否有办法在 customer.subscription.deleted webhook 中检测订阅是否因付款尝试失败而被 Stripe 取消或通过 Stripe Dashboard 手动取消或因我们的应用程序发出 API 请求而取消?
Is there a way to detect in customer.subscription.deleted webhook if subscription is cancelled by stripe because of failed payment attempts OR manually cancelled through Stripe Dashboard OR cancelled because of an API request made from our application?
推荐答案
您无法区分后两种情况,因为仪表板本身使用 API.
You can't differentiate between the last two cases, as the dashboard itself uses the API.
但是,您可以区分自动取消和手动取消.只需查看 request
属性在 customer.subscription.deleted
事件正文.
However, you can differentiate between automatic and manual cancelations. Simply look at the request
attribute in the customer.subscription.deleted
event's body.
如果订阅在支付失败次数过多后自动取消,则 request
将为空值.
If the subscription was canceled automatically after too many failed payments, then request
will have a null value.
否则,如果通过 API 或仪表板取消订阅,request
将具有非空值:请求 ID ("req_..."
) 的订阅取消请求.
Otherwise, if the subscription was canceled through the API or the dashboard, request
will have a non-null value: the request ID ("req_..."
) of the subscription cancelation request.
正如 Yoni Rabinovitch 指出的那样,如果订阅被 at_period_end=false
(或没有 at_period_end
参数,因为 false
是默认值).
as Yoni Rabinovitch pointed out, the above is true if the subscription was canceled with at_period_end=false
(or no at_period_end
parameter, as false
is the default value).
如果使用 at_period_end=true
取消订阅,则将立即触发 customer.subscription.updated
事件(以反映订阅的 cancel_at_period_end
属性现在为真),并且该事件的 request
将具有订阅取消请求的请求 ID.
If the subscription was canceled with at_period_end=true
, then a customer.subscription.updated
event would be fired immediately (to reflect the fact that the subscription's cancel_at_period_end
attribute is now true), and that event's request
would have the request ID of the subscription cancelation request.
但是,在计费周期结束时实际取消订阅时将发送的 customer.subscription.deleted
事件将具有 request=null
,只是就像付款失败太多后自动取消一样.
However, the customer.subscription.deleted
event that would be sent when the subscription is actually canceled at the end of the billing period would have request=null
, just like an automatic cancelation after too many failed payements.
这篇关于检测订阅是否自动取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!