条纹 - 没有提供API密钥?

时间:2014-12-12 21:54:44

标签: ruby-on-rails stripe-payments

我正在努力解决这个错误没有提供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'

测试包括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"] %>

2 个答案:

答案 0 :(得分:5)

您需要将条带键存储在环境变量中,以便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中定义嵌套键值对时,只使用两个空格。也就是说,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]

答案 1 :(得分:1)

条纹支持回复了我的电子邮件,他们的解决方案正常运行:

<强>配置/初始化/ stripe.rb

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

也是他们答案的一部分看起来您正在尝试将API密钥设置为以您的密钥命名的ENV变量,这可能不是有效密钥。您需要将其更改为ENV [&#39; NAME_OF_ENV_VAR_HERE&#39;],或直接访问该键,如上面的行