Stripe - No API Key?

I am struggling with this error. An API key is not provided. Set the API key using "Stripe.api_key =". You can create API keys from the Stripe web interface in the Rails application after the following steps: Stripe guide .

From what I see, everything looks good, but he continues to return this notification. Any tips?

Payment controller:

class ChargesController < ApplicationController def new end def create # Amount in cents @amount = 500 customer = Stripe::Customer.create( :email => ' example@stripe.com ', :card => params[:stripeToken] ) charge = Stripe::Charge.create( :customer => customer.id, :amount => @amount, :description => 'Rails Stripe customer', :currency => 'usd' ) rescue Stripe::CardError => e flash[:error] = e.message redirect_to charges_path end end 

configurations / Initializers / stripe.rb

  Rails.configuration.stripe = { :publishable_key => ENV['pk_test_KEY'], :secret_key => ENV['sk_test_KEY'] } Stripe.api_key = Rails.configuration.stripe[:secret_key] 

Terminal route Started POST "/ charges" for 127.0.0.1 at 2014-12-12 22:15:08 +0100

  Processing by ChargesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXX", "stripeToken"=>"tok_1590kf2NNSl5uX0kXE9XXX", "stripeTokenType"=>"card", "stripeEmail"=>" USER@gmail.com "} Completed 500 Internal Server Error in 2ms Stripe::AuthenticationError - No API key provided. Set your API key using "Stripe.api_key = <API-KEY>". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email support@stripe.com if you have any questions.: () Users/javier/.rvm/gems/ruby-2.1.2/bundler/gems/stripe-ruby-9c7ebd21c973/lib/stripe.rb:71:in `request' () Users/javier/.rvm/gems/ruby-2.1.2/bundler/gems/stripe-ruby-9c7ebd21c973/lib/stripe/api_operations/create.rb:6:in `create' () Users/javier/Desktop/definitive/app/controllers/charges_controller.rb:10:in `create' 

Tested, including keys in secrets.yml, as @sealocal suggests in the comments, but still the same problem:

 development: secret_key_base: key publishable_key: anotherkey secret_key: anotherkey test: secret_key_base:key production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> publishable_key: <%= ENV["publishable_key"] %> secret_key: <%= ENV["secret_key"] %> 
+7
ruby-on-rails stripe-payments
source share
2 answers

You need to save the Stripe keys in environment variables so that config/initializers/stripe.rb can read them.

In Rails 4.1+, you can use secrets.yml :

 development: secret_key_base: key publishable_key: pk_test_lkasjdflkajsd secret_key: sk_test_lkasjdflkajsd 

NOTE. Use only two spaces when defining nested key-value pairs in YAML. That is, the keys under development should be indented with two spaces, and not with a tab character.

In config/initializers/stripe.rb :

 Rails.configuration.stripe = { :publishable_key => Rails.application.secrets.publishable_key :secret_key => Rails.application.secrets.secret_key } Stripe.api_key = Rails.configuration.stripe[:secret_key] 
+5
source share

Stripe support answered my email and their solution worked fine:

configurations / initializers / stripe.rb

  Rails.configuration.stripe = { :publishable_key => 'pk_test_thekey', :secret_key => 'sk_test_thekey' }  

Also part of their answer. It looks like you are trying to set the API key in an ENV variable named after your key, which is most likely not a valid key. You will want to change it to ENV ['NAME_OF_ENV_VAR_HERE'] or simply access the key directly, for example, the lines above

+1
source share

All Articles