You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails集成Stripe报错:未设置有效的发布密钥

Fixing "you did not set a valid publishable key" Error in Stripe Checkout for Rails

Hey there, let's troubleshoot that frustrating publishable key error you're hitting with Stripe Checkout in your Rails app. Even though the tutorial doesn't explicitly call Stripe.setPublishableKey(), the root issue almost always boils down to your publishable key not being loaded correctly in the frontend, or a missing configuration step the tutorial assumes you've covered. Here's how to fix it:

1. Double-Check Your .env File Setup

  • Make sure your .env file (in the Rails root directory) has the correct publishable key entry:
    STRIPE_PUBLISHABLE_KEY=pk_test_your_actual_test_key_here
    
  • Verify you've added the dotenv-rails gem to your Gemfile (it's required to load environment variables from .env):
    gem 'dotenv-rails', groups: [:development, :test]
    
    Run bundle install if you haven't already.
  • Ensure .env isn't accidentally excluded by .gitignore (though it should be ignored for security, it needs to exist locally for your app to read the keys).

2. Confirm the Key is Passed to the Frontend

Newer Stripe Checkout implementations use the data-key attribute in the Stripe button script instead of Stripe.setPublishableKey(). Make sure your view file (e.g., app/views/charges/new.html.erb) correctly injects the environment variable:

<%= form_tag charges_path do %>
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="<%= ENV['STRIPE_PUBLISHABLE_KEY'] %>"
    data-amount="500"
    data-name="Your App Name"
    data-description="Sample Product"
    data-currency="usd">
  </script>
<% end %>
  • Quick test: Load the page in your browser, right-click > "View Page Source", and check if the data-key value matches your pk_test key. If it's empty or shows nil, your app isn't loading the environment variable properly.

3. Restart Your Rails Server

Environment variables from .env are only loaded when your Rails server starts. If you added or modified the key after starting the server, you need to restart it for changes to take effect.

4. Verify Your Stripe Initializer (Backend Configuration)

While frontend keys are handled via the view, your backend needs the secret key for API calls. Make sure you have a config/initializers/stripe.rb file with:

Stripe.api_key = ENV['STRIPE_SECRET_KEY']

This ensures your server can communicate with Stripe's API, but the frontend publishable key issue is separate—this just prevents additional backend errors.

5. Don't Mix Up Keys!

Double-check that you're using a publishable key (starts with pk_test) in the frontend, not a secret key (starts with sk_test). Secret keys should never be exposed in the frontend, and using one here will throw an invalid key error.


If you follow these steps, the error should disappear. The most common culprits are an unloaded environment variable (fixed by restarting the server) or a typo in your .env file.

内容的提问来源于stack exchange,提问作者Malus_khanot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:11:51