DEV Community

Alexander Baumann
Alexander Baumann

Posted on

Stripe Invoice.Upcoming changes: model switching from monthly to yearly

How to use the Python SDK stripe.Invoice.upcoming call to preview a new invoice using a different quantity and plan?

There is a reference in the Stripe Invoice docs stating values can be passed when retrieving the upcoming invoice to model what-if scenarios

When fetching the preview, you can also model what the invoice would look like if you changed the subscription in one of these ways:

Swapping the underlying price.
Altering the quantity.
Applying a trial period.
Adding a coupon.
Enter fullscreen mode Exit fullscreen mode

There isn't enough information in the API or SDK changelogs to determine how to replace the deprecated params we use with new ones

Old:

return stripe.Invoice.upcoming(
        customer=customer_id,
        subscription=stripe_subscription_id,
        subscription_quantity=quantity_to_calculate_with, # if None, defaults to current
        subscription_proration_date=proration_date_as_unix_seconds,  
        subscription_plan=plan_to_calculate_with, # if None, defaults to current  
    )
Enter fullscreen mode Exit fullscreen mode

Attempted New:

subscription_details = dict(
proration_date=proration_date_as_unix_seconds, 
items=[
     {
     'quantity': quantity_to_calculate_with, 
     'plan': plan_to_calculate_with
     }
])

return stripe.Invoice.upcoming(
        customer=customer_id,
        subscription=stripe_subscription_id,
        subscription_details=subscription_details
)

Enter fullscreen mode Exit fullscreen mode

I can set the price to the plan id I was using before, the problem I have now is that I cannot model the new invoice price when switching from a monthly to a yearly plan. The error:

All prices on a subscription must have the same recurring.interval and recurring.interval_count. If you meant to update an existing subscription item instead of creating a new one, make sure to include the subscription item ID in your request. See examples at https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#changing.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay