#sree-subscription-events

1 messages · Page 1 of 1 (latest)

tender ploverBOT
dry tundra
#

Hello! Can you be more specific? What are you trying to do?

toxic totem
#

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

dry tundra
#

Okay. How do webhooks tie into what you want to do?

toxic totem
#

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

dry tundra
toxic totem
#

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?

dry tundra
toxic totem
#

okay

tender ploverBOT
pulsar bone
#

sree-subscription-events

toxic totem
#

Is there any other way i could do the upgrading/downgrading of subcriptions? (without using subscription items)

pulsar bone
#

No

toxic totem
#

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?

pulsar bone
#

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

toxic totem
#

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;

pulsar bone
#

that code would never work no

#

please try it first

toxic totem
#

Ive tried it just now...

#

and it emitts this code:

#

how do i differntiate between adding/removing a subscription item?

pulsar bone
#

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

toxic totem
#

Is there an example repo that i could refer to?

pulsar bone
#

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?

toxic totem
#

I see the example event

#

Ive tested it multiple times with the API, Stripe Cli and from the dashboard

pulsar bone
#

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?

toxic totem
#

new price? whats that? Ive never asked anything about that

pulsar bone
toxic totem
pulsar bone
#

I did

toxic totem
#

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?

pulsar bone
#

I understand that part, that's what my colleague and I tried to explain

toxic totem
#

so that i could make relative changes to my users info

pulsar bone
#

Can you share an exact Event id so that I can show you what you are missing?

toxic totem
#

yep one moment please

pulsar bone
#

No worries!

toxic totem
#

Event ID: evt_1NIJlZK2O6n9E7HHu8nj5tvT

pulsar bone
#

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

toxic totem
#

Okay Thank you for the detailed explanation 👌

#

And that’s it.. that’s what I was wondering about

pulsar bone
#

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

toxic totem
#

So compare previous_attributes to current to detect and make changes accordingly

toxic totem
#

It would be nice to have dedicated events for subscriptions items similar to what subscriptions has

pulsar bone
#

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

toxic totem
#

Subscription.created, subscription.removed and etc

pulsar bone
#

if you add 3 and remove 2 you'd get 5 separate Events out of order and that'd be impossible to handle

toxic totem
#

Same for removed

#

Would it still be hard to manage?

pulsar bone
#

That just wouldn't work. You're literally describing what we do here: a Subscription update aggregating all the changes at once

toxic totem
#

Oh okay

#

Thanks again for the explanation