如何配置Stripe订阅:首两月按比例预付,后续按月付费
按剩余天数比例计费的Stripe订阅配置方案
核心思路
通过Stripe Subscription Schedules分三个阶段配置:
- 首阶段:按注册日到当月月底的剩余天数,计算比例费用并一次性收取
- 第二阶段:收取次月全额订阅费
- 第三阶段:从第三个月起,每月月初自动收取全额订阅费
步骤详解
1. 计算首月比例费用与阶段时长
根据用户注册日期,手动计算当月剩余天数和对应费用:
- 示例:2024年1月3日注册,1月共31天,剩余28天,费用为
(28/31)*100 ≈ 90.32美元 - 转换为Stripe所需的最小单位(分):
Math.round(90.32 * 100) = 9032分 - 阶段时长为剩余天数,结束日期设为当月最后一天
2. 配置Subscription Schedules的三个阶段
用Stripe API创建订阅计划时,分阶段定义规则:
- 第一阶段(首月比例计费):
- 起始日期为用户注册时间
- 结束日期为当月最后一天
- 自定义价格为计算出的比例费用,周期设为剩余天数
- 第二阶段(次月全额计费):
- 起始日期为次月1日
- 结束日期为次月最后一天
- 使用每月100美元的标准订阅价格
- 设置
billing_cycle_anchor为次月1日,确保后续阶段的计费锚点统一为每月月初
- 第三阶段(常规月度订阅):
- 无结束日期,永久循环
- 使用标准订阅价格,每月1日自动扣费
3. 代码示例(Node.js)
const stripe = require('stripe')('你的秘钥'); // 用户注册日期(示例:2024-01-03) const signupDate = new Date('2024-01-03'); // 当月最后一天 const currentMonthLastDay = new Date(signupDate.getFullYear(), signupDate.getMonth() + 1, 0); // 计算剩余天数 const daysRemaining = Math.floor((currentMonthLastDay - signupDate) / (1000 * 60 * 60 * 24)); // 计算比例费用(转换为分) const proratedAmount = Math.round((daysRemaining / currentMonthLastDay.getDate()) * 100 * 100); // 创建订阅计划 const schedule = await stripe.subscriptionSchedules.create({ customer: '客户ID', start_date: Math.floor(signupDate.getTime() / 1000), phases: [ // 第一阶段:首月比例计费 { items: [{ price_data: { currency: 'usd', unit_amount: proratedAmount, product: '你的产品ID', recurring: { interval: 'day', interval_count: daysRemaining } }, quantity: 1 }], end_date: Math.floor(currentMonthLastDay.getTime() / 1000), collection_method: 'charge_automatically' }, // 第二阶段:次月全额计费 { items: [{ price: '每月100美元的价格ID', quantity: 1 }], end_date: Math.floor(new Date(currentMonthLastDay.getFullYear(), currentMonthLastDay.getMonth() + 1, 0).getTime() / 1000), billing_cycle_anchor: Math.floor(new Date(currentMonthLastDay.getFullYear(), currentMonthLastDay.getMonth() + 1, 1).getTime() / 1000) }, // 第三阶段:每月月初循环计费 { items: [{ price: '每月100美元的价格ID', quantity: 1 }], recurring: { interval: 'month', interval_count: 1 }, collection_method: 'charge_automatically' } ] });
注意事项
- 必须手动计算剩余天数和比例费用,Stripe默认的proration规则仅适用于订阅变更场景,无法直接处理初始订阅的首月比例计费
- 确保第二阶段的
billing_cycle_anchor设置为次月1日,保证后续每月月初自动扣费的规则生效 - 所有金额需转换为Stripe支持的最小货币单位(如美元用分)
内容的提问来源于stack exchange,提问作者yellow
相关产品推荐
相关产品推荐

