#sree-subscription-events
1 messages · Page 1 of 1 (latest)
Hello! Can you be more specific? What are you trying to do?
Basic - $0.15 per unit/month (metered) (product 1)
Standard - $30/month (licensed) (product 2)
Premium - $50/month (licensed) (product 3)
By default every user of ours has the default basic plan (upon account creation) and if the user decides to upgrade to standard/premium plan I'd like to add the relative product id (lets say product 2 id) to the existing default basic plan using subscription items api
Okay. How do webhooks tie into what you want to do?
and if the user decides to downgrade from the standard plan, use the same subscription items api to remove it from the basic plan subscription
Im gonna use webhooks to make reltive changes in our db
if product 3 is added to the basic subscription then mark the user as a premium user
if user cancels the premium subscription, then use webhooks to mark the user as not a premium user
Ah, gotcha. You likely want to listen to customer.subscription.updated then: https://stripe.com/docs/api/events/types#event_types-customer.subscription.updated
okay
how do i know what triggered the above event?
"subscription item added" or "subscription item deleted"
is there a particular key/value i could use to detect what caused the event trigger?
You can look in the Event's previous_attributes: https://stripe.com/docs/api/events/object#event_object-data-previous_attributes
okay
sree-subscription-events
Is there any other way i could do the upgrading/downgrading of subcriptions? (without using subscription items)
No
uh oh
Could you please be a bit more specific on how to use "previous_attributes" to detect what trigged the "customer_subscription_updated" event?
My advice is to test this end to end. Create a Subscription, update it to add a second Price, look at the customer.subscription.updated Event and it will be clear immediately
case "customer.subscription.updated":
let plan;
if (data.object.plan !== null && typeof data.object.plan !== 'undefined') {
plan = data.object.plan.id;
}
if(plan === "price_1NEZjCK2O6n9E7HHSM6NddLd"){
console.log("basic");
} else{
console.log("premium")
}
break;
Ive tried it just now...
and it emitts this code:
how do i differntiate between adding/removing a subscription item?
please don't dump an entire object like that.
You need to carefully look at the raw JSON for example in the Dashbaord and then look at previous_attributes to see what happened
ok my bad
Is there an example repo that i could refer to?
No there is not sorry
I don't understand what the problem is exactly. If you trigger this behaviour in Test mode then you will immediately see the example Event. What isn't working?
I see the example event
Ive tested it multiple times with the API, Stripe Cli and from the dashboard
Sure but what is the exact issue. Let's focus on this as developers together. You want to see the new Price, we told you where to look, you are confused. Can you explain what part is not working exactly?
new price? whats that? Ive never asked anything about that
you did, you literally asked how to add a second or third Price to a Subscription here
you should read the full convo
I did
All I was asking is if there's way to detect what caused the "customer.subscription.updated" event?
whether it was the adding of a subscription item or removing one?
I understand that part, that's what my colleague and I tried to explain
so that i could make relative changes to my users info
Can you share an exact Event id so that I can show you what you are missing?
yep one moment please
No worries!
Event ID: evt_1NIJlZK2O6n9E7HHu8nj5tvT
Okay so you can see the Event in the Dashboard here: https://dashboard.stripe.com/test/events/evt_1NIJlZK2O6n9E7HHu8nj5tvT
If you look at it, you can see (partially truncated for readability: { object: { id: "sub_123", object: "subscription", items: { object: "list", data: [ { id: "si_123", object: "subscription_item", ... price: { id: "price_1NEZjCK2O6n9E7HHSM6NddLd", object: "price", ... }, subscription: "sub_123", }, { id: "si_456", object: "subscription_item", price: { id: "price_1NEZlSK2O6n9E7HH6ApQZlPY", object: "price", }, ... } ],... }, ... }, previous_attributes: { items: { data: [ { id: "si_123", object: "subscription_item", ... price: { id: "price_1NEZjCK2O6n9E7HHSM6NddLd", object: "price", ... } } ], }, ... } }
So what that tells you is "before, my Subscription had only one Price in the items sub-list. Now it has 2. And you can compare what it had before, the si_123 with the Price price_1NEZjCK2O6n9E7HHSM6NddLd with what it has now which is that one plus the si_456 with the Price price_1NEZlSK2O6n9E7HH6ApQZlPY
Okay Thank you for the detailed explanation 👌
And that’s it.. that’s what I was wondering about
Does it make sense? Sorry I was trying to avoid sharing your real Event data since you see how big it gets even after truncating
but overall you look at what's in previous_attributes and you compare it to what's in the object itself to see what it was before vs what it is now
So compare previous_attributes to current to detect and make changes accordingly
Yeah it does and thank you
It would be nice to have dedicated events for subscriptions items similar to what subscriptions has
yeah I don't think it'd really make sense because if you add multiple items, reconciling those Events would be way harder if they weren't all together
Subscription.created, subscription.removed and etc
if you add 3 and remove 2 you'd get 5 separate Events out of order and that'd be impossible to handle
Or just show all added items in one event (subitems.created) no?
Same for removed
Would it still be hard to manage?
That just wouldn't work. You're literally describing what we do here: a Subscription update aggregating all the changes at once