Skip to content

Instantly share code, notes, and snippets.

@biancadanforth
Created June 10, 2021 17:08
Show Gist options
  • Save biancadanforth/a9f2cb578054646bda80ff129e49ba21 to your computer and use it in GitHub Desktop.
Save biancadanforth/a9f2cb578054646bda80ff129e49ba21 to your computer and use it in GitHub Desktop.
Formula for computing a prorated upgrade charge amount in Stripe

If upgrading (say from a $10/month to a $20/month plan 3 days into the subscription):

  1. Calculate the fraction of the period used to this point under the previous plan, x.
3 days /30 days = 0.1 = x

Note: This is approximate, since months can have different numbers of days, there are leap years, etc.

  1. Multiply x by the previous plan's billing amount, amount_prev to get how much to credit the customer back, credit.
credit = x * amount_prev  = 0.1 * 10 = 1
  1. Calculate the % of the period not used to this point under the previous plan, y = 1 - x.
1 - x = y 0.10 = 0.90 = y
  1. Multiply y by the new plan's billing amount, amount to get how much to charge the customer, charge.
y * amount = charge = 0.90 * 20 = 18
  1. Subtract credit from charge for the prorated total:
charge - credit = total = 18 - 1 = 17

So, in this example, you would expect to be charged an additional $17 (approximate) for this first billing cycle under the new plan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment