chargify / chargify_api_ares

A Chargify API wrapper for Ruby using ActiveResource
http://chargify.com
MIT License
161 stars 95 forks source link

Reactivating subscription throws ActiveResource error #66

Closed kavitakanojiya closed 10 years ago

kavitakanojiya commented 11 years ago

Hello, 1.) When Chargify marks a subscription as unpaid, in our app, we trying to reactivate this unpaid subscription. But unfortunately, its throwing an error as: ActiveResource::ServerError: Failed. Response code = 500. Response message = Internal Server Error. Previously, it used to used as intended but it isnt working.

2.) I used ' .reactivate ' instance method to reactivate a subscription.

3.) Here is the subscription details: => #<Chargify::Subscription:0x0000000a5696b8 @attributes={"activated_at"=>2013-06-19 08:52:10 UTC, "balance_in_cents"=>100, "cancel_at_end_of_period"=>false, "canceled_at"=>nil, "cancellation_message"=>nil, "created_at"=>2013-06-19 08:52:09 UTC, "current_period_ends_at"=>2013-07-12 12:40:00 UTC, "expires_at"=>nil, "id"=>3493174, "next_assessment_at"=>2013-07-12 12:44:14 UTC, "payment_collection_method"=>"automatic", "state"=>"unpaid", "trial_ended_at"=>nil, "trial_started_at"=>nil, "updated_at"=>2013-07-11 12:44:14 UTC, "current_period_started_at"=>2013-07-11 12:40:00 UTC, "previous_state"=>"unpaid", "signup_payment_id"=>34842396, "signup_revenue"=>"1.00", "delayed_cancel_at"=>nil, "coupon_code"=>nil, "total_revenue_in_cents"=>2000, "customer"=>#<Chargify::Customer:0x0000000a566008 @attributes={"address"=>nil, "address_2"=>nil, "city"=>nil, "country"=>nil, "created_at"=>2013-06-19 08:52:08 UTC, "email"=>"cancel@kiprosh.com", "first_name"=>"Cancel", "id"=>3345855, "last_name"=>"again", "organization"=>nil, "phone"=>nil, "reference"=>"8429524", "state"=>nil, "updated_at"=>2013-06-19 08:52:08 UTC, "zip"=>nil}, @prefix_options={}, @persisted=false>, "product"=>#<Chargify::Product:0x0000000a4dc1f0 @attributes={"accounting_code"=>"slevel5", "archived_at"=>nil, "created_at"=>2012-12-04 08:36:51 UTC, "description"=>"Please ignore this subscription. Generated to test whether user's subscription get canceled at the end of subscription period.\n- On realvolvebeta.com", "expiration_interval"=>nil, "expiration_interval_unit"=>"never", "handle"=>"slevel5", "id"=>3276893, "initial_charge_in_cents"=>0, "interval"=>1, "interval_unit"=>"day", "name"=>"S-Level 5", "price_in_cents"=>100, "request_credit_card"=>true, "require_credit_card"=>true, "return_params"=>nil, "return_url"=>nil, "trial_interval"=>nil, "trial_interval_unit"=>"month", "trial_price_in_cents"=>nil, "update_return_url"=>nil, "updated_at"=>2013-06-19 08:51:05 UTC, "product_family"=>#<Chargify::ProductFamily:0x0000000a4a6a78 @attributes={"accounting_code"=>nil, "description"=>nil, "handle"=>"singleagent", "id"=>225940, "name"=>"Single Agent"}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=false>, "credit_card"=>#<Chargify::Subscription::CreditCard:0x0000000a48fc60 @attributes={"type"=>"PaymentProfile", "billing_address"=>"Oberoi Garden", "billing_address_2"=>nil, "billing_city"=>"Mumbai", "billing_country"=>"AU", "billing_state"=>"ACT", "billing_zip"=>"400084", "card_type"=>"american_express", "current_vault"=>"bogus", "customer_id"=>3345855, "customer_vault_token"=>nil, "expiration_month"=>1, "expiration_year"=>2013, "first_name"=>"Cancel", "id"=>2088667, "last_name"=>"again", "masked_card_number"=>"XXXX-XXXX-XXXX-0002", "vault_token"=>"370000000000002"}, @prefix_options={}, @persisted=false>}, @prefix_options={}, @persisted=true>

