mal*_*avi 8 ruby-on-rails stripe-payments
我正在努力解决这个错误没有提供API密钥.使用"Stripe.api_key ="设置API密钥.按照Stripe的指南逐步执行后,您可以在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
Run Code Online (Sandbox Code Playgroud)
配置/初始化/ 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]
Run Code Online (Sandbox Code Playgroud)
终端跟踪在2014-12-12 22:15:08 +0100开始POST"/ 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'
Run Code Online (Sandbox Code Playgroud)
测试包括secretts.yml中的密钥@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"] %>
Run Code Online (Sandbox Code Playgroud)
您需要将条带键存储在环境变量中,以便config/initializers/stripe.rb可以读取它们.
在Rails 4.1+中,您可以使用secrets.yml:
development:
secret_key_base: key
publishable_key: pk_test_lkasjdflkajsd
secret_key: sk_test_lkasjdflkajsd
Run Code Online (Sandbox Code Playgroud)
注意:在YAML中定义嵌套键值对时,请使用两个空格.也就是说,下面的键development应缩进两个空格,而不是缩进字符.这是因为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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6458 次 |
| 最近记录: |