检测订阅是否自动取消

编程入门 行业动态 更新时间:2024-10-21 03:40:59
本文介绍了检测订阅是否自动取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已将 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.

更多推荐

检测订阅是否自动取消

本文发布于:2023-11-27 09:12:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637448.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!