RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.
http://railsapps.github.io/rails-stripe-membership-saas
1.14k stars 232 forks source link

Stripe no longer returns active_card. #82

Closed v9n closed 11 years ago

v9n commented 11 years ago

In users model. line 61 we have

self.last_4_digits = customer.active_card.last4

Stripe changes their response object: https://stripe.com/docs/api?lang=ruby#retrieve_customer We no longer have active_card so this will lead to an error. I update the code to use the new card data with this line

self.last_4_digits = customer.cards.data.first.last4
v9n commented 11 years ago

Thanks for taking a look Daniel.

When running RSpec, the data is not created actually, but simulating returning from a JSON file spec/support/fixtures/success.json

class Response
         def self.new(stripe_response)
            stripe_response_file = JSON.parse(IO.read("spec/support/fixtures/#{stripe_response}.json"))
           StripeHelper::NestedOstruct.new(stripe_response_file)
          end
        end
{"id":"youAreSuccessful","account_balance":0,"created":1355683240,"delinquent":false,"description":"asdjfasdf","discount":null,"email":"bla@ajsdf.com","livemode":false,"object":"customer","active_card":{"address_city":null,"address_country":null,"address_line1":null,"address_line1_check":null,"address_line2":null,"address_state":null,"address_zip":null,"address_zip_check":null,"country":"US","cvc_check":"pass","exp_month":1,"exp_year":2016,"fingerprint":"g8KJu3wg7rq95y8a","last4":"4242","name":"asdjfasdf","object":"card","type":"Visa"},"subscription":{"cancel_at_period_end":false,"canceled_at":null,"current_period_end":1358361640,"current_period_start":1355683240,"customer":"cus_0vdcpqNJS2YMtm","ended_at":null,"object":"subscription","quantity":1,"start":1355683240,"status":"active","trial_end":null,"trial_start":null,"plan":{"id":"silver","amount":900,"currency":"usd","interval":"month","interval_count":1,"livemode":false,"name":"silver","object":"plan","trial_period_days":null}}}

Here is my log in production mode when real user is actually created:


NoMethodError - undefined method `active_card' for #<Stripe::Customer:0x007fd9deeaf6b8>:
  stripe (1.8.3) lib/stripe/stripe_object.rb:148:in `method_missing'
  app/models/user.rb:61:in `update_stripe'
  activesupport (3.2.13) lib/active_support/callbacks.rb:418:in `_run__789304781485017414__save__1690525790321514017__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  activerecord (3.2.13) lib/active_record/callbacks.rb:264:in `create_or_update'
  activerecord (3.2.13) lib/active_record/persistence.rb:84:in `save'
  activerecord (3.2.13) lib/active_record/validations.rb:50:in `save'
  activerecord (3.2.13) lib/active_record/attribute_methods/dirty.rb:22:in `save'
  activerecord (3.2.13) lib/active_record/transactions.rb:259:in `block (2 levels) in save'
  activerecord (3.2.13) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
  activerecord (3.2.13) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
  activerecord (3.2.13) lib/active_record/transactions.rb:208:in `transaction'
  activerecord (3.2.13) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
  activerecord (3.2.13) lib/active_record/transactions.rb:259:in `block in save'
  activerecord (3.2.13) lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
  activerecord (3.2.13) lib/active_record/transactions.rb:258:in `save'
  devise (2.2.4) app/controllers/devise/registrations_controller.rb:15:in `create'
  actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.13) lib/active_support/callbacks.rb:436:in `_run__3445538358834422996__process_action__3258164263932542086__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'

In order to make sure RSpec running ok, We also need to fix success.json.

