Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13733

How to test a scenario when initial Stripe subscription creation is successful and then the card expires for the next billing period

Imagine the following case:

  1. I buy a monthly subscription - subscription is successfully created in Stripe.
  2. When the next billing period comes, my card is already expired (or there is no fund in it) so the renewal fails.

I had an idea to use a valid test card first and then create another payment method with an expired card. However, Stripe won't allow me to add that kind of payment methods.

How can I test this scenario?

Upvotes: 3

Views: 2281

Answers (2)

Nebula
Nebula

Reputation: 43

Hey I have found a work around, I couldn't find a dedicated solution.

I have used test clocks, stripe cli.

Steps:

  1. Add a customer and attach test clock.
  2. Complete the payment , I used session checkout.
  3. Then open the stripe dashboard, go to customers, then delete the payment method (or edit to a card with the error condition you want to test for).
  4. Forward the test clock accordingly (month, year).
  5. As the payment method is not attached invoice.payment_failed is triggered.
  6. Test weather your code handles it situation properly.

Step3,Step5,Final Dashboard Screenshot

This question is the same and shows the bug in test clocks.

I want to test my project if it handles the condition where the subscription payment fails after one billing cycle

Upvotes: 2

Pompey
Pompey

Reputation: 1354

Stripe has a great test card[1] for this kind of error. You can add it to a customer but it will fail any payment.

4000000000000341 Attaching this card to a Customer object succeeds, but attempts to charge the customer fail.

For this kind of test, I typically create a customer, attach that card to them, and create a subscription with a trial period[2] of 10 seconds. After the trial is over, I manually finalize and try to take payment on the invoice to cause the renewal failure.

[1] https://stripe.com/docs/testing

[2] https://stripe.com/docs/api/subscriptions/create#create_subscription-trial_end

Upvotes: 4

Related Questions