Set up your development environment
Stripe’s server-side helper libraries (also known as server-side SDKs) and command-line interface (CLI) allow you to interact with Stripe’s REST APIs. Start with the Stripe CLI and make Stripe API calls without writing a line of code.
Use the SDKs to avoid writing boilerplate code. To start sending requests from your environment, choose a language to follow a quickstart guide.
In this quickstart, you install the Stripe CLI—an essential tool that gets you command line access to your Stripe integration. You also install the Stripe Node.js server-side SDK to get access to Stripe APIs from applications written in Node.js.
What you learn
In this quickstart, you’ll learn:
- How to call Stripe APIs without writing a line of code
- How to manage third-party dependencies using the npm or yarn package manager
- How to install the latest Stripe Node SDK v15.0.0
- How to send your first SDK request
Initial setup
まず、Stripe アカウントを作成するかサインインします。
Set up the Stripe CLI
Install
コマンドラインから、インストールスクリプトを使用するか、オペレーティングシステムのバージョン管理されたアーカイブファイルをダウンロードして展開し、CLI をインストールします。
Authenticate
ログインして Stripe ユーザーアカウントを認証し、「制限付きのキー」のセットを生成します。詳細については、Stripe CLI キーと権限をご覧ください。
stripe login
キーボードの Enter キーを押し、ブラウザーでの認証プロセスを完了します。
Your pairing code is: enjoy-enough-outwit-win This pairing code verifies your authentication with Stripe. Press Enter to open the browser or visit https://dashboard.stripe.com/stripecli/confirm_auth?t=THQdJfL3x12udFkNorJL8OF1iFlN8Az1 (^C to quit)
Confirm setup
CLI のインストールが完了し、Create a product (商品を作成) への単一の API リクエストを作成できるようになりました。
stripe products create \ --name="My First Product" \ --description="Created with the Stripe CLI"
レスポンスオブジェクトの id
内で商品 ID を探し、次のステップのために保存しておきます。
すべて正常に機能する場合、コマンドラインに以下のレスポンスが表示されます。
{ "id":
, "object": "product","prod_LTenIrmp8Q67sa"
次に、Create a price を呼び出して、30 USD の価格を関連付けます。product
のプレースホルダーを商品 ID (例: prod_LTenIrmp8Q67sa
) に入れ替えます。
stripe prices create \ --unit-amount=3000 \ --currency=usd \ --product=
{{PRODUCT_ID}}
すべて正常に機能する場合、コマンドラインに以下のレスポンスが表示されます。
{ "id":
, "object": "price","price_1KzlAMJJDeE9fu01WMJJr79o"
Install the Node.js server-side SDK
最新バージョンの Stripe Node.js サーバー側 SDK は、v15.0.0 です。Node.js バージョン 12 以降をサポートします。
Node バージョンを確認する
node --version
Node を初期化する
npm init
ライブラリをインストールする
ライブラリのインストールには、Node のパッケージマネージャーである npm を使用します。
npm install stripe --save
npm を使用してライブラリをインストールすると、ライブラリはプロジェクトの package.json ファイルに自動的に依存関係として追加されます。以下に例を示します。
{ "name": "stripe-node-example", "version": "1.0.0", "description": "A Stripe demo", "main": "index.js", "scripts": { "node ": "node create_price.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "stripe": "^15.0.0" } }
インストールの代替手段
Run your first SDK request
Now that you have the Node.js SDK installed, you can create a subscription Product and attach a Price with a couple API requests. The Node.js SDK returns promises which can be used as chainable callbacks. To demonstrate, we’re passing the product identifier returned in the Product response to create a Price in this example.
注
const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc'); stripe.products.create({ name: 'Starter Subscription', description: '$12/Month subscription', }).then(product => { stripe.prices.create({ unit_amount: 1200, currency: 'usd', recurring: { interval: 'month', }, product: product.id, }).then(price => { console.log('Success! Here is your starter subscription product id: ' + product.id); console.log('Success! Here is your starter subscription price id: ' + price.id); }); });
Save the file as create_price.js
. From the command line, cd
to the directory containing the file you just saved and run:
node create_price.js
If everything worked, the command line shows the following response. Save these identifiers so you can use them while building your integration.
Success! Here is your starter subscription product id: prod_0KxBDl589O8KAxCG1alJgiA6 Success! Here is your starter subscription price id: price_0KxBDm589O8KAxCGMgG7scjb
参照情報
This wraps up the quickstart. See the links below for a few different ways to process a payment for the product you just created.