Calculate Prorated Rate For Stripe Using Billing Cycle Anchor

I’m creating a prorated charge for the 1st subscription month using billing_cycle_anchor to make sure that the 1st full invoice is charged on the 1st of the next month.

This part works well but then Stripe calculates a prorated rate for the current invoice to be between (right now) & the future billing date (set by the billing cycle anchor).

How can I calculate and show this prorated charge to my customer before actually charging their card?

Currently we are using the below logic to calculate the current month’s prorated charge using unix timestamp but its not exact. Our formula displayed 3.54 USD but Stripe ended up charging 3.87 USD . Sometimes it does show the exact value but we need a foolproof solution.

async function getThisMonth(pmtdata) {
  let now = Date.now();
  var lastday = function(y,m){
    return new Date(y, m +1, 0).getDate();
  }
  let seconds = now / 1000;
  let year = new Date().getFullYear();
  let month = new Date().getMonth();
  if(month <= 10) {
    let date = lastday(year,month);
    let l1 = 86400 * Number(date);
    let l2 = Number(pmtdata.numberPrice) / l1;
    let todayD = new Date().getUTCDate();
    let l3 = date - todayD;
    let l4 = 86400 * l3;
    let finalOut = Number(l2 * l4).toFixed(2);
    $w("#thisMonth").text = finalOut + ' ' + pmtdata.currency;
  } else if(month === 11) {
    month = await -1;
    let date = lastday(year,month);
    let l1 = 86400 * Number(date);
    let l2 = Number(pmtdata.numberPrice) / l1;
    let todayD = new Date().getUTCDate();
    let l3 = date - todayD;
    let l4 = 86400 * l3;
    let finalOut = Number(l2 * l4).toFixed(2);
    $w("#thisMonth").text = finalOut + ' ' + pmtdata.currency;
  }
}

Is there any way to get the exact amount that Stripe will charge? Went over the internet but could not find any resource that would return the exact amount Stripe would charge. Any hacks?

Hi Shan, first off you will know more about this than me already… :wink:

However, just reading the Stripe page that you linked to for the billing cycle anchor, can we assume that you have seen that prorate has been deprecated.

Can assume that you have, so reading the page section for the proration behavior, which I have linked below.
https://stripe.com/docs/billing/subscriptions/billing-cycle#prorations

…When resetting billing_cycle_anchor , the customer is invoiced immediately for the next period. With proration disabled there is no credit for the unused time, so the new invoice for the full period amount might result in overpayment. Enabling proration in this case will result in the total invoiced amount being correct…

Also, did you read the Prorations page itself?
https://stripe.com/docs/billing/subscriptions/prorations

…The prorated amount is calculated down to the second by Stripe. We calculate the difference between the two plans based on the time the API call was made to change the customer’s subscription, using the current billing period’s start and end times…

…After using the invoice items to calculate the cost of the proposed subscription change, you could confirm the amount with the customer before making the change. But, because Stripe prorates to the second, the amount of the prorations can change slightly between the time they are previewed, and the time the update is actually performed. To avoid this problem, lock in the proration by passing a subscription_proration_date parameter (on an invoice, as in the above code) or proration_date parameter (on a subscription, in the following), which causes the proration to be calculated as though the change was made at that time…

Apologies if you have already read all this and it doesn’t help.:disappointed:

Thanks Greg! I think I found what I need: https://stackoverflow.com/questions/60779249/calculate-prorated-rate-for-stripe-using-billing-cycle-anchor/60780174