rebelidealist/stripe-ruby-mock — pull request #78 by gyardley (2014-03-22 14:16:37+00:00)
Title: Supports trial_end param in customer create, subscription create / update

This pull request provides full support for the trial_end parameter when creating a customer, creating a subscription, or updating a subscription.

I wrote this because I have multiple payment plans in my app, each with a free trial, but didn't want customers switching from one plan to the next and getting a new free trial. I do this by setting trial_end of the new plan to trial_end of the old plan, or to "now" if the trial is completely finished.

Some notes on how trial_end works (all working exactly like the Stripe API):

- trial_end must be an integer Unix timestamp or the word 'now'. Anything else returns an invalid request error.
- trial_end cannot be in the past or more than five years in the future. If so, it returns an invalid request error.
- when creating a customer, 'trial_end' must be accompanied by 'plan'. If there's no plan, submitting trial_end results in an invalid request error.
- setting trial_end to a timestamp will create a subscription / modify an existing subscription to be in a 'trialing' status, with a trial period ending on 'trial_end'. The 'current_period_start' and 'current_period_end' will match the 'trial_start' and 'trial_end' respectively.
- setting trial_end to 'now', will create a subscription / modify an existing subscription to be in an 'active' status, without a trial period. The 'trial_end' and 'trial_start' values are nil. If the subscription was already 'active', this has no effect.

Some implementation notes:

- I split 'add_subscription_to_customer' into two methods, 'custom_subscription_params' and 'add_subscription_to_customer'. The 'custom_subscription_params' method is used both when creating and editing subscriptions, while 'add_subscription_to_customer' is used only when adding a brand new subscription to a customer
- I've separated out a 'verify_trial_end' method to takes care of the error handling for trial_end - not a timestamp, in the past, too far in the future, etc. This was done for readability, it's only called from 'custom_subscription_params'.