0

I am working on integrating Stripe with my Rails app. Per Stripe's documentation, I've added the following to 'config/initializers/stripe.rb', and set the environmental variables to the test keys:

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

(Editing to add that as suggested, I changed this to the following)

Rails.configuration.stripe = {
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'] ||=  Rails.application.secrets.stripe_publishable_key,
:secret_key => ENV['STRIPE_SECRET_KEY'] ||= Rails.application.secrets.stripe_secret_key,
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

When I try to do anything now (generate a controller, say, or run the server), I get an error saying I have an "undefined method `secret_key'":

 $ rails s
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
   /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/stripe-rails-0.3.1/lib/stripe/engine.rb:20:in `block in <class:Engine>': undefined method `secret_key' for #<Hash:0x000000066aa1e8> (NoMethodError)
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
    from /home/ubuntu/workspace/junkyardstorefinal/config/environment.rb:5:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /home/ubuntu/workspace/junkyardstorefinal/config.ru:3:in `block in <main>'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:55:in `instance_eval'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:55:in `initialize'
    from /home/ubuntu/workspace/junkyardstorefinal/config.ru:in `new'
    from /home/ubuntu/workspace/junkyardstorefinal/config.ru:in `<main>'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:49:in `eval'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:49:in `new_from_string'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/builder.rb:40:in `parse_file'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/server.rb:208:in `app'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/server.rb:61:in `app'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/rack-1.6.0/lib/rack/server.rb:336:in `wrapped_app'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/server.rb:78:in `start'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from /home/ubuntu/workspace/junkyardstorefinal/bin/rails:8:in `require'
    from /home/ubuntu/workspace/junkyardstorefinal/bin/rails:8:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `load'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client/rails.rb:27:in `call'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client/command.rb:7:in `call'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/client.rb:26:in `run'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/bin/spring:48:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `load'
    from /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/spring-1.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
    from /home/ubuntu/workspace/junkyardstorefinal/bin/spring:13:in `require'
    from /home/ubuntu/workspace/junkyardstorefinal/bin/spring:13:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

What other data would be helpful to anyone who can help? That's the entirety of stripe.rb up there, and I can't find anywhere else that I've used "secret_key" or anywhere else that it appears in the tutorial I was originally working through. Nor have I touched any of the files mentioned in the full trace.

Oddly, although I can't start the server on Cloud9, when I use Heroku to look at it, the app launches and works just fine -- other than a problem connecting to Redis that prevents it from displaying the user's cart.

(Updating to add: now that I've merged the branch I was on and tried to push the merged master branch, which means Heroku compiles it now, I find that I also can't push to Heroku from the master branch without getting "NoMethodError: undefined method `secret_key' for #" and "push rejected".)

3 Answers3

3

I think your error is the ENV['PUBLISHABLE_KEY'] and ENV['SECRET_KET'] is nil.

To solve your problem. In the terminal

$export PUBLISHABLE_KEY=yourpublisablekey
$export SECRET_KEY=yoursecretkey
$rails s

I would like to encourage you to put your key's in your secret.yml like

development:
  stripe_publishable_key: yourpublisablekey
  stripe_secret_key: yoursecretkey
production:
  stripe_publishable_key: yourpublisablekey
  stripe_secret_key: yoursecretkey

You can access this by simple adding Rails.application.secrets.stripe_publishable_key and Rails.application.secrets.stripe_secret_key

For example in your config/initializers/stripe.rb

Rails.configuration.stripe = {
   :publishable_key => ENV['PUBLISHABLE_KEY'] ||= Rails.application.secrets.stripe_publishable_key,
   :secret_key      => ENV['SECRET_KEY'] ||= Rails.application.secrets.stripe_secret_key
}

Hope it helps.

Archie Reyes
  • 527
  • 2
  • 14
  • 1
    To know more about `||=` just follow this [link](http://stackoverflow.com/questions/995593/what-does-or-equals-mean-in-ruby) – Archie Reyes Apr 28 '15 at 09:58
  • Dang it! That is a\ohttp://stackoverflow.com/questions/29816002/how-to-fix-this-error-undefined-method-encoding-for-nilnilclassa-and-get-c0oiu – Danica Stone Apr 28 '15 at 16:22
  • My cat walked on the keyboard, and now it's too late to edit it! That is a great answer and after implementing all of it (both secret.yml & exporting it), I still get the same error. I tried futzing around with 'Stripe.api_key = Rails.configuration.stripe[:secret_key]' but even experimentally deleting it left me with the same error. I did find someone getting the same error, in a different Stripe situation, here (http://stackoverflow.com/questions/29816002/how-to-fix-this-error-undefined-method-encoding-for-nilnilclassa-and-get-c); maybe the error is misleading? – Danica Stone Apr 28 '15 at 16:40
  • A kind of progress: I uninstalled the stripe-rails gem and deleted it from my Gemfile because I had seen a suggestion in the Stripe irc channel to do so when encountering this problem. I realized I had added the api keys to secret.yml instead of secrets.yml (d'oh) and fixed the problem stated above, but instead started getting "undefined method 'stripe'". I replaced stripe-rails, and... now I'm getting "uninitialized constant Devise (NameError)". /bangs head/ Now to go through the many StackOverflow postings about that error.... – Danica Stone Apr 28 '15 at 18:31
  • All right, progress dismantled. I picked my way through the other error messages, only to find that they were apparently all sitting on top of this one. I have tried storing my keys in secret.yml, exporting them, putting them in .bashrc, and sticking them in .env using the gem dotenv. I still get "undefined method 'secret_key'". – Danica Stone Apr 28 '15 at 21:42
  • 1
    have you tried using `export PUBLISHABLE_KEY=yourpublishablekey`, you can check if your `ENV['PUBLISHABLE_KEY']` is nil by going to the terminal, type `echo $PUBLISHABLE_KEY` and it will return your publishable_key. If publishable_key is not nil your env will work. – Archie Reyes Apr 29 '15 at 02:32
  • I did! And it does return both my publishable and secret keys correctly. It isn't giving me an error saying that the keys are nil (as it did at one point), it says 'NoMethodError: undefined method `secret_key' for # /tmp/build_ec5dbd64ad68fcc1a3067e730ca91147/vendor/bundle/ruby/2.0.0/gems/stripe-rails-0.3.1/lib/stripe/engine.rb:20:in `block in ' – Danica Stone Apr 29 '15 at 16:04
  • Oh I see! What I exported was STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY, but I needed to export them without "stripe". I've now exported secret_key specifically, and instead of "undefined method" etc. I'm now getting /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/uri/common.rb:176:in `split': bad URI(is not URI?): (URI::InvalidURIError) – Danica Stone Apr 29 '15 at 16:25
  • ...Which is a problem with Redis... and when I tweak the url for that in config/initializers/redis.rb, I'm right back to /usr/local/rvm/gems/ruby-2.1.5@rails4/gems/stripe-rails-0.3.1/lib/stripe/engine.rb:20:in `block in ': undefined method `secret_key' for # (NoMethodError) – Danica Stone Apr 29 '15 at 17:19
  • 2
    Try asking another question for redis. Sorry can't help you with redis. – Archie Reyes Apr 30 '15 at 03:55
0

It turned out that the solution was to take out the last line of stripe.rb entirely. I commented it out at the suggestion of someone over at CodeNewbie, to make sure that it was actually setting my stripe configurations. Surprisingly, once I commented that line out ( 'Stripe.api_key = Rails.configuration.stripe[:secret_key]'), everything started working perfectly.

0

Years later I came across a similar issue with stripe-rails, using Rails 5.2.1

The solution was to add the keys to credentials, as per current rails standard, and instead of using an initializer 'stripe.rb' I used the development.rb and production.rb files inside the envirnoments folder.

So, on my development.rb I have something like this:

Rails.configuration.stripe.secret_key = Rails.application.credentials.stripe[:dev_secret_key]  
Rails.configuration.stripe.publishable_key = Rails.application.credentials.stripe[:dev_publishable_key]

And similar code for production.rb

Pedro Coutinho
  • 156
  • 1
  • 5