39

I'm trying to test different Stripe Subscription failures. Specifically, what happens when an active subscription gets renewed when initially the card got accepted and the subscription is active, but the renewal payment gets declined, or the customer deletes the card during the subscription.

How can I simulate this on Stripe?

Renis1235
  • 4,116
  • 3
  • 15
  • 27
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179

1 Answers1

68

EDIT 2023-02-02: The answer below was written in 2016. While it should still work, you can probably achieve this in a more straightforward manner by using test clocks instead.


In order to test failed subscription payments, you can do something like this:

  1. Create the customer with a card token for a valid card (e.g. 4242 4242 4242 4242) in the source parameter.

  2. Create the subscription to the plan. The subscription will be successfully created since the first charge will succeed.

  3. Update the customer with a card token for a failing card (e.g. 4000 0000 0000 0341) in the source parameter.

  4. Update the subscription with the trial_end parameter set to a few seconds in the future and prorate set to false. This effectively changes the next billing date for the subscription.

Once the trial_end date is reached, a new billing period will start and an invoice will be created. After approximately one hour, the invoice will be closed and payment will be attempted. What happens then is decided by your subscription account settings.

To test your second scenario, you can simply change step 3 above to delete the card instead of updating the customer with a failing card.

Ywain
  • 16,854
  • 4
  • 51
  • 67
  • So payment will only be attempted approximately one hour after trial end date? Or what do you mean by the one hour part? – Charlie Fish Sep 09 '16 at 16:09
  • 6
    There is a (roughly) one hour delay between the time an invoice is created, and the time payment for the invoice is attempted. This is by design, so you can add items to the invoice at the last possible moment, which is useful in certain scenarios (e.g. metered billing). You can force the payment attempt earlier using the API: https://stripe.com/docs/api#pay_invoice. – Ywain Sep 09 '16 at 17:13
  • 2
    Great. So I think I now understand what I need to do to test this case. Thank you so much. – Charlie Fish Sep 09 '16 at 17:15
  • This doesn't work. When I try to update the customer's source it explodes saying that the CC has expired. I can't find a way to attach an invalid CC to a customer – Mollo Sep 29 '20 at 20:22
  • 1
    For me updating with `trial_end` did not work somehow, but this worked: `subscription = await stripe.subscriptions.update(subscriptionId, { billing_cycle_anchor: 'now', proration_behavior: 'none' });` – dcts Nov 21 '20 at 16:45
  • Renew subscription with a failing card 4000 0000 0000 0341 still doesn't work for me. It still successes – Toan Tran Van Mar 18 '21 at 10:59
  • It's easiest to update the customer's payment method using the token: `curl https://api.stripe.com/v1/customers/ -u : -d "source=tok_chargeCustomerFail"` – yktoo Apr 05 '21 at 15:57
  • For those who don't want to wait the 1 hour once the invoice is scheduled, you can manually trigger the charge from the Stripe dashboard and it will move the subscription into the `past_due` state – Luke Dec 01 '21 at 23:59
  • 3
    Stripe's Test Clock feature is likely your best bet in 2023 – Kenzo Feb 03 '23 at 05:55