I am listening to Stripe customer.subscription.updated
events which works fine.
When I downgrade a subscription then I create a scheduleSubscription
to downgrade the subscription at the end of the billing period.
My problem is that the customer.subscription.updated
event fires when the scheduleSubscription
is created and when the subscription is really downgraded at the end of period. I just want to process the 2nd event (subscriptopn downgrade at the period end).
Is there anything in the event object that could tell me that the event is only for the creation of scheduleSubscription
(I don't need) or real downgrade (I want to process it)?
Events with the type *.updated
in Stripe's API will have the data[previous_attributes]
property which tells you what exactly changed on the object that is described by the Event.
So in your example, the first customer.subscription.updated
would have
previous_attributes: {
schedule: null
}
to indicate that property changed and was null before, and you can see the whole Subscription after the update and it would have schedule: 'sub_sched_123'
in it.
Then when the schedule's first phase ends and the next one starts the previous_attributes
would cover the items
property since its content is changing as you're swapping to a new Price id for the downgrade. So you can check the absence/presence of data[previous_attributes][items]
and its content to figure out what changed on your Subscription and handle it that way.