#muhtasir-rahin_code

1 messages ยท Page 1 of 1 (latest)

red phoenixBOT
#

๐Ÿ‘‹ 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.

quartz cargo
#

Hello
Right now you seem to be creating a new subscription when the customer is trying to upgrade, correct?

trail wing
#

yes

#

I want that it uses the same subscription and just changes the price of it.

quartz cargo
trail wing
#

ok thank you I will look into them.

quartz cargo
#

NP! ๐Ÿ™‚ Good luck

trail wing
#

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,```
quartz cargo
#

The example in the docs only have code for the server-side changes.

can you try printing invoice.hosted_invoice_url ?

quartz cargo
#

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

red phoenixBOT
trail wing
#

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?

fervent summit
#

๐Ÿ‘‹ Stepping in for my teammate, give me a few mins to catch up

#

You likely want proration_behavior: 'none'

trail wing
#

But in the docs it said if you want to send an invoice use always_invoice wouldn't this break it?

fervent summit
#

In this case, is the billing period the same (e.g., monthly > monthly)?

trail wing
#

yes

fervent summit
#

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

trail wing
#

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 });
fervent summit
#

Is Invoice URL: null?

#

@trail wing let me know if you still need help

trail wing
#

I think I fixed it

#

Invoice URL did exist I just did not redirect to it ma bad

fervent summit
#

It happens ๐Ÿ˜…

trail wing
#

Thanks for the help!