Stripe Live Publishable API Key不正确

时间:2015-04-08 21:32:48

标签: ruby-on-rails initialization stripe-payments

我正在使用CareerFoundry's教程设置使用Rails创建Stripe Checkout。我在表单提交时收到公共API不正确的错误。我认为问题出现在下面的初始化文件中,但它可能是其他内容。

配置/初始化/ stripe.rb

if Rails.env.production?
  Rails.configuration.stripe = {
    publishable_key: ENV[ 'STRIPE_PUBLISHABLE_KEY' ],
    secret_key:      ENV[ 'STRIPE_SECRET_KEY' ]
  }
else
  Rails.configuration.stripe = {
    publishable_key: 'pk_test_UQ2EqhNNQRrDkDouuZ1xgpS5', #Both test keys are fakes in case you're wondering
    secret_key:      'sk_test_hkiYUThcriCTBfHuUSXpUP7n'
  }
end

付款控制器

class PaymentsController < ApplicationController
  def create #You want might to make actions more specific.
    token = params[:stripeToken] #The token when the form is posted includes the stripe token in the url.
    # Create the charge in stripes servers.  This is what commits the transaction.
    begin
      charge = Stripe::Charge.create(
        :amount => 200,
        :currency => "usd",
        :source => token,
        :description => params[:stripeEmail]
        )
      rescue Stripe::CardError => e 
        #The card was decline because of number/ccv error, expired error, bank decline, or zip error

        body = e.json_body
        err = body[:error]
        flash[:error] = "There was an error in processing your card: #{err[:message]}"
    end
    respond_to do |format|
      format.html { redirect_to "/confirmation" }
      #format.html { redirect_to "/purchase", notice: "Purchase was successfully completed.  We'll be in contact shortly!" }
    end
  end
end

查看/共享/ _stripe_checkout_button.html.erb

<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
  data-image="/assets/square-image.png"
  data-name="Achieve More"
  data-description="1 Month ($800)"
  data-amount="2000">
</script>

查看/ payments.html.erb

<%= form_tag "/payments" do %>
  <%= render partial: "shared/stripe_checkout_button" %>
<% end %>

1 个答案:

答案 0 :(得分:2)

所以CareerFoundry教程没有关于设置键的任何内容。条纹演示确实如此。这一行:

heroku config:set PUBLISHABLE_KEY=pk_test_UQ2EqhNNQRrDkD5V0Z1xgpS5 SECRET_KEY=sk_test_hkiYUTQzHiCTBfHuUSXpUP7n

设置初始值设定项的键。那些都丢失了,导致错误。