简体   繁体   English

使用计费周期锚计算 Stripe 的按比例分配的费率

[英]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.我正在使用billing_cycle_anchor为第 1 个订阅月创建按比例收费,以确保在下个月的 1 日收取第一个完整发票。

This part works well but then Stripe calculates a prorated rate for the current invoice to be between (right now) & the future billing date.这部分运行良好,但随后 Stripe 计算了当前发票的按比例分配的费率,该费率介于(现在)和未来的计费日期之间。

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我们的公式显示3.54 美元,但 Stripe 最终收取3.87 美元

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 pmtdata 携带每月价格和货币

I think you may want to use the Upcoming Invoice API rather than trying to calculate.我认为您可能想要使用即将到来的发票 API而不是尝试计算。

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.您可以在创建subscription_items之前传入subscription_billing_cycle_anchorsubscription_items - 这将返回发票的“预览”版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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