【问题标题】:valid publishable key set, yet get no valid publishable key set有效的可发布密钥集,但没有获得有效的可发布密钥集
【发布时间】:2017-03-01 12:02:08
【问题描述】:

我正在尝试从条纹网站作品中制作条纹结帐示例

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

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

您没有设置有效的可发布密钥。称呼 Stripe.setPublishableKey() 与您的可发布密钥。如需更多信息, 见https://stripe.com/docs/stripe.js

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

https://checkout.stripe.com/api/bootstrap?key=&locale=en-US 失败 加载资源:服务器响应状态为 400(错误请求)

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

在 2016-10-19 17:29:24 +0000 开始 GET "/" for 10.240.1.15 不能 从 10.240.1.15 渲染控制台!允许的网络:127.0.0.1, ::1, 127.0.0.0/127.255.255.255 ChargesController#new 处理为 HTML 布局/应用程序中呈现的费用/new.html.erb (0.5ms) 在 53 毫秒内完成 200 次 OK(查看次数:52.6 毫秒 | ActiveRecord:0.0 毫秒)

当我检查我的 html 源代码时,stripe-key 元标记没有任何内容?

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

如果需要更多信息,请询问,以便我发帖。

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.js 吗?
  • @Korben 我展示了我的 stripe.js 内容。我不知道更多
  • 在 stripe.rb 中,您将可发布密钥设置为 ENV['PUBLISHABLE_KEY'],但在元标记中它是 ENV["STRIPE_PUBLIC_KEY"] - 如果您使用 &lt;%= tag :meta, :name=&gt; 'stripe-key', :content =&gt; ENV["PUBLISHABLE_KEY"] %&gt;,会发生什么情况?
  • @MikeH 我试过了,还是一样的错误
  • 如果您将可发布密钥直接作为参数放入Stripe.setPublishableKey(而不是从元标记中获取值),您会得到同样的错误吗?

标签: ruby-on-rails stripe-payments


【解决方案1】:

我也遇到了同样的问题 要解决它,只需将此代码放入您的 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之后

【讨论】:

    【解决方案2】:

    我实际上是这样在 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>
    

    【讨论】:

      【解决方案3】:

      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

      【讨论】:

        猜你喜欢
        • 2018-10-18
        • 2020-10-12
        • 2016-01-01
        • 2021-09-22
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多