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.
How can I calculate and show this prorated charge to my customer before actually charging their card?
Currently we are using the below formula to calculate the current proration amount but its not exact. Our formula displayed 3.54 USD but Stripe ended up charging 3.87 USD
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;
chargeable = finalOut;
} 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;
chargeable = finalOut;
}
}
pmtdata carries the monthly price and currency