且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测订阅是否自动取消

更新时间:2023-11-29 15:52:10

您无法区分后两种情况,因为仪表板本身使用 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.