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.
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?
Open the Settings app. Tap your name. Tap Subscriptions. Tap the subscription.
When canceled, all future billing will be disabled and you will no longer be charged on the billing date set by your plan.
You can't differentiate between the last two cases, as the dashboard itself uses the API.
However, you can differentiate between automatic and manual cancelations. Simply look at the request
attribute in the customer.subscription.deleted
event's body.
If the subscription was canceled automatically after too many failed payments, then request
will have a null value.
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.
EDIT: 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).
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.
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.
If you instead cancel a subscription at the end of the billing period, a customer.subscription.updated event is immediately triggered. That event reflects the change in the subscription’s cancel_at_period_end value. When the subscription is actually canceled at the end of the period, a customer.subscription.deleted event then occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With