0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

クレジットカード決済用JavaScript準備 ⑨環境設定2

Posted at

JavaScriptで環境変数を呼び込みましょう

Railsで設定した環境変数をJavaScriptで呼び出せません。その際は、下記のようにwebpackerを用いることで呼び出せます。

####1)以下のコマンドを実行してwebpacker.rbを作成してください。

% touch config/initializers/webpacker.rb

####2)config/initializers/webpacker.rb

config/initializers/webpacker.rb
Webpacker::Compiler.env["PAYJP_PUBLIC_KEY"] = ENV["PAYJP_PUBLIC_KEY"]

####3)javascript/card.jsを編集

app/javascript/card.js
const pay = () => {
 Payjp.setPublicKey(process.env.PAYJP_PUBLIC_KEY);

これで、環境変数を呼び出すことができました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?