4
4

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 5 years have passed since last update.

Laravel で Stripe を使うなら key は .env に書く

4
Last updated at Posted at 2020-01-04

はじめに

「Laravel Stripe 実装」とかで検索すると、コントローラーに Key を直接書いて動かしている方が多い気がします。(自分もそうしてました...!!)

コントローラーに直接書いても動くのですが、デプロイとかで本番の Key に変換する時にいちいち command + shift + F とかで探すのも面倒ですし、ミスも出やすいし、手間なので、こういう本番環境で変えるだろう設定周りは .env に移動した方が良いと思いました。

.env ファイルに7行追加

.env
# テスト環境
STRIPE_PUBLIC_KEY=pk_test_************************
STRIPE_SECRET_KEY=sk_test_************************

# 本番環境
# STRIPE_PUBLIC_KEY=pk_live_************************
# STRIPE_SECRET_KEY=sk_live_************************

本番環境の時は、テスト環境の2つの key を # でコメントして、本番環境の2つの key の # を取ってあげましょう。

config フォルダに stripe.php ファイルを新しく作成

config/stripe.php

<?php

return [
    'pk_key' => env('STRIPE_PUBLIC_KEY'),
    'sk_key' => env('STRIPE_SECRET_KEY'),
];

Stripe を使っているコントローラーで呼び出し

app/Http/Controllers/StripeControlller.php

// public key (view 側に決済機能を表示させる時に必要)
$stripe_pk_key = config('stripe.pk_key');
dd(stripe_pk_key);

// secret key (支払いが発生した時の決済時に必要)
$stripe_sk_key = config('stripe.sk_key');
dd(stripe_pk_key);

さいごに

自分も、一回一回デプロイする時に、時間かかるし、めんどくさかったし、怖かったので、同じ思いをしている方のお役に立てれば幸いです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?