You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Stripe年度订阅类似Slack的按席位反向按比例计费功能

Stripe 实现类Slack年度席位计费方案

基础配置

  • 在Stripe后台创建许可式按席位计费的商品,价格周期设置为annual,填写单席位年度定价
  • 创建初始订阅时,在API参数中指定proration_behavior: 'none',关闭Stripe默认的按比例折算逻辑,避免默认规则干扰自定义计费

核心新增席位计费实现

每次用户触发新增席位操作时,按以下流程执行:

  1. 调用Stripe订阅查询接口获取当前订阅的周期时间戳,计算剩余周期折算比例:
// 以下时间戳单位均为秒
const currentStart = subscription.current_period_start
const currentEnd = subscription.current_period_end
const totalCycle = currentEnd - currentStart
const remainingTime = currentEnd - Math.floor(Date.now() / 1000)
// 折算比例 = 剩余时长 / 总周期时长
const prorateRate = remainingTime / totalCycle
// 单席位年费以分为单位避免浮点误差,示例为100美元即10000分
const singleSeatPrice = 10000
// 单新增席位应收金额,四舍五入取整
const addonAmount = Math.round(singleSeatPrice * prorateRate)
  1. 生成绑定到当前订阅的一次性计费项,再更新订阅席位数量,最后主动开票扣款:
// 新增一次性计费项
await stripe.invoiceItems.create({
  customer: subscription.customer,
  subscription: subscription.id,
  amount: addonAmount,
  currency: 'usd',
  description: `新增${addSeatCount}个席位 剩余周期折算费用`
})
// 更新订阅总席位数量,不触发默认折算
await stripe.subscriptions.update(
  subscription.id,
  {
    quantity: originalSeatCount + addSeatCount,
    proration_behavior: 'none'
  }
)
// 生成账单并自动触发扣款
await stripe.invoices.create({
  customer: subscription.customer,
  subscription: subscription.id,
  auto_advance: true
})
  1. 扣款成功后席位立即生效,下个账单周期将自动按最新总席位数量收取全额年费。

边界情况处理

  • 新增席位时若剩余周期不足1天,直接收取全额单席位年费,避免折算金额为0的异常
  • 扣款失败时不更新订阅席位数量,返回失败提示引导用户重新操作
  • 若需支持减席退款,可按相同折算比例计算应退金额,调用Stripe退款接口处理即可

内容的提问来源于stack exchange,提问作者Maielo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 04:57:04