我正在努力解决这个错误没有提供API密钥.使用“Stripe.api_key =”设置API密钥.按照步骤
Stripe’s guide,您可以在Rails应用程序中从Stripe Web界面生成API密钥.
从我看来,一切看起来都很好,但它不断回归那个通知.任何建议?
收费控制器:
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
配置/初始化/ 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]
终端跟踪
在2014-12-12 22:15:08 0100开始发布“/ charge”为127.0.0.1
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'
测试中包含了@sealocal在评论中建议的秘密密钥.但仍然是同样的问题:
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"] %>
解决方法
您需要将Stripe密钥存储在环境变量中,以便config / initializers / stripe.rb可以读取它们.
在Rails 4.1中,您可以使用secrets.yml
:
development: secret_key_base: key publishable_key: pk_test_lkasjdflkajsd secret_key: sk_test_lkasjdflkajsd
注意:在YAML中定义嵌套键值对时,恰好使用两个空格.也就是说,正在开发的键应缩进两个空格,而不是缩进字符.
在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]