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

Sha*_*han 1 javascript stripe-payments

我使用billing_cycle_anchor为第一个订阅月创建按比例收费,以确保在下个月的 1 号收取第一张完整发票。

这部分效果很好,但随后 Stripe 会计算当前发票在(现在)和未来账单日期之间的按比例分配的费率。

在实际向客户的卡扣款之前,如何计算并向客户显示按比例分配的费用?

目前我们使用以下公式来计算当前的按比例分配金额,但并不准确。我们的公式显示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;
    }
}
Run Code Online (Sandbox Code Playgroud)

pmtdata 携带每月价格和货币

w1z*_*n1p 6

我认为您可能想使用即将推出的发票 API,而不是尝试计算。

您可以在创建订阅之前传入 - 这将返回发票的“预览”subscription_billing_cycle_anchor版本。subscription_items