如何测试初始 Stripe 订阅创建成功然后卡在下一个计费周期到期的场景

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

想象一下以下情况:

  1. 我按月订阅 - 在 Stripe 中成功创建订阅。
  2. 下一个账单周期到来时,我的卡已经过期(或者里面没有资金)所以续费失败。

我想先使用有效的测试卡,然后使用过期的卡创建另一种支付方式。但是,Stripe 不允许我添加那种支付方式。

我如何测试这个场景?

Stripe 对这种错误有很好的测试卡[1]。您可以将其添加到客户,但它会导致任何付款失败。

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

对于这种测试,我通常会创建一个客户,将卡片附加到他们身上,然后创建一个试用期 [2] 为 10 秒的订阅。试用结束后我手动finalize并尝试在invoice上取款导致续订失败

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

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