Reputation: 73
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".)
Upvotes: 0
Views: 1954
Reputation: 155
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
Upvotes: 0
Reputation: 73
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.
Upvotes: 0
Reputation: 527
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.
Upvotes: 3