#muhtasir-rahin_code
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1270468352749338757
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hello
Right now you seem to be creating a new subscription when the customer is trying to upgrade, correct?
You won't be able to use Checkout Sessions API for that but we have a guide here for how you can handle the usecase: https://docs.stripe.com/billing/subscriptions/upgrade-downgrade
An easier option would be to use customer portal and let Stripe handle the upgrades/downgrades for you: https://docs.stripe.com/customer-management
ok thank you I will look into them.
NP! ๐ Good luck
I looked into them and saw Immediate payment. I wrote some code. It works I think but after the update it takes me to page /undefined.
items: [
{
id: subscriptionItemId,
price: priceId,
},
],
proration_behavior: 'always_invoice',
expand: ['latest_invoice.payment_intent'],
});
if (!pendingUpdate.latest_invoice || typeof pendingUpdate.latest_invoice === 'string') {
throw new Error('Failed to create invoice for subscription update');
}
const invoice = await stripe.invoices.retrieve(pendingUpdate.latest_invoice.id);
return new NextResponse(JSON.stringify({
invoice_url: invoice.hosted_invoice_url,```
The example in the docs only have code for the server-side changes.
can you try printing invoice.hosted_invoice_url ?
alright wait
sorry for late response. It sent Invoice url:
https://invoice.stripe.com/i/acct_1Pjk5BKplnDt3auM/test_YWNjdF8xUGprNUJLcGxuRHQzYXVNLF9RYzhDNXVwVXVUZmNCaUxERnJpTkZoa3VkWE1CRUNqLDExMzUxNjY5Nw02007fOWG5Y3?s=ap
Gotcha. So it is not that.. You'd want to look into your client-side log to see what code is responsible for navigation/redirect and investigate why it is going to /undefined
Hey! I fixed the thing. It takes you to the invoice. But I have a new issue. In the invoice it says: 40.00 Dollars. I think this is because the current price is 39.99 and the price I am prorating to is 79.99(79.99 - 39.99 = 40) and when I upgraded to a bigger price which is 129.99 it said 50 which means my suspicions were correct. But I want the users to pay full price for the upgrade. Not ( Upgraded Plan Price - Current Price Paid )
const pendingUpdate = await stripe.subscriptions.update(subscriptionId, {
items: [
{
id: subscriptionItemId,
price: priceId,
},
],
proration_behavior: 'always_invoice',
expand: ['latest_invoice.payment_intent'],
});
if (!pendingUpdate.latest_invoice || typeof pendingUpdate.latest_invoice === 'string') {
throw new Error('Failed to create invoice for subscription update');
}
const invoice = await stripe.invoices.retrieve(pendingUpdate.latest_invoice.id);
// Print the hosted invoice URL for debugging
console.log(`Invoice URL: ${invoice.hosted_invoice_url}`);
return new NextResponse(JSON.stringify({
invoice_url: invoice.hosted_invoice_url,
}), { status: 200 });
Do you know how I could achieve that?
๐ Stepping in for my teammate, give me a few mins to catch up
You likely want proration_behavior: 'none'
But in the docs it said if you want to send an invoice use always_invoice wouldn't this break it?
In this case, is the billing period the same (e.g., monthly > monthly)?
yes
I see, okay.
So yes, if you set proration_behavior to none, the customer won't be charged the new price until the next invoice is generated
You could update the Subscription with proration_behavior: 'none' and reset the billing cycle anchor to now to immediately cut an invoice: https://docs.stripe.com/billing/subscriptions/billing-cycle#reset-the-billing-cycle-to-the-current-time
Thanks! I'll look into it
Hmmm it did not send me to the invoice page:
const subscriptionId = subscriptionExists.subscription.subscriptionId;
const currentSubscriptionDetails = await stripe.subscriptions.retrieve(subscriptionId);
const subscriptionItemId = currentSubscriptionDetails.items.data[0].id;
// Update the subscription with proration_behavior: 'none' and reset billing cycle anchor
const updatedSubscription = await stripe.subscriptions.update(subscriptionId, {
items: [
{
id: subscriptionItemId,
price: priceId,
},
],
proration_behavior: 'none',
billing_cycle_anchor: 'now',
expand: ['latest_invoice.payment_intent'],
});
if (!updatedSubscription.latest_invoice || typeof updatedSubscription.latest_invoice === 'string') {
throw new Error('Failed to create invoice for subscription update');
}
const invoice = await stripe.invoices.retrieve(updatedSubscription.latest_invoice.id);
// Print the hosted invoice URL for debugging
console.log(`Invoice URL: ${invoice.hosted_invoice_url}`);
return new NextResponse(JSON.stringify({
invoice_url: invoice.hosted_invoice_url,
}), { status: 200 });
It happens ๐
Thanks for the help!