How to determine when a subscription cycles using webhooks
Subscriptions items can cycle at different intervals using the current_period_start
and current_period_end
parameters. For example, one item can cycle monthly and another annually. Depending on how you're currently listening to customer.subscription.updated
webhook events, you may need to update your integration to determine if an event was for a subscription cycling.
Determine when a subscription cycles
When a subscription cycles to a new billing period, Stripe generates a customer.subscription.updated event. However, not all subscription update events indicate a cycle. To reliably determine if a subscription has cycled, examine specific changes in the event's data.
A subscription has cycled if all the following conditions are true in the customer.subscription.updated
event:
- The event's
previous_attributes
contains a change to thelatest_invoice
field. - When the
items
array is present inprevious_attributes
, for each item:- The item's
current_period_end
inprevious_attributes
matches its newcurrent_period_start
. - The new
current_period_start
is later than thecurrent_period_start
inprevious_attributes
.
- The item's
Common pitfalls to avoid
- Don't assume all items cycle together.
- Don't only check the first item in the subscription.
- Don't skip checking the
latest_invoice
field. - Don't rely only on checking if
items
exists inprevious_attributes
.
Did this answer your question?
Related articles