ruby-on-rails - strip 化-没有提供API key ?

标签 ruby-on-rails stripe-payments

我正在为这个错误而苦苦挣扎没有提供API key 。使用“Stripe.api_key =”设置您的API key 。您可以按照Stripe's guide的逐步操作,从Rails应用程序中的Stripe Web界面生成API key 。

从我看来,一切看起来都很好,但是它一直在返回该通知。有什么建议吗?

收费 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

config/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]

终端走线
在2014年12月12日22:15:08 +0100开始为127.0.0.1进行POST“/收费”
    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在注释中建议的secrets.yml中的 key ,但仍然是相同的问题:
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中定义嵌套键/值对时,恰好使用两个空格。也就是说,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]

关于ruby-on-rails - strip 化-没有提供API key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27452731/

相关文章:

html - 如何在 Rails 4 中修改 rails-bootstrap-forms tabindex

javascript - 在 Jest 中创建自定义转换

ruby-on-rails - 设计 Flash 方法 Rails

express - Stripe Checkout 不会重定向到 Express 结账页面

node.js - 如何使用Stripe NodeJS SDK获取更多客户源?

javascript - strip 错误 : 'Token is Not Supported' when trying to sign up to subscription

javascript - 每次提交时数据库条目数都会增加一倍

ruby-on-rails - 从belongs_to动态查找父对象

node.js - Stripe Node JS 在多个帐户中创建客户

javascript - Stripe 集成 - 具有结帐重定向功能的付款意图