Stripe subscription to update billing cycle anchor after payment success

Viewed 132

Lets say I have a subscription in stripe (starting on 2021-10-01), and at the end of the first billing cycle on 2021-11-01

  1. stripe attempts to charge the card on file
  2. lets say that transaction fails due to insufficient_funds
  3. stripe retries again after 2 days (on 2021-11-03), this time the transaction is successful.

now the subscription will again charge the user again on 2021-12-01 even though the prev payment was done on 2021-11-03

I do not provide services to the user (2021-11-01 to 2021-11-03) when they payment is not complete

What is the best practice here so the next charge happens on 2021-12-03 (instead of 2021-12-01)?

1 Answers

Using stripe webhooks and 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;
}

More info:

https://stripe.com/docs/webhooks

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

Related