#nikivi
1 messages ยท Page 1 of 1 (latest)
Hello nikivi, we'll be with you shortly! Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
โข nikivi, 0 days ago, 68 messages
๐ happy to help
hey
so basically i want to update a user plan
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
i store stripe subscription.id in db
i think using it i can get the price_id of current subscribed object?
and then switch?
please read through the link I sent above
that explains how to update a customer's subscription
after reading it if you have further questions just let me know
i only have sub id
did you read the doc?
if you have the subscription ID you can skip step 1
and go straight to https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#changing
yea this cant do too
SUB_ITEM_ID
reading now
if it says how to get it
because i only have sub id now
or actually
im confused though
items: [
{
id: '{{SUB_ITEM_ID}}',
price: '{{NEW_PRICE_ID}}',
},
],
}
i would think that given a sub id
i can specify the api id
to update to
but it also needs price id strangely
this is test api id
the thing i want is
say user has monthly plan
when they update to new plan via stripe checkout
it should essentially switch their plan
and add the timeUntil on top of what they have with monthly plan remaining
inside the web hook
which in my case looks like this now
you can first retrieve the subscription
const subscription = await stripe.subscriptions.retrieve(
checkoutSessionCompleted.subscription,
)
yea i do that here
once you do, you can get the subcription item ID
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
but thats beside the point i guess
how can i check there is an active subscription
i only want to do this update if there is active
because at the point this web hook gets called i currently dont know that
current_period_end
i guess by checking this is not undefined
you can always check the status https://stripe.com/docs/api/subscriptions/object#subscription_object-status
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
im confused though
so say user first does checkout
for month plan
then they do a checkout for year plan
how does it know thats the same user doing the checkout
by email?
it just returned active
for email that never got entered before
console.log(subscription.status, "status")
how i create my checkouts
right now im testing with this
using stripe listen --forward-to localhost:8787/learn-anything-bought
but status is always active
i dont get why
i wanted here in web hook code
to check if status is active
and if yes, check if its a year plan
if yes, update that user to year plan
and update endDate accordingly
so it considers remaining time of existing plan + the year plan they just got
if that makes sense
if (subscription.status === "active") {
await stripe.subscriptions.update(subscription.id, {
items: [
// {
// id: "{{SUB_ITEM_ID}}",
// price: "{{NEW_PRICE_ID}}",
// },
],
})
}
need this i think
but again
my status is always active now
super strange
also i see that items exists on subscription object
thank you
Is your goal to update the price of an existing Subscription?
correct
inside a web hook i have
switch (event.type) {
case "checkout.session.completed":
for reference
say user has an active plan
if they switch to year plan
it should switch plan and endDate is year + whatever is there left for current month one
right now i do this
when user presses a button to pay
const monthSubscription = await stripe.checkout.sessions.create({
success_url: process.env.STRIPE_SUCCESS_URL!,
mode: "subscription",
metadata: {
userEmail: args.userEmail
},
line_items: [
{
quantity: 1,
price: process.env.STRIPE_MONTH_SUBSCRIPTION!
}
]
})
is for month checkout plan
and one for year
once that succeeds
web hook gets called
in there i update some database state like save stripeSubscriptionId + memberUntil date
but i dont have ability to switch subscriptions
Wait. If you create a brand new Subscription, then using Checkout Session makes a lot of sense.
However if you want to update an existing Subscription, then you can't use Checkout Session. Instead you change the Price of the existing Subscription following this guide: https://stripe.com/docs/billing/subscriptions/upgrade-downgrade
oh ok
so basically when user is on monthly plan
this button istead of creating a checkout
will do stripe api call to upgrade
user won't be charged with 60$ immediately
or will they
im confused
i started making endpoint for it here
but its unclear how to make it work
at that point i only have stripeSubscriptionId
this button istead of creating a checkout
will do stripe api call to upgrade
Yes correct!
at that point i only have stripeSubscriptionId
That's enought. With the Subscription ID you cna update the subscription.
im confused how to do this
i can see api id
but i dont know where to see sub_item_id
or wait
let me try this
does this look right?
upgradeStripeMonthlyPlanToYear returns the subscriptionId object
i have stored in db
im not sure about part in items:
price_1O0mg74soP2HpBfdjnm593F6
I'm not sure the subscription item is in the dashboard, but you can find it in the API.
- retrieve the Subscription object with its ID using https://stripe.com/docs/api/subscriptions/retrieve?lang=node
- Then get the Subscription Item ID (
si_xxx) from here: https://stripe.com/docs/api/subscriptions/object?lang=node#subscription_object-items-data-id
this is 6$/month price id
price_1O0mgN4soP2HpBfdFkfIl9Bl is 60$/year price id
oh ok
mm
let me try via api
the thing is that my prices are fixed
i dont mind hard coding it
like in my eyes
No your code is wrong. The items.id should be a SubscriptionItem ID (si_xxx).
You can't hard code the si_xxx, it changes for each Subscription. But yes you can hard code the Price ID
ok trying to get SubscriptionItem from my subscriptionId
const stripeSubscriptionObjectId =
await upgradeStripeMonthlyPlanToYear(hankoId)
const subscription = await stripe.subscriptions.retrieve(
// @ts-ignore
stripeSubscriptionObjectId?.stripeSubscriptionObjectId
)
console.log(
stripeSubscriptionObjectId?.stripeSubscriptionObjectId,
"sub id"
)
console.log(subscription)
console.log(subscription.items.find)
wait so i have this now
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
im unclear
i need to do it with .find?
is whats returned
and i need Subscription Item ID ๐ค
i dont see it
or wait
ok so i guess its .find
on the price_id
No. It's in subscription.items.data[0].id, assuming you have a single subscription item
you can console.log subscription.items.data[0].id, and you should see the si_xxx ID.
Subscription Items are all the price for that Subscription yes.
Great, now you use that ID to udpate the Price of the Subscription.
i hard code the price as you can only upgrade from month to year plan
and that price id is static i think
yep, that should work
ok so then when this happens
say user paid for month plan
5 days passed
his plan ends in november
they decide to upgrade
above endpoint gets called
their plan switches
will they instantly get billed 60$ on card
and enddate unix will be increased to 1 year + 25 days?
If you switch from monthly to yearly, yes.
I recommend doing some test in test mode to see how that works
And alos read this link I shared before in details https://stripe.com/docs/billing/subscriptions/upgrade-downgrade
Happy to help ๐
if i may ask in this thread
im trying to fix this thing now
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
reading through
in web hook i need to know what plan is currently in subscription
i guess i can compare it by price_1O0mg74soP2HpBfdjnm593F6 one of these
im trying to fix this thing now
What "thing"? What are you trying to do and what is the issue?
so in my web hook
i added to my User table
# date until user has paid membership for
memberUntil: datetime;
# month/year
stripePlan: str;
# after stripe payment works, you get back subscription object id (can be used to cancel subscription)
stripeSubscriptionObjectId: str;
# whether user has stopped subscription and won't be be charged again
subscriptionStopped: bool;
are the values i store related to stripe
stripePlan says what plan user is currently on
this code runs as web hook on success of checkout
i can do this of course
but i think i should just be able to get it from stripe to know what user has actually bought, month or year plan
hope that makes sense
You mean when you receieve the checkout.session.completed event, you want to know the price/product bought?
yes
You have two options:
- The Checkout Session contains a
subscriptionparameter. Use that to retrieve the Susbcription and check it's price/product - Or re-retrieve the Checkout Session with
expand: ["line_items"], and in the response you will get access to a new property calledline_itemswith the information you need: https://stripe.com/docs/api/checkout/sessions/object?lang=node#checkout_session_object-line_items
for 1.
const subscription = await stripe.subscriptions.retrieve(
checkoutSessionCompleted.subscription,
)
i think i already do this
which is what you meant
i now need to check price/product
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
is it items here?
Yep, once you have the Subscripiton object, check the Subscription Item (like earlier) to find the Price ID https://stripe.com/docs/api/subscriptions/object?lang=node#subscription_object-items-data-price