#vajhatz
1 messages ยท Page 1 of 1 (latest)
๐ happy to help
Hey! ๐
items: [
{
id: newPriceID, // This is the new priceID
quantity,
},```
this is wrong
the id here is the Subscription 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.
then you need to pass in the price ID https://stripe.com/docs/api/subscriptions/update#update_subscription-items-price
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
( 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
}
const planItem = planItems.find((i) => i.price.id === newPriceID)
don't you mean the oldPrice ID?
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.
oh you're doing me a massive favor just by looking at my code so thanks so much
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
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.
it's really up to you
Yeah, business logic ๐
I gave this a shot
Thanks a lot for the help! ๐๐
let me know if you need any more help
Thank you, I hope I won't will go through the flows and see if anything comes up. Have a great day! ๐ช