有效的可发布密钥集,但未获得有效的可发布密钥集

valid publishable key set, yet get no valid publishable key set

我正在尝试从 stripe 网站制作 stripe checkout 样本

https://stripe.com/docs/checkout/rails

,但当我尝试付款时,我收到此 错误消息

You did not set a valid publishable key. Call Stripe.setPublishableKey() with your publishable key. For more info, see https://stripe.com/docs/stripe.js

在我的 JavaScript 控制台中,错误消息

https://checkout.stripe.com/api/bootstrap?key=&locale=en-US Failed to load resource: the server responded with a status of 400 (Bad Request)

在我的控制台中,错误消息

Started GET "/" for 10.240.1.15 at 2016-10-19 17:29:24 +0000 Cannot render console from 10.240.1.15! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by ChargesController#new as HTML Rendered charges/new.html.erb within layouts/application (0.5ms) Completed 200 OK in 53ms (Views: 52.6ms | ActiveRecord: 0.0ms)

当我查看我的html源代码stripe-key元标签没有任何内容?

条带示例使用他们自己的秘密和可发布的密钥,但我使用我的。

如果需要更多信息,请询问,这样我就可以post。

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Workspace</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag  'https://js.stripe.com/v2/' %> 
  <%= csrf_meta_tags %>
  <%= tag :meta, :name=> 'stripe-key', :content => ENV["STRIPE_PUBLIC_KEY"] %>
</head>
<body>

<%= yield %>

</body>
</html>

charges.html.erb

  <head>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

stripe.js

$(document).ready(function() {
    Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'));        
});

stripe.rb

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

stripe.rb 中,您从 ENV['PUBLISHABLE_KEY'] 设置 :publishable_key,而在 application.html.erb 中,您使用 ENV["STRIPE_PUBLIC_KEY"]。您可能应该更新 application.html.erb 以使用 Rails.configuration.stripe[:publishable_key] 代替:

  <%= tag :meta, :name=> 'stripe-key', :content => Rails.configuration.stripe[:publishable_key] %>

也就是说,问题很可能是您没有设置 environment variables。您可以尝试直接在 stripe.rb 中设置密钥吗?例如:

Rails.configuration.stripe = {
  :publishable_key => 'pk_...',
  :secret_key      => 'sk_...'
}

提醒一下,您可以在 Stripe 控制面板中找到 API 键:https://dashboard.stripe.com/account/apikeys.

我实际上是这样在 new.html.erb 中添加了可发布密钥:

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
      Stripe.setPublishableKey('PUBLISHABLE_KEY');
      data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
      data-description="A month's subscription"
      data-amount="100"
      data-locale="auto">
</script>

我也遇到了同样的问题, 要解决它,只需将此代码放入您的 stripe.rb 文件

config/initializers/stripe.rb

Rails.configuration.stripe = {
  publishable_key: Rails.application.secrets.stripe_publishable_key,
  secret_key: Rails.application.secrets.stripe_secret_key
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]

当然是在将 test_keys 放入 config/secrets.yml

之后