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:
Create the customer with a card token for a valid card (e.g. 4242 4242 4242 4242
) in the source
parameter.
Create the subscription to the plan. The subscription will be successfully created since the first charge will succeed.
Update the customer with a card token for a failing card (e.g. 4000 0000 0000 0341
) in the source
parameter.
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.