#nikivi

1 messages ยท Page 1 of 1 (latest)

shy quartzBOT
#

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

tight fossil
#

๐Ÿ‘‹ happy to help

native swift
#

hey

#

so basically i want to update a user plan

#

i store stripe subscription.id in db

#

i think using it i can get the price_id of current subscribed object?

#

and then switch?

tight fossil
#

please read through the link I sent above

#

that explains how to update a customer's subscription

native swift
#

CUSTOMER_ID

#

mm

tight fossil
#

after reading it if you have further questions just let me know

native swift
#

i only have sub id

tight fossil
#

did you read the doc?

native swift
#

reading now

#

just in retrieve identifiers

#

its already something i cant do now

tight fossil
#

if you have the subscription ID you can skip step 1

native swift
#

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

native swift
#

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

tight fossil
#

you can first retrieve the subscription

native swift
#
        const subscription = await stripe.subscriptions.retrieve(
          checkoutSessionCompleted.subscription,
        )
#

yea i do that here

tight fossil
#

once you do, you can get the subcription item ID

native swift
#

have that too

#

subscription.id

tight fossil
#

that's the subscription ID

#

each subscription could have multiple items

native swift
#

its quite annoying i cant get typed events from stripe

tight fossil
native swift
#

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

tight fossil
native swift
#

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

pure axle
#

Hi! I'm taking over this thread.

#

Give me a few minutes to catchup.

native swift
#

thank you

pure axle
#

Is your goal to update the price of an existing Subscription?

native swift
#

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

pure axle
#

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

native swift
#

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

pure axle
#

this button istead of creating a checkout
will do stripe api call to upgrade
Yes correct!

native swift
pure axle
#

at that point i only have stripeSubscriptionId
That's enought. With the Subscription ID you cna update the subscription.

native swift
#

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

pure axle
native swift
#

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

pure axle
#

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

native swift
#

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

#

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

pure axle
#

No. It's in subscription.items.data[0].id, assuming you have a single subscription item

native swift
#

im confused

#

subscription item means that user has one recurring plan?

pure axle
#

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.

native swift
#
si_Os5pLYG04W2YCv" "sub id"
#

yep

pure axle
#

Great, now you use that ID to udpate the Price of the Subscription.

native swift
#

i hard code the price as you can only upgrade from month to year plan

#

and that price id is static i think

pure axle
#

yep, that should work

native swift
#

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?

pure axle
#

If you switch from monthly to yearly, yes.

#

I recommend doing some test in test mode to see how that works

native swift
#

will do

#

thank you

pure axle
#

Happy to help ๐Ÿ™‚

native swift
#

if i may ask in this thread

#

im trying to fix this thing now

#

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

pure axle
#

im trying to fix this thing now
What "thing"? What are you trying to do and what is the issue?

native swift
#

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

native swift
#

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

pure axle
#

You mean when you receieve the checkout.session.completed event, you want to know the price/product bought?

native swift
#

yes

pure axle
native swift
#

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

#

is it items here?

pure axle
native swift
#

const subscriptionItemId = subscription.items.data[0].id

#

but priceId i guess

#

or wait

pure axle
#

const priceID = subscription.items.data[0].price

#

Please, try to more carefully read the links I share with you. They contain all the information you need.