$: << File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'chargify_api_ares'

# You could load your credentials from a file...
chargify_config = YAML::load_file(File.join(File.dirname(__FILE__), '..', 'chargify.yml'))

Chargify.configure do |c|
  c.subdomain = chargify_config['subdomain']
  c.api_key   = chargify_config['api_key']
end

# Lookup up existing Subscription using the Customer's reference
subscription = Subscription.find_by_customer_reference('marky-mark')

# Migrate the subscription to a new product
subscription.migrate(:product_handle => "basic-plan")
# => #<Chargify::Migration:0x007fe656ab4430 @attributes={"product_id"=>20, "id"=>518, "activated_at"=>2013-11-11 20:42:57 UTC, "balance_in_cents"=>-7499, "cancel_at_end_of_period"=>false, "canceled_at"=>nil, "cancellation_message"=>nil, "created_at"=>2013-11-11 20:42:55 UTC, "current_period_ends_at"=>2013-12-12 03:54:21 UTC, "expires_at"=>nil, "next_assessment_at"=>2013-12-12 03:54:21 UTC, "payment_collection_method"=>"automatic", "state"=>"active", "trial_ended_at"=>nil, "trial_started_at"=>nil, "updated_at"=>2013-11-12 03:54:21 UTC, "current_period_started_at"=>2013-11-12 03:54:21 UTC, "previous_state"=>"active", "signup_payment_id"=>2029, "signup_revenue"=>"99.00", "delayed_cancel_at"=>nil, "coupon_code"=>nil, "total_revenue_in_cents"=>10001, "product_price_in_cents"=>2400, "product_version_number"=>2, "customer"=>#<Chargify::Customer:0x007fe656977ea0 @attributes={"address"=>nil, "address_2"=>nil, "city"=>nil, "country"=>nil, "created_at"=>2013-11-11 20:42:35 UTC, "email"=>"marky@example.com", "first_name"=>"Marky ", "id"=>518, "last_name"=>"Mark", "organization"=>nil, "phone"=>nil, "reference"=>"marky-mark", "state"=>nil, "updated_at"=>2013-11-11 20:42:35 UTC, "zip"=>nil}, @prefix_options={}, @persisted=false>, "product"=>#<Chargify::Product:0x007fe656976910 @attributes={"accounting_code"=>nil, "archived_at"=>nil, "created_at"=>2013-11-09 20:00:59 UTC, "description"=>"Eos quam veritatis quo iste et.  Quaerat dolor suscipit explicabo et sit.  Nostrum dolor commodi voluptatum similique et facere accusamus.", "expiration_interval"=>nil, "expiration_interval_unit"=>"never", "handle"=>"basic-plan", "id"=>20, "initial_charge_in_cents"=>nil, "interval"=>1, "interval_unit"=>"month", "name"=>"Basic", "price_in_cents"=>2400, "request_credit_card"=>true, "require_credit_card"=>true, "return_params"=>nil, "return_url"=>nil, "trial_interval"=>1, "trial_interval_unit"=>"month", "trial_price_in_cents"=>0, "update_return_url"=>nil, "updated_at"=>2013-11-12 03:51:18 UTC, "product_family"=>#<Chargify::ProductFamily:0x007fe6569749f8 @attributes={"accounting_code"=>nil, "description"=>nil, "handle"=>"acme-online", "id"=>3, "name"=>"Acme Online"}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=false>, "credit_card"=>#<Chargify::Migration::CreditCard:0x007fe65696de78 @attributes={"type"=>"PaymentProfile", "billing_address"=>nil, "billing_address_2"=>nil, "billing_city"=>nil, "billing_country"=>nil, "billing_state"=>nil, "billing_zip"=>nil, "card_type"=>"bogus", "current_vault"=>"bogus", "customer_id"=>518, "customer_vault_token"=>nil, "expiration_month"=>1, "expiration_year"=>2023, "first_name"=>"Marky", "id"=>518, "last_name"=>"Mark", "masked_card_number"=>"XXXX-XXXX-XXXX-1", "vault_token"=>"1"}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=true, @remote_errors=nil, @validation_context=nil, @errors=#<ActiveResource::Errors:0x007fe656aafd18 @base=#<Chargify::Migration:0x007fe656ab4430 ...>, @messages={}>>

