简体   繁体   中英

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.

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

I think you may want to use the Upcoming Invoice API rather than trying to calculate.

You can pass in subscription_billing_cycle_anchor and subscription_items before ever creating the Subscription - this will return a "preview" version of the invoice.

You can calculate the amount with your formula and charge your customer with payment intnent. Then, update your subscription without invoicing by setting:

proration_behavior = none

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM