ketan
ketan

Reputation: 103

stripe upcoming invoice for same billing cycle in stripe

I'd like to show a customer a preview (or a quote) of what their subscription charges will be if they make changes to their subscription. For this, I'm using the "upcoming invoice" API endpoint:

https://stripe.com/docs/api/invoices/upcoming

From that link, they state that "You can preview the effects of updating a subscription, including a preview of what proration will take place.".

here is the exapmle

def retrieveUpcomingInvoice(subscriptionId,customerId,nextPriceId):

    try:
        # Retrieve the subscription
        subscription = stripe.Subscription.retrieve(subscriptionId)

        # Retrieve the invoice
        invoice = stripe.Invoice.upcoming(
            customer=customerId,
            subscription=subscriptionId,
            subscription_items=[
                {
                    'id': subscription['items']['data'][0].id,
                    'deleted': True,
                },
                {
                    'price': nextPriceId,
                    'deleted': False,
                }
            ],
        )
        return invoice
    except Exception as e:
        return str(e)

i got the amount_due for upgrad subscription amount user has to pay but only for different billing cycle

Monthly to monthly subscription "or" yearly to yearly subscription(same billing cycle):

like if i am on monthly basic subscription for $10 and i upgrade the subscription for advance subscription for $30 than invoice in return i got amount_due for $40 which is merged amount of plus and advanced, which is wrong , because i had already paid for 10$, so amount pay should be 30-10 = 20$ (tested: if i pay then 20$ is cut from account) (and invoice show $40 which is wrong)

Monthly subscription to yearly subscription or Vice-versa (this is ok)

if currently i am on monthly basic subscription for 10$ and i upgrade to yearly plus subscription for 100$ then upcoming invoice in amount_due show me correct information which is 90$ (and also if i pay it cut 90$ from account)

so how to get coorect amount in same billing cycle in stripe

Upvotes: 1

Views: 1105

Answers (2)

soma
soma

Reputation: 2204

You have a subscription for $10 per month, and after 15 days (half of the month) you want to change it to $30. With the upcoming invoice, you get the invoice the customer should pay in the next billing cycle which includes 3 line items:

  • Unused time: -$5 (that's the initial price divided by two)
  • Remaining time: +$15 (that's the new price divided by two)
  • Next invoice: +$30 (the new price for next month)

Here the total is: -$5 + $15 + $30 = $40 to be paid in 15 days. If that's not what you want you could:

Note that changing the billing cycle of a subscription from monthly to yearly works differently, as explained here.

Upvotes: 2

ketan
ketan

Reputation: 103

reset your billing cycle for get the exact amount

add subscription_billing_cycle_anchor = "now" at getting upcoming invoice

def retrieveUpcomingInvoice(subscriptionId,customerId,nextPriceId):

    try:
        # Retrieve the subscription
        subscription = stripe.Subscription.retrieve(subscriptionId)

        # Retrieve the invoice
        invoice = stripe.Invoice.upcoming(
            customer=customerId,
            subscription=subscriptionId,
            subscription_items=[
                {
                    'id': subscription['items']['data'][0].id,
                    'deleted': True,
                },
                {
                    'price': nextPriceId,
                    'deleted': False,
                }
            ],

            subscription_billing_cycle_anchor = "now",  #  add 
        )
        return invoice
    except Exception as e:
        return str(e)

Upvotes: 0

Related Questions