#vajhatz

1 messages ยท Page 1 of 1 (latest)

snow heronBOT
frank kindle
#

๐Ÿ‘‹ happy to help

vast grove
#

Hey! ๐Ÿ™‚

frank kindle
#
items: [
      {
        id: newPriceID, // This is the new priceID 
        quantity,
      },```
this is wrong
#

the id here is the Subscription Item ID

vast grove
#

( Just a sec, looking at the code I have to see if I understand correctly, thanks for the patience! ๐Ÿ™‚ )

#

export const updateSubscription = async (
  { subscriptionID, newPriceID, quantity }: UpdateSubscriptionParams,
  stripeClient: Stripe
) => {
  let subscription = await stripeClient.subscriptions.retrieve(subscriptionID)

  const planItems = subscription.items.data
  if (planItems.length === 0) {
    throw new InternalServerError({
      message: `No plan items found for subscription ${subscriptionID}`,
    })
  }
  if (planItems.length > 1) {
    throw new InternalServerError({
      message: `Multiple plan items found for subscription ${subscriptionID}`,
    })
  }

  const planItem = planItems.find((i) => i.price.id === newPriceID)
  if (!planItem) {
    subscription = await stripeClient.subscriptions.update(subscription.id, {
      proration_behavior: 'always_invoice',
      items: [
        {
          id: planItems[0].id,
          quantity,
          price: newPriceID,
        },
      ],
      payment_behavior: 'pending_if_incomplete',
      expand: ['latest_invoice.payment_intent'],
    })
  } else {
    subscription = await stripeClient.subscriptions.update(subscription.id, {
      proration_behavior: 'always_invoice',
      items: [
        {
          id: planItem.id,
          quantity,
        },
      ],
      payment_behavior: 'pending_if_incomplete',
      expand: ['latest_invoice.payment_intent'],
    })
  }

  return subscription
}
frank kindle
#

const planItem = planItems.find((i) => i.price.id === newPriceID)
don't you mean the oldPrice ID?

vast grove
#

That's sort of how I'm figuring out that the price_id has changed. Because the subscription doesn't have it in the list of items.

frank kindle
#

oh sorry my bad!

#

I just understood the logic

vast grove
frank kindle
#

ok but what if you have multiple items in the subscription? how would you know which one to substitute?

#

that's why I was thinking of the oldPriceID

vast grove
#

I think we'll end up refactoring how we handle subscriptions soon-ish when we introduce multiple items, for now we have one item per subscription.

#

We have two products, each with a price_id and we generate a subscription for each

#

Also a hack would be that if we don't find the updated priceID (so it was changed) in a subscription with multiple items, we might just set them all to 0 and turn the first one into the new priceID.

frank kindle
#

it's really up to you

vast grove
#

Yeah, business logic ๐Ÿ™‚

vast grove
#

Thanks a lot for the help! ๐Ÿ™๐Ÿ™

frank kindle
#

let me know if you need any more help

vast grove
#

Thank you, I hope I won't will go through the flows and see if anything comes up. Have a great day! ๐Ÿ’ช