# Another way to migrate a subscription to a new product
Migration.create(:subscription_id => subscription.id, :product_handle => "basic-plan")
# => #<Chargify::Migration:0x007fe656cc8b90 @attributes={"product_id"=>20, "id"=>518, "activated_at"=>2013-11-11 20:42:57 UTC, "balance_in_cents"=>-7499, "cancel_at_end_of_period"=>false, "canceled_at"=>nil, "cancellation_message"=>nil, "created_at"=>2013-11-11 20:42:55 UTC, "current_period_ends_at"=>2013-12-12 03:53:10 UTC, "expires_at"=>nil, "next_assessment_at"=>2013-12-12 03:53:10 UTC, "payment_collection_method"=>"automatic", "state"=>"active", "trial_ended_at"=>nil, "trial_started_at"=>nil, "updated_at"=>2013-11-12 03:53:10 UTC, "current_period_started_at"=>2013-11-12 03:53:09 UTC, "previous_state"=>"active", "signup_payment_id"=>2029, "signup_revenue"=>"99.00", "delayed_cancel_at"=>nil, "coupon_code"=>nil, "total_revenue_in_cents"=>9999, "product_price_in_cents"=>2400, "product_version_number"=>2, "customer"=>#<Chargify::Customer:0x007fe6548fdbc0 @attributes={"address"=>nil, "address_2"=>nil, "city"=>nil, "country"=>nil, "created_at"=>2013-11-11 20:42:35 UTC, "email"=>"marky@example.com", "first_name"=>"Marky ", "id"=>518, "last_name"=>"Mark", "organization"=>nil, "phone"=>nil, "reference"=>"marky-mark", "state"=>nil, "updated_at"=>2013-11-11 20:42:35 UTC, "zip"=>nil}, @prefix_options={}, @persisted=false>, "product"=>#<Chargify::Product:0x007fe6548fc608 @attributes={"accounting_code"=>nil, "archived_at"=>nil, "created_at"=>2013-11-09 20:00:59 UTC, "description"=>"Eos quam veritatis quo iste et.  Quaerat dolor suscipit explicabo et sit.  Nostrum dolor commodi voluptatum similique et facere accusamus.", "expiration_interval"=>nil, "expiration_interval_unit"=>"never", "handle"=>"basic-plan", "id"=>20, "initial_charge_in_cents"=>nil, "interval"=>1, "interval_unit"=>"month", "name"=>"Basic", "price_in_cents"=>2400, "request_credit_card"=>true, "require_credit_card"=>true, "return_params"=>nil, "return_url"=>nil, "trial_interval"=>1, "trial_interval_unit"=>"month", "trial_price_in_cents"=>0, "update_return_url"=>nil, "updated_at"=>2013-11-12 03:51:18 UTC, "product_family"=>#<Chargify::ProductFamily:0x007fe6548f6820 @attributes={"accounting_code"=>nil, "description"=>nil, "handle"=>"acme-online", "id"=>3, "name"=>"Acme Online"}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=false>, "credit_card"=>#<Chargify::Migration::CreditCard:0x007fe6548f5d58 @attributes={"type"=>"PaymentProfile", "billing_address"=>nil, "billing_address_2"=>nil, "billing_city"=>nil, "billing_country"=>nil, "billing_state"=>nil, "billing_zip"=>nil, "card_type"=>"bogus", "current_vault"=>"bogus", "customer_id"=>518, "customer_vault_token"=>nil, "expiration_month"=>1, "expiration_year"=>2023, "first_name"=>"Marky", "id"=>518, "last_name"=>"Mark", "masked_card_number"=>"XXXX-XXXX-XXXX-1", "vault_token"=>"1"}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=true, @remote_errors=nil, @validation_context=nil, @errors=#<ActiveResource::Errors:0x007fe656cc8320 @base=#<Chargify::Migration:0x007fe656cc8b90 ...>, @messages={}>>

# Preview a migration
preview = Migration::Preview.create(:subscription_id => subscription.id, :product_handle => "basic-plan")
# => #<Chargify::Migration::Preview:0x007fe65686fb98 @attributes={"product_id"=>20, "prorated_adjustment_in_cents"=>"-9899", "charge_in_cents"=>"2400", "payment_due_in_cents"=>"0", "credit_applied_in_cents"=>"-7499"}, @prefix_options={}, @persisted=false, @remote_errors=nil, @validation_context=nil, @errors=#<ActiveResource::Errors:0x007fe65686f260 @base=#<Chargify::Migration::Preview:0x007fe65686fb98 ...>, @messages={}>>

# Another way to preview a migration
preview = Migration.preview(:subscription_id => subscription.id, :product_handle => "basic-plan")
# => #<Chargify::Migration::Preview:0x007fe65686fb98 @attributes={"product_id"=>20, "prorated_adjustment_in_cents"=>"-9899", "charge_in_cents"=>"2400", "payment_due_in_cents"=>"0", "credit_applied_in_cents"=>"-7499"}, @prefix_options={}, @persisted=false, @remote_errors=nil, @validation_context=nil, @errors=#<ActiveResource::Errors:0x007fe65686f260 @base=#<Chargify::Migration::Preview:0x007fe65686fb98 ...>, @messages={}>>
