# Amazon Pay によるサブスクリプションを設定する Amazon Pay を使用したサブスクリプションの作成と請求の方法をご紹介します。 このガイドを使用して、支払い方法として [Amazon Pay](https://docs.stripe.com/payments/amazon-pay.md) を使用する*サブスクリプション* (A Subscription represents the product details associated with the plan that your customer subscribes to. Allows you to charge the customer on a recurring basis)を設定します。 # Intents API を設定 > This is a Intents API を設定 for when api-integration is setupintents. View the full page at https://docs.stripe.com/billing/subscriptions/amazon-pay?api-integration=setupintents. 2 回の API コールを使用してサブスクリプションを作成し、確定します。[1 つ目の API コール](https://docs.stripe.com/billing/subscriptions/amazon-pay.md#create-setup-intent)は、[Setup Intents API](https://docs.stripe.com/api/setup_intents.md) を使用して、Amazon Pay を支払い方法として設定します。[2 つ目の API コール](https://docs.stripe.com/billing/subscriptions/amazon-pay.md#create-subscription)で、顧客、商品、支払い方法の情報を [Subscriptions API](https://docs.stripe.com/api/subscriptions.md) に送信し、1 回のコールでサブスクリプションを作成し、支払いを確定します。 ## 商品と価格を作成する [ダッシュボード] [Products (商品)](https://docs.stripe.com/api/products.md) は、販売しているアイテムまたはサービスを表します。[Prices (価格)](https://docs.stripe.com/api/prices.md) は、商品の価格と請求頻度を定義します。これには、商品の価格、受け付ける通貨、および 1 回限りの支払いか継続支払いかが含まれます。商品と価格が数個のみの場合は、ダッシュボードでそれらを作成および管理します。 このガイドでは、例としてストックフォトサービスを使用し、15 USD の月次サブスクリプションを顧客に請求します。これをモデル化するには、次のようにします。 1. [商品](https://dashboard.stripe.com/products?active=true)ページに移動し、**商品を作成**をクリックします。 1. 商品の**名前**を入力します。オプションで**説明**を追加して、商品の画像をアップロードできます。 1. **商品税コード**を選択します。[商品税コード](https://docs.stripe.com/tax/tax-codes.md)の詳細をご確認ください。 1. **継続**を選択します。次に、価格に**15**を入力し、通貨として**\**を選択します。 1. **価格に税金を含める**かどうかを選択します。[税金設定](https://dashboard.stripe.com/test/settings/tax)のデフォルト値を使用するか、値を手動で設定できます。この例では、**自動**を選択します。 1. **請求期間**で**月次**を選択します。 1. **その他の料金体系オプション**をクリックします。次に、この例の料金体系モデルとして**定額**を選択します。[定額料金](https://docs.stripe.com/products-prices/pricing-models.md#flat-rate)とその他の[料金体系モデル](https://docs.stripe.com/products-prices/pricing-models.md)の詳細をご確認ください。 1. 将来的に特定の価格を整理、クエリ、更新するために、内部**価格の説明**と[検索キー](https://docs.stripe.com/products-prices/manage-prices.md#lookup-keys) 追加します。 1. **次へ**をクリックします。次に、**商品を追加**をクリックします。 商品と価格を作成したら、価格 ID を記録しておき、後続のステップで使用できるようにします。ID は料金体系ページで `price_G0FvDp6vZvdwRZ` のように表示されます。 ## Customer を作成または取得する [サーバー側] 今後の支払いに備えて Amazon Pay の支払い方法を保存するには、これを *Customer* (Customer objects represent customers of your business. They let you reuse payment methods and give you the ability to track multiple payments) に関連付ける必要があります。 ビジネスで顧客がアカウントを作成した後に `Customer` オブジェクトを作成します。`Customer` オブジェクトの ID を自社固有の顧客の内部表記に関連付けることにより、後から保存済みの支払い方法情報を取得して使用できます。顧客がアカウントを作成していない場合でも `Customer` オブジェクトを作成して、後でその顧客のアカウントの内部表記に関連付けることができます。 ```curl curl https://api.stripe.com/v1/customers \ -u "<>:" \ --data-urlencode description="My First Test Customer (created for API docs)" ``` ## SetupIntent を作成する [サーバー側] [SetupIntent](https://docs.stripe.com/api/setup_intents.md) を作成して、今後の決済に備えて顧客の決済手段を保存します。[SetupIntent (支払い方法設定インテント)](https://docs.stripe.com/api/setup_intents.md) は、今後の決済に備えて顧客の支払い方法を設定する意図を表すオブジェクトです。SetupIntent は、この設定プロセスのステップを追跡します。 サーバー側で [payment_method_types](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-payment_method_types) を `amazon_pay` に設定して SetupIntent を作成し、Customer の ID と [usage=off_session](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-usage) または `usage=on_session` を指定します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d confirm=true \ --data-urlencode return_url="https://www.stripe.com" \ -d usage=off_session \ -d customer=cus_ODQluYFNl44ODI \ -d "payment_method_data[type]"=amazon_pay \ -d "payment_method_types[]"=amazon_pay \ -d "mandate_data[customer_acceptance][type]"=online \ -d "mandate_data[customer_acceptance][online][ip_address]"="127.0.0.0" \ -d "mandate_data[customer_acceptance][online][user_agent]"=device ``` SetupIntent オブジェクトには `client_secret` が含まれています。これは、同意書の承認を得るために買い手を Amazon Pay にリダイレクトする際に、クライアント側で Stripe.js に渡す必要がある一意のキーです。 ### client secret を取得する SetupIntent には、*client secret* (The client secret is a unique key returned from Stripe as part of a PaymentIntent. This key lets the client access important fields from the PaymentIntent (status, amount, currency) while hiding sensitive ones (metadata, customer)) が含まれています。これは、支払いプロセスを安全に完了するためにクライアント側で使用されます。client secret をクライアント側に渡す際は、いくつかの方法を使用できます。 #### 1 ページのアプリケーション ブラウザーの `fetch` 関数を使用して、サーバーのエンドポイントから client secret を取得します。この方法は、クライアント側が 1 ページのアプリケーションで、特に React などの最新のフロントエンドフレームワークで構築されている場合に最適です。client secret を処理するサーバーのエンドポイントを作成します。 #### Ruby ```ruby get '/secret' do intent = # ... Create or retrieve the SetupIntent {client_secret: intent.client_secret}.to_json end ``` その後、クライアント側で JavaScript を使用して client secret を取得します。 ```javascript (async () => { const response = await fetch('/secret'); const {client_secret: clientSecret} = await response.json(); // Render the form using the clientSecret })(); ``` #### サーバ側のレンダリング サーバーからクライアントに client secret を渡します。この方法は、アプリケーションがブラウザーへの送信前に静的なコンテンツをサーバーで生成する場合に最適です。 決済フォームに [client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) を追加します。サーバー側のコードで、SetupIntent から client secret を取得します。 #### Ruby ```erb
``` ```ruby get '/checkout' do @intent = # ... Fetch or create the SetupIntent erb :checkout end ``` 次に、[Stripe.js](https://docs.stripe.com/payments/elements.md) を使用してクライアントに Amazon Pay を保存します。 Stripe.js スクリプトを決済ページに含めるには、このスクリプトを HTML ファイルの `head` に追加します。 ```html Checkout ``` 顧客が Amazon Pay での支払いをクリックしたときに、Stripe.js を使用してその支払いを Stripe に送信します。[Stripe.js](https://docs.stripe.com/payments/elements.md) は、決済フローを構築するための基本的な JavaScript ライブラリです。このライブラリにより、以下で説明するリダイレクトなどの複雑な処理が自動的に行われ、他の決済手段にも対応できるように実装を拡張できます。Stripe.js スクリプトを決済ページに含めるには、HTML ファイルの `head` にこのスクリプトを追加します。 ```html Checkout ``` 決済ページで以下の JavaScript を使用して、Stripe.js のインスタンスを作成します。 ```javascript // Set your publishable key. Remember to change this to your live publishable key in production! // See your keys here: https://dashboard.stripe.com/apikeys const stripe = Stripe('<>'); ``` `stripe.confirmAmazonPaySetup` を使用し、[return_url](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-return_url) と [mandate_data](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-mandate_data) を指定して、クライアント側で SetupIntent を確定します。 [return_url](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-return_url) を使用して SetupIntent の成功後に顧客を指定のページにリダイレクトします。 ```javascript // Redirects away from the client const {error} = await stripe.confirmAmazonPaySetup( '{{SETUP_INTENT_CLIENT_SECRET}}', { return_url: 'https://example.com/setup/complete', mandate_data: { customer_acceptance: { type: 'online', online: { infer_from_client: true } } } } ); if (error) { // Inform the customer that there was an error. } ``` ## 定期支払いの作成 [サーバー側] 価格と顧客を含むサブスクリプションを作成します。`default_payment_method` パラメーターの値を SetupIntent から返された PaymentMethod ID に設定します。 ```curl curl https://api.stripe.com/v1/subscriptions \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d "items[0][price]"={{PRICE_ID}} \ -d default_payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true ``` 事前設定されたデフォルトの支払い方法があるため、サブスクリプションを作成すると顧客に自動的に請求されます。支払いが成功すると、Stripe ダッシュボードのステータスが**有効**に変わります。すでに設定済みの価格によって以降の請求額が決まります。[無料のトライアル期間を利用してサブスクリプションを作成](https://docs.stripe.com/billing/subscriptions/amazon-pay.md#trial-periods)する方法をご覧ください。 # Payment Intents API > This is a Payment Intents API for when api-integration is subscription. View the full page at https://docs.stripe.com/billing/subscriptions/amazon-pay?api-integration=subscription. 2 回の API コールを使用してサブスクリプションを作成し、確定します。[1 つ目の API コール](https://docs.stripe.com/billing/subscriptions/amazon-pay.md#pi-create-subscription)で顧客と商品の情報を [Subscriptions API](https://docs.stripe.com/api/subscriptions.md) に送信し、1 回のコールでサブスクリプションと PaymentIntent を作成します。レスポンスには PaymentIntent ID が含まれます。これを [Payment Intents API](https://docs.stripe.com/api/payment_intents.md) コールで使用して、[支払いを確定する](https://docs.stripe.com/billing/subscriptions/amazon-pay.md#pi-confirm-payment)必要があります。 ## 商品と価格を作成する [ダッシュボード] [Products (商品)](https://docs.stripe.com/api/products.md) は、販売しているアイテムまたはサービスを表します。[Prices (価格)](https://docs.stripe.com/api/prices.md) は、商品の価格と請求頻度を定義します。これには、商品の価格、受け付ける通貨、および 1 回限りの支払いか継続支払いかが含まれます。商品と価格が数個のみの場合は、ダッシュボードでそれらを作成および管理します。 このガイドでは、例としてストックフォトサービスを使用し、15 USD の月次サブスクリプションを顧客に請求します。これをモデル化するには、次のようにします。 1. [商品](https://dashboard.stripe.com/products?active=true)ページに移動し、**商品を作成**をクリックします。 1. 商品の**名前**を入力します。オプションで**説明**を追加して、商品の画像をアップロードできます。 1. **商品税コード**を選択します。[商品税コード](https://docs.stripe.com/tax/tax-codes.md)の詳細をご確認ください。 1. **継続**を選択します。次に、価格に**15**を入力し、通貨として**\**を選択します。 1. **価格に税金を含める**かどうかを選択します。[税金設定](https://dashboard.stripe.com/test/settings/tax)のデフォルト値を使用するか、値を手動で設定できます。この例では、**自動**を選択します。 1. **請求期間**で**月次**を選択します。 1. **その他の料金体系オプション**をクリックします。次に、この例の料金体系モデルとして**定額**を選択します。[定額料金](https://docs.stripe.com/products-prices/pricing-models.md#flat-rate)とその他の[料金体系モデル](https://docs.stripe.com/products-prices/pricing-models.md)の詳細をご確認ください。 1. 将来的に特定の価格を整理、クエリ、更新するために、内部**価格の説明**と[検索キー](https://docs.stripe.com/products-prices/manage-prices.md#lookup-keys) 追加します。 1. **次へ**をクリックします。次に、**商品を追加**をクリックします。 商品と価格を作成したら、価格 ID を記録しておき、後続のステップで使用できるようにします。ID は料金体系ページで `price_G0FvDp6vZvdwRZ` のように表示されます。 ## 定期支払いの作成 [サーバー側] [payment_behavior](https://docs.stripe.com/api/subscriptions/create.md#create_subscription-payment_behavior) パラメーターに `default_incomplete` の値を指定して、`incomplete` ステータスで価格と顧客を指定した [Subscription (サブスクリプション)](https://docs.stripe.com/api/subscriptions.md) を作成します。`payment_settings.save_default_payment_method=on_subscription` パラメーターを設定して、サブスクリプションが有効化されたときに支払い方法を保存します。 ```curl curl https://api.stripe.com/v1/subscriptions \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d payment_behavior=default_incomplete \ -d "items[0][price]"={{PRICE_ID}} \ -d "payment_settings[save_default_payment_method]"=on_subscription \ -d "payment_settings[payment_method_types][0]"=amazon_pay \ -d "payment_settings[payment_method_types][1]"=card \ -d "expand[0]"="latest_invoice.payments" \ -d "expand[1]"="latest_invoice.confirmation_secret" ``` レスポンスには、*サブスクリプション* (A Subscription represents the product details associated with the plan that your customer subscribes to. Allows you to charge the customer on a recurring basis)の最初の[請求書)](https://docs.stripe.com/api/invoices.md)が含まれます。これにはインボイスの支払いが含まれます。これには、Stripe がこのインボイスに対して生成したデフォルトの PaymentIntent と、PaymentIntent オブジェクト全体を渡す代わりにクライアント側で支払いプロセスを安全に完了するために使用できる Confirmation Secret が含まれます。支払いの確定に使用する必要がある PaymentIntent ID を `latest_invoice.payments` から取得します。`latest_invoice.confirmation_secret.client_secret` をフロントエンドに返して、支払いを完了します。[無料トライアル期間付きのサブスクリプションを作成](https://docs.stripe.com/billing/subscriptions/amazon-pay.md#trial-periods)する方法をご覧ください。 ## 支払いを確定する [サーバー側] サブスクリプションレスポンスの PaymentIntent ID を使用し、[PaymentIntents (支払いインテント)](https://docs.stripe.com/api/payment_intents.md) で支払いを確定します。その後、PaymentIntent ID を URL パスに追加して、`payment_method_types` パラメーターの値を `amazon_pay` に設定します。 ```curl curl https://api.stripe.com/v1/payment_intents/:id/confirm \ -u "<>:" \ -d "payment_method_data[type]"=amazon_pay \ --data-urlencode return_url="https://www.stripe.com" \ -d "mandate_data[customer_acceptance][type]"=online \ -d "mandate_data[customer_acceptance][online][ip_address]"="127.0.0.0" \ -d "mandate_data[customer_acceptance][online][user_agent]"=device \ -d "mandate_data[customer_acceptance][accepted_at]"=1660000000 ``` PaymentIntent のレスポンスにはステータス `requires_action` が含まれます。これは、PaymentIntent を完了するにはユーザーが Amazon Pay の認証を行う必要があることを示します。支払いが成功すると、サブスクリプションは有効になり、その支払い方法がデフォルトの支払い方法として保存されます。 # Stripe がオンラインで提供するページ > This is a Stripe がオンラインで提供するページ for when api-integration is checkout. View the full page at https://docs.stripe.com/billing/subscriptions/amazon-pay?api-integration=checkout. [Checkout API](https://docs.stripe.com/api/checkout/sessions.md) を使用すると、構築済みの決済ページでサブスクリプションを作成して確認できます。 ## 商品と価格を作成する [ダッシュボード] [Products (商品)](https://docs.stripe.com/api/products.md) は、販売しているアイテムまたはサービスを表します。[Prices (価格)](https://docs.stripe.com/api/prices.md) は、商品の価格と請求頻度を定義します。これには、商品の価格、受け付ける通貨、および 1 回限りの支払いか継続支払いかが含まれます。商品と価格が数個のみの場合は、ダッシュボードでそれらを作成および管理します。 このガイドでは、例としてストックフォトサービスを使用し、15 USD の月次サブスクリプションを顧客に請求します。これをモデル化するには、次のようにします。 1. [商品](https://dashboard.stripe.com/products?active=true)ページに移動し、**商品を作成**をクリックします。 1. 商品の**名前**を入力します。オプションで**説明**を追加して、商品の画像をアップロードできます。 1. **商品税コード**を選択します。[商品税コード](https://docs.stripe.com/tax/tax-codes.md)の詳細をご確認ください。 1. **継続**を選択します。次に、価格に**15**を入力し、通貨として**\**を選択します。 1. **価格に税金を含める**かどうかを選択します。[税金設定](https://dashboard.stripe.com/test/settings/tax)のデフォルト値を使用するか、値を手動で設定できます。この例では、**自動**を選択します。 1. **請求期間**で**月次**を選択します。 1. **その他の料金体系オプション**をクリックします。次に、この例の料金体系モデルとして**定額**を選択します。[定額料金](https://docs.stripe.com/products-prices/pricing-models.md#flat-rate)とその他の[料金体系モデル](https://docs.stripe.com/products-prices/pricing-models.md)の詳細をご確認ください。 1. 将来的に特定の価格を整理、クエリ、更新するために、内部**価格の説明**と[検索キー](https://docs.stripe.com/products-prices/manage-prices.md#lookup-keys) 追加します。 1. **次へ**をクリックします。次に、**商品を追加**をクリックします。 商品と価格を作成したら、価格 ID を記録しておき、後続のステップで使用できるようにします。ID は料金体系ページで `price_G0FvDp6vZvdwRZ` のように表示されます。 ## Checkout セッションを作成する [サーバー側] Stripe Checkout を使用した将来の支払いに Amazon アカウントを使用することを、顧客がお客様に許可する必要があります。これにより、Amazon による決済を受け付けることができます。サーバー側のエンドポイントを呼び出す決済ボタンをウェブサイトに追加して、[Checkout Session (セッション)](https://docs.stripe.com/api/checkout/sessions.md) を作成します。 ```html Checkout
``` `subscription` モードで Checkout セッションを作成して、必要な情報を収集します。Checkout セッションを作成したら、レスポンスで返される [URL](https://docs.stripe.com/api/checkout/sessions/object.md#checkout_session_object-url) に顧客をリダイレクトします。 ```curl curl https://api.stripe.com/v1/checkout/sessions \ -u "<>:" \ --data-urlencode success_url="https://example.com/success" \ -d "line_items[0][price]"={{RECURRING_PRICE_ID}} \ -d "line_items[0][quantity]"=1 \ -d "payment_method_types[0]"=card \ -d "payment_method_types[1]"=amazon_pay \ -d mode=subscription ``` ## 実装内容をテストする [サーバー側] 支払い方法として Amazon Pay を選択してから、「登録」をタップします。リダイレクトページで支払いを認証することで、支払いの成功ケースをテストできます。PaymentIntent のステータスが `requires_action` から `succeeded` に移行します。