这个问题已被问到,但使用 Stripe Checkout 的格式似乎已经改变,以及加密凭证的格式(secrets.yml 到credentials.yml)
所以在开发过程中我们一直使用测试密钥并且没有任何问题。但现在我们已经部署到 Heroku,每次尝试导航到 Stripe Checkout 时它都会崩溃。 Heroku 日志中的错误看起来非常简单:
021-01-27T15:41:02.784429+00:00 app[web.1]: I, [2021-01-27T15:41:02.784365 #4] INFO -- : [88208302-394a-4144-b0d7-61fc69369ce7] Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms | Allocations: 379)
2021-01-27T15:41:02.785456+00:00 app[web.1]: F, [2021-01-27T15:41:02.785380 #4] FATAL -- : [88208302-394a-4144-b0d7-61fc69369ce7]
2021-01-27T15:41:02.785459+00:00 app[web.1]: [88208302-394a-4144-b0d7-61fc69369ce7] 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 [email protected] if you have any questions.):
我运行 EDITOR=vimrailscredentials:edit 来编辑credentials.yml.enc
在那里我有:
aws:
access_key_id: xxx
secret_access_key: xxx
stripe:
stripe_secret_key: sk_live_xxx
stripe_publishable_key: pk_live_xxx
secret_key_base: xxx
在我的 stripe.rb 文件中,我有:
Rails.configuration.stripe = {
:publishable_key => Rails.application.credentials.dig(:stripe, :stripe_publishable_key),
:secret_key => Rails.application.credentials.dig(:stripe, :stripe_secret_key)
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
当我进入rails console以及rails console --environment = Production并检查Stripe.api_key时 它完美地返回准确的条带密钥。我不知道为什么错误显示没有提供 Stripe API 密钥。我不明白什么?这是完成我的第一个“专业”网络应用程序(朋友的在线商店)的最后一步。
我明白了!就像 v3nkman 所建议的那样,我检查了 heroku 控制台是否有
Rails.application.credentials.stripe
。它没有返回任何内容,而开发控制台正在返回密钥。事实证明,当我们最初部署 Heroku 时,我在 Heroku 上设置了两个环境变量:PUBLISHABLE_KEY
和 SECRET_KEY
。但 Stripe.rb 文件中的 stripe.api_key
指向 Rails.application.credentials.dig(:stripe, :stripe_secret_key)
。在 Heroku 中这是 null 。所以我将 stripe.rb 更改为
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
最初它不起作用,但那是因为来自前端的Javascript请求仍然使用测试密钥。一旦我将其更改为实时密钥,它就起作用了
我怀疑环境系统在所有生产情况下都无法正常运行,在我的情况下,控制台输出了正确的值,但使用日志变量调试技术我看到了空值 从这里
查看我的答案