Stripe 订阅在付款成功后更新计费周期锚

rra*_*rag 5 stripe-payments

假设我有一个 stripe 订阅(从 2021 年 10 月 1 日开始),并在 2021 年 11 月 1 日第一个计费周期结束时

  1. stripe 尝试对存档的卡进行收费
  2. 假设交易因资金不足而失败
  3. 2天后(2021-11-03)再次stripe重试,这次交易成功。

现在,即使之前的付款已于 2021 年 11 月 3 日完成,订阅仍将在 2021 年 12 月 1 日再次向用户收取费用

当用户付款未完成时,我不向用户提供服务(2021-11-01至2021-11-03)

此处的最佳做法是什么,以便下次收费发生在 2021 年 12 月 3 日(而不是 2021 年 12 月 1 日)?

kur*_*tko 2

使用 stripe webhook 和billing_cycle_anchor: "now"

// stripe webhooks: see example of how to implement here: https://stripe.com/docs/webhooks

const dataObject = event.data.object;

switch (event.type) {
    
  //important to use the event invoice.updated to to avoid infinite loops
  case "invoice.updated":

      await stripe.subscriptions.update(dataObject.subscription, {
        billing_cycle_anchor: "now",
        proration_behavior: "create_prorations",
      });         

      break;
}
Run Code Online (Sandbox Code Playgroud)

更多信息:

https://stripe.com/docs/webhooks

https://stripe.com/docs/billing/subscriptions/billing-cycle