4.) So here when I call subscription.reactivate, it gives out stack trace: subs.reactivate ActiveResource::ServerError: Failed. Response code = 500. Response message = Internal Server Error. from /app/vendor/bundle/ruby/1.9.1/gems/activeresource-3.2.12/lib/active_resource/connection.rb:148:in handle_response' from /app/vendor/bundle/ruby/1.9.1/gems/activeresource-3.2.12/lib/active_resource/connection.rb:115:inrequest' from /app/vendor/bundle/ruby/1.9.1/gems/activeresource-3.2.12/lib/active_resource/connection.rb:92:in block in put' from /app/vendor/bundle/ruby/1.9.1/gems/activeresource-3.2.12/lib/active_resource/connection.rb:218:inwith_auth' from /app/vendor/bundle/ruby/1.9.1/gems/activeresource-3.2.12/lib/active_resource/connection.rb:92:in put' from /app/vendor/bundle/ruby/1.9.1/gems/activeresource-3.2.12/lib/active_resource/custom_methods.rb:102:input' from /app/vendor/bundle/ruby/1.9.1/gems/chargify_api_ares-0.5.2/lib/chargify_api_ares/resources/subscription.rb:49:in reactivate' from (irb):8 from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:47:instart' from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in start' from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:41:in<top (required)>' from script/rails:6:in require' from script/rails:6:in

'

Will you please advise what am I doing wrong in this? Or do I need to use some other method to reactivate a subscription ?

Thanks

jeremywrowe commented 11 years ago

Hi @kavitakanojiya -

Can you please provide the initialization of the Chargify ares gem here (with your credentials blanked out) To get an over all picture of what is going on it would be helpful to see what is going on from start to finish. For best result it might be worth opening a trouble ticket (if you haven't already) at help.chargify.com. Please ask our level 1 support to direct it my way.

Thanks!

Jeremy

kavitakanojiya commented 11 years ago

Hello Jeremy, Thanks for your quick reply. I have already opened a ticket for this on help.chargify.com. Even I have started hear to hear from help team.

Apart from this: 1.) From Initializer file: require 'chargify_api_ares' begin

loads in the data from the YAML file in the config directory

chargify_config = YAML::load_file(File.join(Rails.root, 'config', 'chargify.yml'))
# configure Chargify for the existing Rails environment
Chargify.configure do |c|
  c.subdomain = chargify_config[Rails.env]['subdomain']
  c.api_key = chargify_config[Rails.env]['api_key']
end

rescue nil end

2.) Using Chargify methods: -> Finding subscription: subscription = Chargify::Subscription.find_by_customer_reference(user_id) -> where user_id is a reference_id -> updating CC details: subscription.credit_card_attributes = { first_name: info[:first_name], last_name: info[:last_name], expiration_month: info[:date][:month].try(:to_i), expiration_year: info[:date][:year].try(:to_i), full_number: info[:card_number] }

-> Saving subscription: subscription.save -> Now trying to reactivate unpaid subscription: subscription.reactivate Here it throws error as ' ActiveResource::ServerError: Failed. Response code = 500. Response message = Internal Server Error '

For development and testing, Chargify provides test CC numbers which ideally gives successful transaction, this is too failing. If you need more trace, please let me know.

Thanks.

jonathansimmons commented 10 years ago

The only reason I know of for reactivate call to fail is that the card on file is bad or bogus.

The Chargify gem should be returning a Subscription object with and errors message but unfortunately it does not. They fixed this recently for when a charge failed via #67 but even that fix took over a year and they have continued to overlook the issue when reactivating subscription despite my multiple calls/reports.

@kavitakanojiya In the meantime you can simply rescue the ActiveResource failure inline.

begin
  subscription.reactivate # attempt to reactivate subscription
rescue ActiveResource::ResourceInvalid => error
  # do something here a a result of the failure. 
end
jeremywrowe commented 10 years ago

@jonathansimmons / @kavitakanojiya

Sorry for the delay, I have made the appropriate fixes to the endpoint and several other non restful endpoints for the subscription resource found in this commit: https://github.com/chargify/chargify_api_ares/commit/7aa988c282d07ab30bf8ca2bcf0af2747850d3fd and version 1.0.5 and greater

you can now do something like this (as expected):

subscription = Chargify::Subscription.first # assuming subscription is 'active'
subscription.reactivate
if subscription.errors.any?
  # handle validation errors here
 subscription.errors.full_messages # => ["Cannot reactivate a subscription that is not marked \"Canceled\", \"Unpaid\", or \"Trial Ended\"."]
end