其实很简单。
您需要将 Stripe 的 metered billing 与 billing cycle anchor 结合起来。
首先,您需要为结算创建一个“价格”。这可以通过 API 完成。注意interval、usage_type、currency 和unit_amount:
const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
const price = await stripe.prices.create({
currency: 'usd',
recurring: {
interval: 'month',
usage_type: 'metered'
},
product_data: {
name: 'Gold special',
},
nickname: 'Gold special price',
unit_amount: 3000,
});
然后您需要创建订阅,确保不要传入quantity 参数。确保记录输出subscription item ID - 这是用于报告使用情况。
创建付款后,您需要使用billing cycle anchor 设置订阅。锚点是当前 UNIX 时间戳的 UNIX 时间戳(以秒为单位)。
要达到您想要的效果,您需要创建一个在每月 31 日触发的锚点。当给定月份没有 31 日时,它将在该月的最后一天触发发票:
const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
const subscription = await stripe.subscriptions.create({
customer: 'cus_4fdAW5ftNQow1a',
items: [
{
price: 'price_CBb6IXqvTLXp3f',
},
],
billing_cycle_anchor: 1611008505, // Calculate current time in seconds until anchor date
});
这将按照文档中的说明自动按比例分配。
我给出的两个示例非常简单,您可以进行更多配置,但我希望它们作为起点有所帮助。请阅读答案顶部的两个文档链接,了解如何配置您可能需要的所有其他内容。