#rzzz | Real Deal
1 messages · Page 1 of 1 (latest)
Hello! What's up?
Hello
I'm currently doing upgrade/downgrade for metered based subscriptions
I want to achieved something like this.
Ok, and can you describe what you're doing and whats not working like you want/expect it to?
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
can I achieved something like this ?
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."
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
like this one?
but then on the next month the customer still has 2 active product under 1 subscription
Right, but you'd remove the old item
We explain how you can show what the adjustment would look like here:
https://stripe.com/docs/billing/subscriptions/usage-based#prorations
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
Like this
it's almost the same for the standard pricing
Ok, and what part isnt what you want/expect?
Sure, so you need to delete that item then
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
And decide if you want to clear_usage or not (usually this is what's wanted)
https://stripe.com/docs/api/subscriptions/update#update_subscription-items-clear_usage
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Can i show you the my code for this one?
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 });
}
}
Sure
I think i'm missing something on my code
Yes, this:
await stripe.subscriptionItems.del(subscriptionItemId);
You probably want to add clear_usage:
https://stripe.com/docs/api/subscription_items/delete#delete_subscription_item-clear_usage
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
If you're deleting the item directly, thats the API