#rzzz | Real Deal

1 messages · Page 1 of 1 (latest)

grizzled mangoBOT
pastel dove
#

Hello! What's up?

abstract kettle
#

Hello

#

I'm currently doing upgrade/downgrade for metered based subscriptions

#

I want to achieved something like this.

grizzled mangoBOT
past iron
#

Ok, and can you describe what you're doing and whats not working like you want/expect it to?

abstract kettle
#

For example the customer has a premium metered based subscription then it has a usage record let say 20 executions of api for that subscription. Then the customer decides to downgrade to starter within the same period

abstract kettle
#

tho one of your team said that
"Prorations are created only for licensed (per-seat) subscriptions because they’re billed at the start of each billing period."

past iron
#

Right, for metered pricing you need to manage this explicitly. Typically we recommend adding a new subscription item for the transition period and ascribing new usage there and stopping usage records on the original item, then you can remove the old subscription item after the billing cycle rolls over

abstract kettle
#

like this one?

#

but then on the next month the customer still has 2 active product under 1 subscription

past iron
#

Right, but you'd remove the old item

#

But you'd need to decide what to do with the usage -- probably you want to move it to the new price and clear the old usage, but thats up to you

abstract kettle
#

it's almost the same for the standard pricing

past iron
#

Ok, and what part isnt what you want/expect?

abstract kettle
#

this part 😂

#

then it's carry over to the next month

past iron
#

Sure, so you need to delete that item then

abstract kettle
#

exports.SubscriptionMetered = async (req, res) => {
const { subscriptionId, newPriceId } = req.body;

try {
const subscription = await stripe.subscriptions.retrieve(subscriptionId);

let subscriptionItemId;
for (const item of subscription.items.data) {
  if (item.price.id === subscription.items.data[0].price.id) {
    subscriptionItemId = item.id;
    break;
  }
}

if (!subscriptionItemId) {
  return res.status(400).send({ error: 'Could not find subscription item with current price.' });
}

const prorationDate = subscription.current_period_end;

if (subscription.status === 'canceled') {
  return res.status(400).send({ error: 'Cannot update a canceled subscription.' });
}

// Create a new subscription item for the transition period
const newSubscriptionItem = await stripe.subscriptionItems.create({
  subscription: subscription.id,
  price: newPriceId,
  metadata: { transition_period: true }
});

// Stop usage records on the old subscription item
await stripe.subscriptionItems.update(subscriptionItemId, {
  billing_thresholds: {
    usage_gte: null
  }
});

// Update the subscription with the new subscription item
await stripe.subscriptions.update(subscriptionId, {
  cancel_at_period_end: false,
  items: [{
    id: newSubscriptionItem.id,
    price: newPriceId
  }]
});

// Remove the old subscription item
await stripe.subscriptionItems.del(subscriptionItemId);

res.status(200).send({ message: 'Subscription updated successfully.' });

} catch (error) {
console.log(error);
res.status(500).send({ error: error.message });
}
}

past iron
#

Sure

grizzled mangoBOT
abstract kettle
#

I think i'm missing something on my code

past iron
#

Yes, this:
await stripe.subscriptionItems.del(subscriptionItemId);

#

If you're deleting the item directly, thats the API