これからStripeを使ったアプリケーション開発をはじめたいという方にお勧めしたいのが、このstripe samples
コマンドです。
これはStripeが用意しているサンプルコードを、自分の環境にコピーできるコマンドです。
% stripe samples --help
Sample integrations built by Stripe
Usage:
stripe samples [command]
Available commands:
create Setup and bootstrap a Stripe Sample
list List Stripe Samples supported by the CLI
Flags:
-h, --help help for samples
Global flags:
--api-key string Your API key to use for the command
--color string turn on/off color output (on, off, auto)
--config string config file (default is
$HOME/.config/stripe/config.toml)
--device-name string device name
--log-level string log level (debug, info, warn, error)
(default "info")
-p, --project-name string the project name to read from for config
(default "default")
Use "stripe samples [command] --help" for more information about a command.
stripe samples list
でサンプルの種類を確認する
Stripeが用意しているサンプルは1つではありません。stripe samples list
コマンドを実行することで、様々なケースのサンプルプロジェクトを見ることができます。
% stripe samples list | head -n 30
A list of available Stripe Samples:
Loading...
accept-a-card-payment
Learn how to accept a basic card payment
Repo: https://github.com/stripe-samples/accept-a-card-payment
adding-sales-tax
Learn how to use PaymentIntents to build a simple checkout flow
Repo: https://github.com/stripe-samples/adding-sales-tax
au-becs-debit-payment
Collecting AU BECS Direct Debit mandates and payments
Repo: https://github.com/stripe-samples/au-becs-debit-payment
card-brand-choice
Supporting Card Brand Choice with Stripe.
Repo: https://github.com/stripe-samples/card-brand-choice
サンプルは「リポジトリ名」「説明文」「リポジトリURL」の3つで構成されています。GitHubのリポジトリにアクセスすると、説明やデモアプリのURLが記載されたREADME.mdが用意されています。
サンプルの検索は、GitHub上からも可能
v1.5.8時点では、stripe samples list
コマンドに検索機能はありません。そのため、「Rubyのコードサンプルが見たい」「subscriptionのサンプルを一通り試したい」のように目的がある程度固まっている場合は、stripe-samplesのGitHub Organizationで検索する方が早いこともあります。
stripe samples create
気になるリポジトリを見つけたら、GitHubやstripe samples list
コマンドで気になるサンプルを見つけた場合、stripe samples create <リポジトリ名>
でプロジェクトをコピーしましょう。
% stripe samples --project-name test create adding-sales-tax
✔ Finished downloading
Use the arrow keys to navigate: ↓ ↑ → ←
Webhookの設定や使用する言語が選択できる
ただコピーするだけであれば、git clone
と大差ありません。が、stripe samples create
の場合、対話式である程度設定をカスタマイズすることができます。
adding-sales-tax
など、Webhookを使用するサンプルが含まれている場合には、「Webhookあり版」「Webhookなし版」が選べます。
? What type of integration would you like to use:
▸ using-webhooks
without-webhooks
また、サンプルプロジェクトは高確率で複数の言語が用意されています。自分の使いたい言語を選んでcreateしましょう。
? What server would you like to use:
▸ java
node
php
python
ruby
createしたサンプルは、<リポジトリ名>ディレクトリ配下に配置される
下のサンプルは、adding-sales-tax
とcharging-a-saved-card
の2つをcreateした状態です。create
の引数で渡したものと同じディレクトリが作成され、その配下にファイルが配置されます。
% tree -I node_modules
.
├── adding-sales-tax
│ ├── LICENSE
│ ├── README.md
│ ├── client
│ │ ├── css
│ │ │ ├── global.css
│ │ │ └── normalize.css
│ │ ├── favicon.ico
│ │ ├── index.html
│ │ └── script.js
│ ├── package.json
│ ├── sales-tax-preview.png
│ └── server
│ ├── README.md
│ ├── package-lock.json
│ ├── package.json
│ └── server.js
└── charging-a-saved-card
├── LICENSE
├── README.md
├── charging-saved-card-diagram.png
├── charging-saved-card.gif
├── client
│ ├── css
│ │ ├── global.css
│ │ └── normalize.css
│ ├── favicon.ico
│ ├── images
│ │ ├── card-blocked.svg
│ │ ├── card-requires-auth.svg
│ │ ├── card-success.svg
│ │ └── chevon-down.svg
│ ├── index.html
│ └── script.js
├── package.json
└── server
├── README.md
├── composer.json
├── composer.lock
├── config.php
└── index.php
ディレクトリ名を指定したい場合は、stripe samples create <リポジトリ名> <作成したいディレクトリ名>
でコマンドを実行しましょう。
% stripe samples create charging-a-saved-card next
✔ Finished downloading
✔ Selected server: node
✔ Files copied
✔ Project configured
You're all set. To get started: cd next
create後、環境変数の設定がほぼ必須。README.mdを必ず読もう
stripe samples create
したプロジェクトは、ほぼ全てで環境変数にStripeのAPIキーをセットする必要があります。
.env.example
ファイルがプロジェクトルートに用意されている場合は、.env
ファイルを作成して自分のStripeアカウントのAPIキーなどを入力するようにしましょう。
% cat .env.example
# Stripe keys
STRIPE_PUBLISHABLE_KEY=pk_test_1234
STRIPE_SECRET_KEY=sk_test_1234
# Required to run webhook
# See README on how to use the Stripe CLI to setup
STRIPE_WEBHOOK_SECRET=whsec_1234
#Environment setup
STATIC_DIR=../../client
% cp .env.example .env
% vim .env
STRIPE_PUBLISHABLE_KEY``STRIPE_SECRET_KEY``STRIPE_WEBHOOK_SECRET
の3つはほとんどのサンプルで使用します。もし複数のサンプルを試されたい場合は、事前にdirenv
などで環境変数をセットしておくと入力の手間が省けます。