{"object":"customer","created":1373955688,"id":"youAreSuccessful","livemode":false,"description":"asdjfasdf","email":"bla@ajsdf.com","delinquent":false,"subscription":{"plan":{"interval":"month","name":"Silver","amount":900,"currency":"usd","id":"silver","object":"plan","livemode":false,"interval_count":1,"trial_period_days":15},"object":"subscription","start":1373955688,"status":"trialing","customer":"cus_2CrZQp2cVLDLjd","cancel_at_period_end":false,"current_period_start":1373955688,"current_period_end":1375251688,"ended_at":null,"trial_start":1373955688,"trial_end":1375251688,"canceled_at":null,"quantity":1},"discount":null,"account_balance":0,"cards":{"object":"list","count":1,"url":"/v1/customers/cus_2CrZQp2cVLDLjd/cards","data":[{"id":"cc_2CrZUbQKIg7Pyp","object":"card","last4":"4242","type":"Visa","exp_month":1,"exp_year":2016,"fingerprint":"h7OEHTVeEFYHFl1n","customer":"cus_2CrZQp2cVLDLjd","country":"US","name":"asdjfasdf","address_line1":null,"address_line2":null,"address_city":null,"address_state":null,"address_zip":null,"address_country":null,"cvc_check":"pass","address_line1_check":null,"address_zip_check":null}]},"default_card":"cc_2CrZUbQKIg7Pyp"}

I updated pull request that will pass the test.

Looking at Stripe Doc: https://stripe.com/docs/api/ruby#create_customer, the return code change:

Stripe::Customer.create( :description => "Customer for test@example.com", :card => "tok_27HzrB8d5s8xhe" # obtained with Stripe.js ) 

#<Stripe::Customer id=cus_2CrZQp2cVLDLjd 0x00000a> JSON: { "object": "customer", "created": 1373955688, "id": "cus_2CrZQp2cVLDLjd", "livemode": false, "description": "Vinh Nguyen", "email": "kurei@axcoto.com", "delinquent": false, "subscription": { "plan": { "interval": "month", "name": "Silver", "amount": 599, "currency": "usd", "id": "noty-silver", "object": "plan", "livemode": false, "interval_count": 1, "trial_period_days": 15 }, "object": "subscription", "start": 1373955688, "status": "trialing", "customer": "cus_2CrZQp2cVLDLjd", "cancel_at_period_end": false, "current_period_start": 1373955688, "current_period_end": 1375251688, "ended_at": null, "trial_start": 1373955688, "trial_end": 1375251688, "canceled_at": null, "quantity": 1 }, "discount": null, "account_balance": 0,

 "cards": { "object": "list", "count": 1, "url": "/v1/customers/cus_2CrZQp2cVLDLjd/cards",
 "data": [ { "id": "cc_2CrZUbQKIg7Pyp", "object": "card", "last4": "4242", "type": "Visa", "exp_month": 1, "exp_year": 2016, "fingerprint": "h7OEHTVeEFYHFl1n", "customer": "cus_2CrZQp2cVLDLjd", "country": "US", "name": "Vinh Nguyen", "address_line1": null, "address_line2": null, "address_city": null, "address_state": null, "address_zip": null, "address_country": null, "cvc_check": "pass", "address_line1_check": null, "address_zip_check": null } ] },

 "default_card": "cc_2CrZUbQKIg7Pyp" }
☁  notyim [master] ⚡ rake spec
/Users/kureikain/.rvm/rubies/ruby-1.9.3-p448/bin/ruby -S rspec ./spec/controllers/content_controller_spec.rb ./spec/controllers/home_controller_spec.rb ./spec/controllers/users_controller_spec.rb ./spec/mailers/user_mailer_spec.rb ./spec/models/user_spec.rb ./spec/stripe/stripe_config_spec.rb
............................

Finished in 0.84027 seconds
28 examples, 0 failures

Randomized with seed 21452

☁  notyim [master] ⚡

Here is the change log: https://stripe.com/docs/upgrades#2013-07-05

2013-07-05

    Customer objects now include a cards sublist and a default_card id in place of a fully expanded active_card. The default_card attribute can be expanded into a card object when retrieving the customer but is not expanded by default.
DanielKehoe commented 11 years ago

Very good. Thanks!