LoginSignup
6
6

More than 5 years have passed since last update.

何回やってもRails Stripeが実装出来ない

Posted at

実現したいこと

Railsにて作成中の簡易ECサイトにカード決済機能を追加

gem 'stripe'

一回戦

stripe.rb
Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
index.html.erb
  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
            data-description='商品のご購入'
            data-amount="<%= sum %>"
            data-currency="jpy"
            data-locale="auto"></script>

一通り公式に指定された形式で記述していざ決済画面に飛ぶが

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

エラー遭遇する。1回目。

二回戦

ENVでkeyを指定した際には.envファイルなるものをどこかに記述しなければならないと聞き、全くの無知識だったので検索をかけてapp内に.envを作成。

#api_key
PUBLISHABLE_KEY = 'pk_test 00000000000'
SECRET_KEY= 'sk_test_0000000000000'

しかし

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

エラーに遭遇する。2回目。

三回戦

マニュアル通りに記述しているのにエラーが起きるのはおかしいとググって同様の事例を発見。
valid publishable key set, yet get no valid publishable key setより

I actually added the publishable key in the new.html.erb like that:

<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>

の回答を参考に、data-keyの前にStripe.setPublishableKey('PUBLISHABLE_KEY');の記述を追加して実行。今度こそは行けるだろう・・。

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

エラーに遭遇。三回目。

4回戦

では.envを使わなければ良いのだ。
Rails Stripe サブスクリプション決済実装 初期設定をしてAPIを使える状態にするを参考に

config/secrets.yml
development:
  # stripe
  stripe_publishable_key: pk_test_********************
  stripe_secret_key: sk_test_********************
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.application.secrets.stripe_secret_key

新たにymlファイルを作成し、stripe.rbの記述内容を変更。そして

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

最終決戦

公式の記述を再び目に通してみることに。


<form action="your-server-side-code" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_3ps82fl2u34IurHC87is6g56"
    data-amount="999"
    data-name="ohayo"
    data-description="Example charge"
    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
    data-locale="auto"
    data-currency="jpy">
  </script>
</form>

!? data-keyに直接keyを書いてるぞ??
何度となくRails.configrationの記述を変更したりstripe.rbの内容を再三再四書き換えたとところで

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

有効なパブリッシュキーを設定していませんと出てくるのなら、直接記述すればいいのではと投げやりに記述。

index.html.erb
  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="pk_test_9999999999999999999"
            data-description='商品のご購入'
            data-amount="<%= sum %>"
            data-currency="jpy"
            data-locale="auto"></script>

そして・・エラーは消えていった。

Railsの謎

参考記述例・導入方法は以下になります。が、必ずエラーを起こす。
投稿画像のリサイズ時も散々苦労したわけだが、面白いくらい同様事例発生例が見つからないので
そういう星の元に生まれたのだ・・と諦め。
直接keyを記述しなくともいいように設定を加えてた筈..だが何か根本的な記述ミスがあったのではないかと思われる

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6