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 thesourceparameter. -
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 thesourceparameter. -
Update the subscription with the
trial_endparameter set to a few seconds in the future andprorateset tofalse. 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.