2

I am getting this error when I submit a card with test info from the _card partial. The error is being generated within CGI.escape, here's the code:

URL-encode a string.
# url_encoded_string = CGI::escape("'Stop!' said Fred")
# # => "%27Stop%21%27+said+Fred"
def CGI::escape(string)
  encoding = string.encoding
  string.dup.force_encoding('ASCII-8BIT').gsub(/([^ a-zA-Z0-9_.-]+)/) do
  '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
  end.tr(' ', '+').force_encoding(encoding)
end

I've traced it back a few steps, and I'm pretty sure the error is stemming from the fact that the 'string' being passed in from here:

def retrieve(id, api_key=nil)
  api_key ||= @api_key
  response, api_key = Stripe.request(:get,"#{url}/#{CGI.escape(id)}", api_key)
  Util.convert_to_stripe_object(response, api_key)
end

is nil. 'retrieve' is called from Koudoku::Subscription#processing:

  self.stripe_id = customer.id
  self.last_four = customer.cards.retrieve(customer.default_card).last4
  finalize_new_subscription!
  finalize_upgrade!

It looks like customer.default_card is nil, which is leading to the error. What should this be? I think it is being set from Stripe:

          customer_attributes = {
            description: subscription_owner_description,
            email: subscription_owner_email,
            card: credit_card_token, # obtained with Stripe.js
            plan: plan.stripe_id
          }

          # create a customer at that package level.
          customer = Stripe::Customer.create(customer_attributes)

How can I fix this? Thanks!

EDIT: Add stack trace

Started POST "/koudoku/users/26/subscriptions" for 127.0.0.1 at 2014-03-07 15:53:50 -0500
Processing by Koudoku::SubscriptionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WHbDFjKiCFNr6mnPTwGvYdE321mC9WgKpUzwh2X1W5s=", "subscription"=>{"plan_id"=>"1"}, "owner_id"=>"26"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 26 ORDER BY "users"."id" ASC LIMIT 1
  Subscription Load (0.5ms)  SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."user_id" = $1 ORDER BY "subscriptions"."id" ASC LIMIT 1  [["user_id", 26]]
   (0.2ms)  BEGIN
  Plan Load (0.5ms)  SELECT "plans".* FROM "plans" WHERE "plans"."id" = $1 ORDER BY "plans"."id" ASC LIMIT 1  [["id", 1]]
   (0.3ms)  ROLLBACK
Completed 500 Internal Server Error in 1786ms

NoMethodError - undefined method `encoding' for nil:NilClass:
  /Users/my_name/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/cgi/util.rb:7:in `escape'
  stripe (1.10.1) lib/stripe/list_object.rb:19:in `retrieve'
  koudoku (0.0.11) app/concerns/koudoku/subscription.rb:95:in `processing!'
  activesupport (4.0.0) lib/active_support/callbacks.rb:387:in `_run__1944923345756159471__save__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  activerecord (4.0.0) lib/active_record/callbacks.rb:299:in `create_or_update'
  activerecord (4.0.0) lib/active_record/persistence.rb:106:in `save'
  activerecord (4.0.0) lib/active_record/validations.rb:51:in `save'
  activerecord (4.0.0) lib/active_record/attribute_methods/dirty.rb:32:in `save'
  activerecord (4.0.0) lib/active_record/transactions.rb:270:in `block (2 levels) in save'
  activerecord (4.0.0) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
  activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction'
  activerecord (4.0.0) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
  activerecord (4.0.0) lib/active_record/transactions.rb:270:in `block in save'
  activerecord (4.0.0) lib/active_record/transactions.rb:281:in `rollback_active_record_state!'
  activerecord (4.0.0) lib/active_record/transactions.rb:269:in `save'
  koudoku (0.0.11) app/controllers/koudoku/subscriptions_controller.rb:100:in `create'
  actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (4.0.0) lib/active_support/callbacks.rb:443:in `_run__4271750995081991206__process_action__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
  activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
  actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
  actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/railtie/configurable.rb:30:in `method_missing'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
  meta_request (0.2.8) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
  rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
  meta_request (0.2.8) lib/meta_request/middlewares/headers.rb:16:in `call'
  meta_request (0.2.8) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__3592197274752226705__call__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (1.0.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (1.0.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (1.0.1) lib/better_errors/middleware.rb:56:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/my_name/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /Users/my_name/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /Users/my_name/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
jackerman09
  • 2,492
  • 5
  • 29
  • 46
  • show error stack.. pls – Arup Rakshit Mar 07 '14 at 20:34
  • @ArupRakshit, I've added the stack trace, thanks again – jackerman09 Mar 07 '14 at 20:55
  • From where you are calling `CGI::escape` method ? – Arup Rakshit Mar 07 '14 at 20:58
  • @ArupRakshit, it is being called by the Koudoku gem. This is called retrieve on customer.default_card (https://github.com/andrewculver/koudoku/blob/master/app/concerns/koudoku/subscription.rb), which is nil, and the `retrieve` function is in turn is calling `CGI.escape` (you can see the detail above). – jackerman09 Mar 07 '14 at 21:02
  • @ArupRakshit, any suggestions on what's causing this error and/or how I can resolve it? Thanks again – jackerman09 Mar 08 '14 at 19:38
  • 1
    @ArupRakshit, damn, thanks anyway. I'm fully stuck on this, and I can't roll out if users can't subscribe – jackerman09 Mar 09 '14 at 15:26
  • Hey @jackerman09 did you ever get this resolved? – bnussey Oct 16 '14 at 15:46
  • @bnussey if I remember correctly, it was related to the fact that the rails code was being run before the Koudoku JS was being called on application.html. This was a while ago, so I'm not 100% sure, but I think moving the JS include up to before the rails made the difference. – jackerman09 Oct 17 '14 at 03:41
  • thanks for your reply @jackerman09 are you happy with Koudoku? – bnussey Oct 17 '14 at 09:25
  • 1
    @bnussey, for the most part, yes. It was fairly easy to set up (once I got past a few snags), it works well, and it is regularly maintained. If you want to see it in action, this is where I used it whatkeyamiin.com – jackerman09 Oct 17 '14 at 13:35
  • Can you please make 110% sure `turbolinks` is NOT active in your application? – yas4891 Jan 19 '15 at 19:08

0 Answers0