PAYATHON 2026

支持每月可变金额 recurring billing 的 payment gateway

支付小周

结论

支持。这种计费方式通常不是“修改固定订阅金额”,而是以下几类模式:

  • Usage-based billing / metered billing:按实际用量计费
  • Quantity-based billing:单价固定,每月数量不同
  • Variable recurring payments:扣款周期固定,金额可变
  • Invoice billing with card on file:每月计算账单,再从已保存的支付方式中自动扣款

对于 FogBugz 这类按照当月 active users 数量收费的业务,适合采用“固定单价 × 当月有效用户数”的计量计费模型。无需每月修改 subscription amount,只要在结算前提交当月数量,或动态创建账单项即可。

推荐实现方式

假设每位 active user 每月收费 10 美元:

[ \text{Monthly Charge} = \text{Active Users} \times $10 ]

常见流程如下:

  1. 客户首次订阅时完成支付授权。
  2. 支付服务保存客户的 payment method,不保存原始银行卡信息。
  3. 系统在每个计费周期内统计 active users。
  4. 在结算日前,将数量或用量提交给 billing provider。
  5. billing provider 生成 invoice,并从已保存的 payment method 中自动扣款。
  6. 系统通过 webhook 处理支付成功、支付失败、退款和订阅取消等事件。

如果支付平台只支持固定金额的 recurring profile,也可以保存客户的支付授权,由自己的系统每月计算金额,再发起 merchant-initiated transaction。能否使用这种扣款方式,取决于支付服务、银行卡网络规则和当地监管要求。

示例:按用户数量生成账单

以下是与具体厂商无关的示意代码。实际 endpoint、字段名称和签名方式应以所选服务的官方文档为准。

async function billCustomerForMonth(customerId, billingPeriod) {
  const activeUsers = await countActiveUsers(customerId, billingPeriod);
  const unitPriceInCents = 1000;
  const amountInCents = activeUsers * unitPriceInCents;

  const invoice = await paymentGateway.createInvoice({
    customer: customerId,
    currency: "usd",
    line_items: [
      {
        description: `Active users for ${billingPeriod}`,
        quantity: activeUsers,
        unit_amount: unitPriceInCents
      }
    ],
    collection_method: "charge_automatically"
  });

  return paymentGateway.finalizeInvoice(invoice.id);
}

如果平台原生支持 metered billing,通常只需上报用量:

await billingProvider.reportUsage({
  subscription_item: subscriptionItemId,
  quantity: activeUsers,
  period: "2026-08"
});

平台会按照订阅价格、数量和计费周期计算最终金额。这里的 API 仅用于展示结构,不代表任何服务的真实参数。

数据库与幂等处理

至少需要保存以下信息:

customer_id
subscription_id
billing_period
active_user_count
unit_price
calculated_amount
invoice_id
payment_status
idempotency_key

每个计费周期都应使用唯一的幂等键,防止定时任务重试时重复扣款:

const idempotencyKey = `${customerId}:${billingPeriod}`;

await paymentGateway.createInvoice(
  invoiceData,
  { idempotencyKey }
);

还应保存当月的计费快照。如果用户在出账后被删除或停用,而系统没有保留快照,历史账单中的 active user 数量可能无法复核。

选择服务时要确认的能力

产品标注“支持订阅”并不代表它适合这种计费方式,还需要确认是否具备以下能力:

  • metered usage 或可变 quantity
  • 每个周期追加 invoice items
  • 自动扣取已保存的 payment method
  • proration,也就是用户在周期中途增减时按比例计费
  • invoice preview,可在出账前预览金额
  • idempotency,防止重复扣款
  • payment failure retry 和 dunning
  • webhook 事件通知
  • 税费、折扣、退款和贷项处理
  • 展示客户账单及用量明细

有些平台允许按周期更新 subscription quantity,但这未必等同于按照整月峰值、平均值或去重后的活跃人数计费。应先确定 active user 的计算规则,再决定上报“期末数量”还是累计 usage。

注意事项

可变金额自动扣款也涉及客户授权和合规要求。客户首次授权时,应明确告知扣款频率、金额计算方式和取消规则。部分国家、支付方式或银行卡网络可能要求在扣款前发送通知、进行额外认证,或限制 merchant-initiated transaction 的使用条件。

不建议为了后续扣款而直接存储银行卡号。使用支付服务提供的 token、payment method 或 vaulted card 功能,可以缩小 PCI DSS 合规范围。

应优先选择支持 usage-based billing、metered billing 或 quantity-based subscription 的平台。如果使用的只是支付网关,没有订阅计费引擎,就需要自行完成每月计量和账单生成,再调用保存支付方式的扣款接口。

备注:内容仅供参考。