# 将来の PayPal 支払いを設定する PayPal 詳細を保存し、後で顧客に請求する方法を紹介します。 > #### Accounts v2 API を使用した顧客の表現 > > Accounts v2 API では、Connect ユーザーには一般提供され、その他の Stripe ユーザーには公開プレビューで提供されます。Accounts v2 プレビューの一部である場合は、コードで[プレビューバージョン](https://docs.stripe.com/api-v2-overview.md#sdk-and-api-versioning)を指定する必要があります。 > > Accounts v2 プレビューへのアクセスをリクエストするには、 > > ほとんどのユースケースでは、[Customer](https://docs.stripe.com/api/customers.md) オブジェクトを使用するのではなく、[顧客を顧客設定済みの Account オブジェクトとしてモデル化する](https://docs.stripe.com/accounts-v2/use-accounts-as-customers.md)ことをお勧めします。 将来の PayPal 決済を設定して、サブスクリプション、支払いの遅延、効率的な購入に備えるための顧客の支払い情報を保存します。Stripe を介して PayPal による継続支払いを有効にし、使用する方法をご紹介します。 ## 継続支払いの有効化 Stripe ではほとんどの場合、ユーザーが Stripe ダッシュボードで [PayPal 決済を有効化](https://docs.stripe.com/payments/paypal/activate.md)すると、継続支払いが自動的に有効になります。ただし、PayPal のポリシーや地域別の制限により、一部のユーザーは継続支払いを手動で有効にしなければならない場合があります。この例外には、自動有効化が導入される前にアカウントを設定したユーザーも含まれます。継続支払いを手動で有効にするには、以下の手順に従います。 1. [決済手段の設定](https://dashboard.stripe.com/settings/payment_methods)に移動します。 1. **継続支払い**セクションで**PayPal** > **有効にする**をクリックします。 継続支払いを有効にすると、ダッシュボードに**保留中**と表示されます。アクセスできるようになるまでには通常、最大 5 営業日かかります。 アクセス権を付与すると、[PayPal 設定](https://dashboard.stripe.com/settings/payment_methods)で継続支払いを利用できるようになります。テスト環境では、継続支払いはデフォルトで有効になっています。 [Stripe Checkout](https://docs.stripe.com/payments/checkout.md) を使用して、事前に PayPal 支払いの詳細を収集し、後から最終的な金額や支払い日を決定します。この機能の用途を紹介します。 - 支払い方法をウォレットに保存して、以降の購入を効率化する - サービスの提供後に追加料金を回収する - [サブスクリプションの無料トライアルを開始する](https://docs.stripe.com/billing/subscriptions/trials.md) ## Stripe を設定する [サーバー側] まず、Stripe アカウントが必要です。[今すぐご登録ください](https://dashboard.stripe.com/register)。 アプリケーションから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。 #### Ruby ```bash # Available as a gem sudo gem install stripe ``` ```ruby # If you use bundler, you can add this line to your Gemfile gem 'stripe' ``` ## 設定前に Customer を作成または取得する [サーバー側] 以降の支払いに PayPal の支払い方法を再利用するには、*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 -X POST https://api.stripe.com/v1/customers \ -u "<>:" ``` ## Checkout セッションを作成する [クライアント側] [サーバー側] PayPal 支払いを受け付ける前に、顧客は、お客様が Stripe Checkout を通じて今後の支払いに PayPal アカウントを使用することを承認する必要があります。 サーバー側のエンドポイントを呼び出す決済ボタンをウェブサイトに追加して [Checkout Session (セッション)](https://docs.stripe.com/api/checkout/sessions.md) を作成します。 ```html Checkout
``` `setup` モードで Checkout セッションを作成して、必要な情報を収集します。Checkout セッションを作成したら、レスポンスで返された [URL](https://docs.stripe.com/api/checkout/sessions/object.md#checkout_session_object-url) に顧客をリダイレクトします。 #### curl ```bash curl https://api.stripe.com/v1/checkout/sessions \ -u <>: \ -d "payment_method_types[]"="paypal" \ -d mode=setup \ -d customer="{{CUSTOMER_ID}}" \ -d success_url="https://example.com/success?session_id={CHECKOUT_SESSION_ID}" \ ``` 顧客は支払い方法の詳細を指定すると、`success_url` にリダイレクトされます。これはお客様のウェブサイト上にあり、支払い方法が正常に保存されたことを顧客に知らせるページです。上記の例のように、`success_url` に `{CHECKOUT_SESSION_ID}` テンプレート変数を含めて、成功ページでセッション ID を使用できるようにします。 > 決済開始の検出にあたっては、`success_url` へのリダイレクトのみに依存しないでください。その理由は次のとおりです。 > > - 悪意を持つユーザが、支払いをせずに `success_url` に直接アクセスし、商品やサービスにアクセスできるようになる可能性があります。 - 決済が成功した後で、顧客が `success_url` にリダイレクトされる前にブラウザタブを閉じる可能性あります。 ## 支払い方法を取得する [サーバー側] 顧客が支払いの詳細を送信した後に、[PaymentMethod (決済手段)](https://docs.stripe.com/payments/payment-methods.md) オブジェクトを取得します。*PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) は、顧客のPayPal account情報を保存して今後の支払いに利用できるようにします。PaymentMethod は、`success_url` を使用して同期的に取得することも、*Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) を使用して非同期的に取得することもできます。 顧客が支払いの成功後に必ず `success_url` に到達するとは限らないため (たとえば、リダイレクトが行われる前に顧客がブラウザータブを閉じることもあります)、PaymentMethod を同期的に取得するか非同期的に取得するかは、ドロップオフの許容度によって異なります。Webhook を使用すると、組み込みでこの種のドロップオフを防止できます。 #### Webhook Session オブジェクトを含む `checkout.session.completed` Webhook を処理します。詳細については、[Webhook を設定する](https://docs.stripe.com/webhooks.md)をご覧ください。以下は、`checkout.session.completed` レスポンスの例です。 ```json { "id": "evt_1Ep24XHssDVaQm2PpwS19Yt0", "object": "event", "api_version": "2019-03-14", "created": 1561420781, "data": { "object": { "id": "cs_test_MlZAaTXUMHjWZ7DcXjusJnDU4MxPalbtL5eYrmS2GKxqscDtpJq8QM0k", "object": "checkout.session", "billing_address_collection": null, "client_reference_id": null, "customer": null, "customer_email": null, "display_items": [], "mode": "setup","setup_intent": "seti_1EzVO3HssDVaQm2PJjXHmLlM", "submit_type": null, "subscription": null, "success_url": "https://example.com/success" } }, "livemode": false, "pending_webhooks": 1, "request": { "id": null, "idempotency_key": null }, "type": "checkout.session.completed" } ``` Checkout Session で作成された SetupIntent の ID である `setup_intent` キーの値を書き留めます。[SetupIntent](https://docs.stripe.com/payments/setup-intents.md) は、今後の支払いに利用できるように顧客のPayPal account情報を設定するために使用されるオブジェクトです。この ID を使用して SetupIntent オブジェクトを[取得](https://docs.stripe.com/api/setup_intents/retrieve.md)します。返されるオブジェクトには `payment_method` ID が含まれます。 ```curl curl https://api.stripe.com/v1/setup_intents/seti_1EzVO3HssDVaQm2PJjXHmLlM \ -u "<>:" ``` #### 成功時の URL ユーザーがリダイレクトによってサイトに戻ったら、URL から `session_id` を取得し、Session オブジェクトを[取得](https://docs.stripe.com/api/checkout/sessions/retrieve.md) します。 ```curl curl -G https://api.stripe.com/v1/checkout/sessions/{{SESSION_ID}} \ -u "<>:" \ -d "expand[]=setup_intent" ``` > URL から `session_id` を確実に使用できるようにするには、Checkout セッションを作成する際に `success_url` に `session_id={CHECKOUT_SESSION_ID}` テンプレート変数を含めます。 Checkout Session 中に作成された SetupIntent を書き留めます。[SetupIntent](https://docs.stripe.com/payments/setup-intents.md) は、今後の支払いに利用できるように顧客のPayPal account情報を設定するために使用されるオブジェクトです。返されるオブジェクトには `payment_method` ID が含まれます。 ## 設定後のイベントを処理する [サーバー側] 顧客が請求契約を正常に承認したことを確認するには、顧客が支払いステータスページに戻るのを期待するのではなく、[Webhook](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) などの方法を使用します。利用者が請求契約を正常に承認すると、SetupIntent は [setup_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.succeeded) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信します。請求契約の承認が正常に行われなかった場合、SetupIntent は [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) Webhook イベントを送信し、`requires_payment_method` ステータスに戻ります。利用者が PayPal アカウントから請求契約を取り消すと、[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) が送信されます。 ## 構築したシステムをテストする [テスト API キー](https://docs.stripe.com/keys.md#test-live-modes) を使用して PayPal の実装をテストするには、リダイレクトページを表示します。リダイレクトページで支払いを認証することにより、支払い成功のケースをテストできます。PaymentIntent は `requires_action` から `succeeded` に移行します。 ユーザが認証に失敗するケースをテストするには、テスト API キーを使用してリダイレクトページを表示します。リダイレクトページで **テスト支払い失敗** をクリックします。PaymentIntent は、`requires_action` から `requires_payment_method` に移行します。 ## 支払い方法を将来の支払いに使用する [サーバー側] オフセッションで顧客に請求する準備ができたら、*Customer* (Customer objects represent customers of your business. They let you reuse payment methods and give you the ability to track multiple payments) ID と *PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) ID を使用して、[PaymentIntent (支払いインテント)](https://docs.stripe.com/api/payment_intents.md) を作成します。 請求する `paypal` の手段を見つけるには、Customer に関連付けられた PaymentMethod を[リスト](https://docs.stripe.com/api/payment_methods/list.md)表示します。 ```curl curl -G https://api.stripe.com/v1/payment_methods \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d type=paypal ``` Customer ID と PaymentMethod ID を取得したら、支払いの金額と通貨を使用して PaymentIntent を作成します。その他のいくつかのパラメーターを設定して、*オフセッション支払い* (A payment is described as off-session if it occurs without the direct involvement of the customer, using previously-collected payment information)を行います。 - [off_session](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-off_session) を `true` に設定して、支払いの実行時に顧客が決済フローに存在しないことを示します。これにより、認証が必要な場合は PaymentIntent からエラーが返されます。 - PaymentIntent の [confirm](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-confirm) プロパティの値を `true` に設定します。これにより、PaymentIntent が作成されると直ちに確定されます。 - [payment_method](https://docs.stripe.com/api.md#create_payment_intent-payment_method) を PaymentMethod の ID に設定し、[customer](https://docs.stripe.com/api.md#create_payment_intent-customer) を Customer の ID に設定します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ -d confirm=true ``` ## ユーザー主導の支払い方法のキャンセル [サーバー側] 顧客は、PayPal アカウントを通じてサブスクリプション(請求契約)をキャンセルできます。その際、Stripeは[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) Webhook を発行します。保存された支払い方法を使用する以降のすべての PaymentIntent は、有効な同意書を持つ支払い方法に変更するまで失敗します。サブスクリプションの支払いが失敗すると、ステータスは[自動回収設定](https://docs.stripe.com/invoicing/automatic-collection.md) で設定したサブスクリプションのステータスに変更されます。顧客に失敗を通知し、[別の支払い方法で支払う](https://docs.stripe.com/billing/subscriptions/overview.md#requires-payment-method)。 ## Optional: 保存された PayPal アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した PayPal アカウントを削除できます。PayPal の決済手段の関連付けを解除すると、[同意書](https://docs.stripe.com/api/mandates.md)が取り消され、さらに関連付けられた PayPal 請求契約をキャンセルするための PayPal API の呼び出しも行われます。 ```curl curl -X POST https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/detach \ -u "<>:" ``` # ダイレクト API > This is a ダイレクト API for when payment-ui is direct-api. View the full page at https://docs.stripe.com/payments/paypal/set-up-future-payments?payment-ui=direct-api. [Setup Intents](https://docs.stripe.com/api/setup_intents.md) を使用して事前に PayPal 支払い方法の詳細を収集し、後から最終的な金額や支払い日を決定します。この機能は、次の場合に使用します。 - 支払い方法をウォレットに保存して、以降の購入を効率化する - サービスの提供後に追加料金を回収する - [サブスクリプションの無料トライアルを開始する](https://docs.stripe.com/billing/subscriptions/trials.md) ## Stripe を設定する [サーバー側] まず、Stripe アカウントが必要です。[今すぐご登録ください](https://dashboard.stripe.com/register)。 アプリケーションから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。 #### Ruby ```bash # Available as a gem sudo gem install stripe ``` ```ruby # If you use bundler, you can add this line to your Gemfile gem 'stripe' ``` ## 設定前に Customer を作成または取得する [サーバー側] 以降の支払いに PayPal の支払い方法を再利用するには、*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 -X POST https://api.stripe.com/v1/customers \ -u "<>:" ``` ## SetupIntent を作成する [サーバー側] [SetupIntent (支払い方法設定インテント)](https://docs.stripe.com/api/setup_intents.md) は、将来の支払いに備えて顧客の支払い方法を設定するという意図を示し、そのステップを追跡するオブジェクトです。 サーバーで [SetupIntent](https://docs.stripe.com/api/setup_intents.md) を作成し、[payment_method_types](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-payment_method_types) を `paypal` に設定して、顧客が設定した [Account](https://docs.stripe.com/api/v2/core/accounts/object.md#v2_account_object-id) または [Customer](https://docs.stripe.com/api/customers/object.md#customer_object-id) の ID を指定します。 #### Accounts v2 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer_account={{CUSTOMERACCOUNT_ID}}" \ -d "payment_method_types[]=paypal" \ -d "payment_method_data[type]=paypal" ``` #### Customers v1 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d "payment_method_types[]=paypal" \ -d "payment_method_data[type]=paypal" ``` SetupIntent オブジェクトには、[client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) が含まれています。これは、買い手を PayPal にリダイレクトして同意書の承認を得る際に、クライアント側で Stripe に渡す必要がある一意のキーです。 ## 顧客をリダイレクトする [クライアント側] 顧客が将来の支払いに備えて PayPal アカウントを設定する際に、[Stripe.js](https://docs.stripe.com/js.md) を使用して SetupIntent を確定することをお勧めします。Stripe.js は、決済フローを構築するための Stripe の基本的な 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('<>', {} ); ``` クライアント側で設定を確認するには、ステップ 3 で作成した SetupIntent オブジェクトの client secret を渡します。 client secret は、Stripe API リクエストを認証する API キーとは異なります。client secret は決済を完了できるため、慎重に取り扱う必要があります。ログに記録したり、URL に埋め込んだり、顧客以外に漏洩したりしないでください。 ### PayPal 設定を確認する 将来の支払いに PayPal アカウントを使用することを許可するため、顧客は PayPal 請求契約ページにリダイレクトされ、そのページで契約を承認してから、お客様のウェブサイトに戻る必要があります。[stripe.confirmPayPalSetup](https://docs.stripe.com/js/setup_intents/confirm_paypal_setup) を使用して、お客様のページからのリダイレクトを処理して設定を完了します。この機能に `return_url` を追加し、ユーザーが PayPal のウェブサイトで請求契約を承認した後にリダイレクトされる場所を指定します。 ```javascript // Redirects away from the client const {error} = await stripe.confirmPayPalSetup( '{{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. } ``` 決済手段の支払人 ID と請求契約 ID は、[payment_method_details](https://docs.stripe.com/api/mandates/object.md#mandate_object-payment_method_details-paypal) プロパティで生成される [Mandate (同意書)](https://docs.stripe.com/api/mandates/.md) にあります。買い手のメールアドレスと支払人 ID は、[PaymentMethod (決済手段)](https://docs.stripe.com/api/payment_methods.md) の [paypal](https://docs.stripe.com/api/payment_methods/object.md#payment_method_object-paypal) プロパティでも確認できます。 | フィールド | 値 | | ---------------------- | -------------------------------------------------------------------- | | `payer_email` | PayPal アカウントの支払人のメールアドレス。 | | `payer_id` | 支払人の PayPal アカウントの一意の ID。 | | `billing_agreement_id` | PayPal 請求契約 ID (BAID)。これは、PayPal によって生成される ID で、ビジネスと顧客の間の同意書に相当します。 | ## Webhook を監視する [サーバー側] 顧客が請求契約を正常に承認したことを確認するには、顧客が支払いステータスページに戻るのを期待するのではなく、[Webhook](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) などの方法を使用します。利用者が請求契約を正常に承認すると、SetupIntent は [setup_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.succeeded) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信します。請求契約の承認が正常に行われなかった場合、SetupIntent は [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) Webhook イベントを送信し、`requires_payment_method` ステータスに戻ります。利用者が PayPal アカウントから請求契約を取り消すと、[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) が送信されます。 ## 保存された PayPal 支払い方法でオフセッションの支払いに請求する [サーバー側] オフセッションで顧客に請求する準備ができたら、*Customer* (Customer objects represent customers of your business. They let you reuse payment methods and give you the ability to track multiple payments) ID と *PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) ID を使用して、[PaymentIntent (支払いインテント)](https://docs.stripe.com/api/payment_intents.md) を作成します。 請求する `paypal` の手段を見つけるには、Customer に関連付けられた PaymentMethod を[リスト](https://docs.stripe.com/api/payment_methods/list.md)表示します。 ```curl curl -G https://api.stripe.com/v1/payment_methods \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d type=paypal ``` Customer ID と PaymentMethod ID を取得したら、支払いの金額と通貨を使用して PaymentIntent を作成します。その他のいくつかのパラメーターを設定して、*オフセッション支払い* (A payment is described as off-session if it occurs without the direct involvement of the customer, using previously-collected payment information)を行います。 - [off_session](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-off_session) を `true` に設定して、支払いの実行時に顧客が決済フローに存在しないことを示します。これにより、認証が必要な場合は PaymentIntent からエラーが返されます。 - PaymentIntent の [confirm](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-confirm) プロパティの値を `true` に設定します。これにより、PaymentIntent が作成されると直ちに確定されます。 - [payment_method](https://docs.stripe.com/api.md#create_payment_intent-payment_method) を PaymentMethod の ID に設定し、[customer](https://docs.stripe.com/api.md#create_payment_intent-customer) を Customer の ID に設定します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ -d confirm=true ``` ## 保存された PayPal 支払い方法でオンセッションの支払いに請求する [クライアント側] オンセッションで顧客に請求する準備ができたら、*Customer* (Customer objects represent customers of your business. They let you reuse payment methods and give you the ability to track multiple payments) ID と *PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) ID を使用して、[PaymentIntent (支払いインテント)](https://docs.stripe.com/api/payment_intents.md) を作成します。 請求する `paypal` 手段を見つけるには、[Customer に関連付けられた PaymentMethod を一覧表示](https://docs.stripe.com/api/payment_methods/list.md)します。 ```curl curl -G https://api.stripe.com/v1/payment_methods \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d type=paypal ``` Customer ID と PaymentMethod ID を取得したら、支払いの金額と通貨を指定して PaymentIntent を作成します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d payment_method={{PAYMENT_METHOD_ID}} ``` Stripe.js SDK を使用し、次のように [confirmPayPalPayment](https://docs.stripe.com/js/payment_intents/confirm_paypal_payment) 関数を呼び出して、作成した PaymentIntent を実行します。 ```javascript // Confirms the on-session payment const {error} = await stripe.confirmPayPalPayment( '{{PAYMENT_INTENT_CLIENT_SECRET}}', {payment_method: '{{PAYMENT_METHOD_ID}}'} // Note: return_url is not required here because the PayPal payment method was // previously set up using either a SetupIntent or a PaymentIntent with setup_future_usage ); if (error) { // Inform the customer that there was an error. } ``` > **注意**:`confirmPayPalPayment` には、`return_url` パラメーターが条件付きで必要です。 > > - 以前に SetupIntent または `setup_future_usage` を指定して設定された PaymentIntent で PayPal 決済手段を使用する場合、これは *不要* です。 - オンセッションで新しい PayPal 決済手段を作成する場合など、その他すべてのケースで *必須* です。 ## ユーザー主導の支払い方法のキャンセル [サーバー側] 顧客は、PayPal アカウントを通じてサブスクリプション(請求契約)をキャンセルできます。その際、Stripeは[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) Webhook を発行します。保存された支払い方法を使用する以降のすべての PaymentIntent は、有効な同意書を持つ支払い方法に変更するまで失敗します。サブスクリプションの支払いが失敗すると、ステータスは[自動回収設定](https://docs.stripe.com/invoicing/automatic-collection.md) で設定したサブスクリプションのステータスに変更されます。顧客に失敗を通知し、[別の支払い方法で支払う](https://docs.stripe.com/billing/subscriptions/overview.md#requires-payment-method)。 ## Optional: PayPal 支払いを事前設定し、支払いをキャプチャする [サーバー側] [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成する際は、将来の利用に備えて PayPal 決済手段をセットアップすることも、請求を同時に行うこともできます。 [setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage) を `off_session` に設定し、将来使用するための支払い方法を事前設定する意思を示します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d "payment_method_types[]=paypal" \ -d setup_future_usage=off_session ``` ## Optional: PayPal リダイレクトを手動で処理する [サーバー側] クライアント側で `confirmPayPalSetup` を使用して PayPal のリダイレクトおよび請求のオーソリを処理するには、Stripe.js を使用することをお勧めします。Stripe.js を使用すると、組み込みを他の支払い方法に容易に拡張できるようになります。ただし、以下のステップを使用して、お客様のサーバで顧客を手動でリダイレクトすることもできます。 `confirm: true` を設定し、[mandate_data](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-mandate_data) パラメーターで同意書に関するデータを提供することにより、作成時に SetupIntent を*確定* (Confirming an intent indicates that the customer intends to use the current or provided payment method. Upon confirmation, the intent attempts to initiate the portions of the flow that have real-world side effects)できます。 ユーザーが PayPal のウェブサイトやモバイルアプリケーションで設定を完了した後に、どこにリダイレクトするかを指定するため、SetupIntent を確定する際に `return_url` を指定する必要があります。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d "payment_method_types[]=paypal" \ -d "payment_method_data[type]=paypal" \ -d usage=off_session \ -d "mandate_data[customer_acceptance][type]=online" \ -d "mandate_data[customer_acceptance][online][ip_address]={{IP_ADDRESS}}" \ -d "mandate_data[customer_acceptance][online][user_agent]={{USER_AGENT}}" \ -d confirm=true \ --data-urlencode "return_url=https://example.com/setup/complete" ``` SetupIntent のステータスが `requires_action` であることと、`next_action` のタイプが `redirect_to_url` であることを確認します。 ```json { "id": "seti_1IQ9hjJJahOk1vSNevPWnhEN", "object": "setup_intent","status": "requires_action", "next_action": { "type": "redirect_to_url", "redirect_to_url": { "url": "https://hooks.stripe.com/...", "return_url": "https://example.com/setup/complete" } }, "application": null, "cancellation_reason": null, "client_secret": "seti_1IQ9hjJJahOk1vSNevPWnhEN_secret_J2EAlI0GQbQKV9tg7ITRcUWRBiAwvUV", "created": 1614597263, "customer": null, "description": null, "last_setup_error": null, "latest_attempt": "setatt_1IQ9hkJJahOk1vSN0rsCpnLI", "livemode": false, "mandate": null, "metadata": {}, "next_action": null, "on_behalf_of": null, "payment_method": "pm_1IQ9hjJJahOk1vSNDc5lQWia", "payment_method_options": {}, "payment_method_types": ["paypal"], "single_use_mandate": null, "usage": "off_session" } ``` `next_action.redirect_to_url.url` プロパティで指定した URL に顧客をリダイレクトします。ここに示すコード例はおおまかなものであり、リダイレクト方法は、ご使用のウェブフレームワークによって異なる場合があります。 #### Ruby ```ruby if setup_intent.status == 'requires_action' && setup_intent.next_action.type == 'redirect_to_url' url = setup_intent.next_action.redirect_to_url.url redirect(url) end ``` 顧客がオーソリプロセスを完了すると、ステップ 1. で設定した `return_url` に送られます。URL には `setup_intent` と`setup_intent_client_secret` のクエリパラメーターが含まれており、上記のように独自のクエリパラメーターを渡すこともできます。 ## Optional: オンセッションの支払いに対するリスクライブラリの実装を手動で処理する [サーバー側] 保存された PayPal 支払い方法でオンセッションの支払いを処理するには、Stripe.js を使用することをお勧めします。構築済みの [Fraudnet](https://developer.paypal.com/limited-release/fraudnet/) 実装が付属しているためです。ただし、次の手順に従って、お客様のサーバーで PayPal の PaymentIntent を手動で確定することもできます。 PayPal のリスクライブラリ (ウェブ用は [Fraudnet](https://developer.paypal.com/limited-release/fraudnet/)、モバイル用は [Magnes](https://developer.paypal.com/limited-release/magnes/)) を実装して、買い手が支払いセッションを実施しているときに PayPal がリスクデータを収集できるようにします。これにより、不正が減少し、オンセッションの支払いのコンバージョン率が向上します。Stripe に対する API コールを実行するときは、ライブラリの初期化に使用するクライアントメタデータ ID (リスク相関 ID とも呼ばれます) が必要です。 ライブラリが読み込まれたら、次のように支払いのクライアントメタデータ ID、金額、通貨を指定して PaymentIntent を作成できます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d "payment_method_options[paypal][risk_correlation_id]={{RISK_CORRELATION_ID}}" \ -d confirm=true \ -d payment_method={{PAYMENT_METHOD_ID}} ``` オンセッションの支払いの確定時に [risk_correlation_id](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-payment_method_options-paypal-risk_correlation_id) パラメーターを渡さないと、`paypal_risk_correlation_id_missing` メッセージコードが返されます。 ## Optional: 保存された PayPal アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した PayPal アカウントを削除できます。PayPal の決済手段の関連付けを解除すると、[同意書](https://docs.stripe.com/api/mandates.md)が取り消され、さらに関連付けられた PayPal 請求契約をキャンセルするための PayPal API の呼び出しも行われます。 ```curl curl -X POST https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/detach \ -u "<>:" ``` # iOS > This is a iOS for when payment-ui is mobile and platform is ios. View the full page at https://docs.stripe.com/payments/paypal/set-up-future-payments?payment-ui=mobile&platform=ios. [Setup Intents](https://docs.stripe.com/api/setup_intents.md) を使用して事前に PayPal 支払い方法の詳細を収集し、後から最終的な金額や支払い日を決定します。この機能は、次の場合に使用します。 ## Customer を作成または取得する [サーバー側] 以降の支払いに PayPal の支払い方法を再利用するには、*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 -X POST https://api.stripe.com/v1/customers \ -u "<>:" ``` ## SetupIntent を作成する [サーバー側] [SetupIntent (支払い方法設定インテント)](https://docs.stripe.com/api/setup_intents.md) は、将来の支払いに備えて顧客の支払い方法を設定するという意図を示し、そのステップを追跡するオブジェクトです。 サーバーで [SetupIntent](https://docs.stripe.com/api/setup_intents.md) を作成し、[payment_method_types](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-payment_method_types) を `paypal` に設定して、顧客が設定した [Account](https://docs.stripe.com/api/v2/core/accounts/object.md#v2_account_object-id) または [Customer](https://docs.stripe.com/api/customers/object.md#customer_object-id) の ID を指定します。 #### Accounts v2 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer_account={{CUSTOMERACCOUNT_ID}}" \ -d "payment_method_types[]=paypal" \ -d "payment_method_data[type]=paypal" ``` #### Customers v1 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d "payment_method_types[]=paypal" \ -d "payment_method_data[type]=paypal" ``` SetupIntent オブジェクトには、[client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) が含まれています。これは、買い手を PayPal にリダイレクトして同意書の承認を得る際に、クライアント側で Stripe に渡す必要がある一意のキーです。 ## 支払い方法の詳細を収集する [クライアント側] #### Swift ```swift // PayPal doesn't require additional parameters so we only need to pass the initialized // STPPaymentMethodPayPalParams instance to STPPaymentMethodParams let payPal = STPPaymentMethodPayPalParams() let paymentMethodParams = STPPaymentMethodParams(payPal: payPal, billingDetails: nil, metadata: nil) ``` ## Stripe に支払いを送信する [クライアント側] 作成した SetupIntent から client secret を取得し、[STPPaymentHandler confirmSetupIntent](https://stripe.dev/stripe-ios/stripe-payments/Classes/STPPaymentHandler.html#/c:objc\(cs\)STPPaymentHandler\(im\)confirmSetupIntent:withAuthenticationContext:completion:) を呼び出します。これにより WebView が表示され、顧客はそこから PayPal で支払いを完了できます。その後、支払い結果を示す完了ブロックが呼び出されます。 #### Swift ```swift let setupIntentParams = STPSetupIntentConfirmParams(clientSecret: setupIntentClientSecret) setupIntentParams.paymentMethodParams = paymentMethodParams setupIntentParams.returnURL = "payments-example://stripe-redirect" STPPaymentHandler.shared().confirmSetupIntent(withParams: setupIntentParams, authenticationContext: self) { (handlerStatus, setupIntent, error) in switch handlerStatus { case .succeeded: // Setup succeeded case .canceled: // Setup was canceled case .failed: // Setup failed @unknown default: fatalError() } } ``` ## Webhook を監視する [サーバー側] 顧客が請求契約を正常に承認したことを確認するには、顧客が支払いステータスページに戻るのを期待するのではなく、[Webhook](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) などの方法を使用します。利用者が請求契約を正常に承認すると、SetupIntent は [setup_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.succeeded) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信します。請求契約の承認が正常に行われなかった場合、SetupIntent は [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) Webhook イベントを送信し、`requires_payment_method` ステータスに戻ります。利用者が PayPal アカウントから請求契約を取り消すと、[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) が送信されます。 ## 保存された PayPal 支払い方法でオフセッションの支払いに請求する [サーバー側] オフセッションで顧客に請求する準備ができたら、*Customer* (Customer objects represent customers of your business. They let you reuse payment methods and give you the ability to track multiple payments) ID と *PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) ID を使用して、[PaymentIntent (支払いインテント)](https://docs.stripe.com/api/payment_intents.md) を作成します。 請求する `paypal` の手段を見つけるには、Customer に関連付けられた PaymentMethod を[リスト](https://docs.stripe.com/api/payment_methods/list.md)表示します。 ```curl curl -G https://api.stripe.com/v1/payment_methods \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d type=paypal ``` Customer ID と PaymentMethod ID を取得したら、支払いの金額と通貨を使用して PaymentIntent を作成します。その他のいくつかのパラメーターを設定して、*オフセッション支払い* (A payment is described as off-session if it occurs without the direct involvement of the customer, using previously-collected payment information)を行います。 - [off_session](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-off_session) を `true` に設定して、支払いの実行時に顧客が決済フローに存在しないことを示します。これにより、認証が必要な場合は PaymentIntent からエラーが返されます。 - PaymentIntent の [confirm](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-confirm) プロパティの値を `true` に設定します。これにより、PaymentIntent が作成されると直ちに確定されます。 - [payment_method](https://docs.stripe.com/api.md#create_payment_intent-payment_method) を PaymentMethod の ID に設定し、[customer](https://docs.stripe.com/api.md#create_payment_intent-customer) を Customer の ID に設定します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ -d confirm=true ``` ## ユーザー主導の支払い方法のキャンセル [サーバー側] 顧客は、PayPal アカウントを通じてサブスクリプション(請求契約)をキャンセルできます。その際、Stripeは[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) Webhook を発行します。保存された支払い方法を使用する以降のすべての PaymentIntent は、有効な同意書を持つ支払い方法に変更するまで失敗します。サブスクリプションの支払いが失敗すると、ステータスは[自動回収設定](https://docs.stripe.com/invoicing/automatic-collection.md) で設定したサブスクリプションのステータスに変更されます。顧客に失敗を通知し、[別の支払い方法で支払う](https://docs.stripe.com/billing/subscriptions/overview.md#requires-payment-method)。 ## Optional: PayPal 支払いを事前設定し、支払いをキャプチャする [サーバー側] [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成する際は、将来の利用に備えて PayPal 決済手段をセットアップすることも、請求を同時に行うこともできます。 [setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage) を `off_session` に設定し、将来使用するための支払い方法を事前設定する意思を示します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d "payment_method_types[]=paypal" \ -d setup_future_usage=off_session ``` ## Optional: オンセッションの支払いに対するリスクライブラリの実装を手動で処理する [サーバー側] 保存された PayPal 支払い方法でオンセッションの支払いを処理するには、Stripe.js を使用することをお勧めします。構築済みの [Fraudnet](https://developer.paypal.com/limited-release/fraudnet/) 実装が付属しているためです。ただし、次の手順に従って、お客様のサーバーで PayPal の PaymentIntent を手動で確定することもできます。 PayPal のリスクライブラリ (ウェブ用は [Fraudnet](https://developer.paypal.com/limited-release/fraudnet/)、モバイル用は [Magnes](https://developer.paypal.com/limited-release/magnes/)) を実装して、買い手が支払いセッションを実施しているときに PayPal がリスクデータを収集できるようにします。これにより、不正が減少し、オンセッションの支払いのコンバージョン率が向上します。Stripe に対する API コールを実行するときは、ライブラリの初期化に使用するクライアントメタデータ ID (リスク相関 ID とも呼ばれます) が必要です。 ライブラリが読み込まれたら、次のように支払いのクライアントメタデータ ID、金額、通貨を指定して PaymentIntent を作成できます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d "payment_method_options[paypal][risk_correlation_id]={{RISK_CORRELATION_ID}}" \ -d confirm=true \ -d payment_method={{PAYMENT_METHOD_ID}} ``` オンセッションの支払いの確定時に [risk_correlation_id](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-payment_method_options-paypal-risk_correlation_id) パラメーターを渡さないと、`paypal_risk_correlation_id_missing` メッセージコードが返されます。 ## Optional: 保存された PayPal アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した PayPal アカウントを削除できます。PayPal の決済手段の関連付けを解除すると、[同意書](https://docs.stripe.com/api/mandates.md)が取り消され、さらに関連付けられた PayPal 請求契約をキャンセルするための PayPal API の呼び出しも行われます。 ```curl curl -X POST https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/detach \ -u "<>:" ``` # Android > This is a Android for when payment-ui is mobile and platform is android. View the full page at https://docs.stripe.com/payments/paypal/set-up-future-payments?payment-ui=mobile&platform=android. [Setup Intents](https://docs.stripe.com/api/setup_intents.md) を使用して事前に PayPal 支払い方法の詳細を収集し、後から最終的な金額や支払い日を決定します。この機能は、次の場合に使用します。 - 支払い方法をウォレットに保存して、以降の購入を効率化する - サービスの提供後に追加料金を回収する - [サブスクリプションの無料トライアルを開始する](https://docs.stripe.com/billing/subscriptions/trials.md) ## Stripe を設定する [サーバ側] [クライアント側] まず、Stripe アカウントが必要です。[今すぐ登録](https://dashboard.stripe.com/register)してください。 ### サーバ側 この組み込みには、サーバ上に Stripe API と通信するエンドポイントが必要です。サーバから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。 #### Ruby ```bash # Available as a gem sudo gem install stripe ``` ```ruby # If you use bundler, you can add this line to your Gemfile gem 'stripe' ``` ### クライアント側 [Stripe Android SDK](https://github.com/stripe/stripe-android) はオープンソースであり、[詳細なドキュメントが提供されています](https://stripe.dev/stripe-android/)。 SDK をインストールするには、[app/build.gradle](https://developer.android.com/studio/build/dependencies) ファイルの `dependencies` ブロックに `stripe-android` を追加します。 #### Kotlin ```kotlin plugins { id("com.android.application") } android { ... } dependencies { // ... // Stripe Android SDK implementation("com.stripe:stripe-android:23.7.0") // Include the financial connections SDK to support US bank account as a payment method implementation("com.stripe:financial-connections:23.7.0") } ``` > SDK の最新リリースおよび過去バージョンの詳細については、GitHub の [Releases](https://github.com/stripe/stripe-android/releases) ページをご覧ください。新しいリリースの公開時に通知を受け取るには、[リポジトリのリリースを確認](https://docs.github.com/en/github/managing-subscriptions-and-notifications-on-github/configuring-notifications#configuring-your-watch-settings-for-an-individual-repository)してください。 Stripe の[公開可能キー](https://dashboard.stripe.com/apikeys)を使用して SDK を設定し、 `Application` サブクラスなどで、Stripe API へのリクエストを実行できるようにします。 #### Kotlin ```kotlin import com.stripe.android.PaymentConfiguration class MyApp : Application() { override fun onCreate() { super.onCreate() PaymentConfiguration.init( applicationContext, "<>" ) } } ``` > テストおよび開発時には[テストキー](https://docs.stripe.com/keys.md#obtain-api-keys)を使用し、アプリの公開時には[本番環境](https://docs.stripe.com/keys.md#test-live-modes)キーを使用します。 Stripe サンプルでは、サーバへの HTTP リクエストの作成に、[OkHttp](https://github.com/square/okhttp) および [GSON](https://github.com/google/gson) も使用します。 ## Customer を作成または取得する [サーバー側] 以降の支払いに PayPal の支払い方法を再利用するには、*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 -X POST https://api.stripe.com/v1/customers \ -u "<>:" ``` ## SetupIntent を作成する [サーバー側] [SetupIntent (支払い方法設定インテント)](https://docs.stripe.com/api/setup_intents.md) は、将来の支払いに備えて顧客の支払い方法を設定するという意図を示し、そのステップを追跡するオブジェクトです。 サーバーで [SetupIntent](https://docs.stripe.com/api/setup_intents.md) を作成し、[payment_method_types](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-payment_method_types) を `paypal` に設定して、顧客が設定した [Account](https://docs.stripe.com/api/v2/core/accounts/object.md#v2_account_object-id) または [Customer](https://docs.stripe.com/api/customers/object.md#customer_object-id) の ID を指定します。 #### Accounts v2 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer_account={{CUSTOMERACCOUNT_ID}}" \ -d "payment_method_types[]=paypal" \ -d "payment_method_data[type]=paypal" ``` #### Customers v1 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d "payment_method_types[]=paypal" \ -d "payment_method_data[type]=paypal" ``` SetupIntent オブジェクトには、[client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) が含まれています。これは、買い手を PayPal にリダイレクトして同意書の承認を得る際に、クライアント側で Stripe に渡す必要がある一意のキーです。 ## Stripe に支払い方法の詳細を送信する [クライアント側] 作成した SetupIntent から client secret を取得し、[PaymentLauncher confirm](https://stripe.dev/stripe-android/payments-core/com.stripe.android.payments.paymentlauncher/-payment-launcher/confirm.html) を呼び出します。これにより、WebView が表示され、顧客はここで PayPal の設定を完了できます。完了すると、支払い結果とともに、指定された `PaymentResultCallback` が呼び出されます。 #### Kotlin ```kotlin class PayPalSetupActivity : AppCompatActivity() { // ... private val paymentLauncher: PaymentLauncher by lazy { val paymentConfiguration = PaymentConfiguration.getInstance(this) PaymentLauncher.create( activity = this, publishableKey = paymentConfiguration.publishableKey, stripeAccountId = paymentConfiguration.stripeAccountId, callback = ::onPaymentResult, ) } override fun onCreate(savedInstanceState: Bundle?) { // … startCheckout() } private fun startCheckout() { // Create a SetupIntent on your backend and return the client_secret here val setupIntentClientSecret = // … val payPalPayParams = PaymentMethodCreateParams.createPayPal() val confirmParams = ConfirmSetupIntentParams.create( paymentMethodCreateParams = payPalPayParams, clientSecret = setupIntentClientSecret, // Add a mandate ID or MandateDataParams… ) paymentLauncher.confirm(confirmParams) } private fun onPaymentResult(paymentResult: PaymentResult) { // Handle the setup result… } } ``` ## Webhook を監視する [サーバー側] 顧客が請求契約を正常に承認したことを確認するには、顧客が支払いステータスページに戻るのを期待するのではなく、[Webhook](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) などの方法を使用します。利用者が請求契約を正常に承認すると、SetupIntent は [setup_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.succeeded) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信します。請求契約の承認が正常に行われなかった場合、SetupIntent は [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) Webhook イベントを送信し、`requires_payment_method` ステータスに戻ります。利用者が PayPal アカウントから請求契約を取り消すと、[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) が送信されます。 ## 保存された PayPal 支払い方法でオフセッションの支払いに請求する [サーバー側] オフセッションで顧客に請求する準備ができたら、*Customer* (Customer objects represent customers of your business. They let you reuse payment methods and give you the ability to track multiple payments) ID と *PaymentMethod* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs) ID を使用して、[PaymentIntent (支払いインテント)](https://docs.stripe.com/api/payment_intents.md) を作成します。 請求する `paypal` の手段を見つけるには、Customer に関連付けられた PaymentMethod を[リスト](https://docs.stripe.com/api/payment_methods/list.md)表示します。 ```curl curl -G https://api.stripe.com/v1/payment_methods \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d type=paypal ``` Customer ID と PaymentMethod ID を取得したら、支払いの金額と通貨を使用して PaymentIntent を作成します。その他のいくつかのパラメーターを設定して、*オフセッション支払い* (A payment is described as off-session if it occurs without the direct involvement of the customer, using previously-collected payment information)を行います。 - [off_session](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-off_session) を `true` に設定して、支払いの実行時に顧客が決済フローに存在しないことを示します。これにより、認証が必要な場合は PaymentIntent からエラーが返されます。 - PaymentIntent の [confirm](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-confirm) プロパティの値を `true` に設定します。これにより、PaymentIntent が作成されると直ちに確定されます。 - [payment_method](https://docs.stripe.com/api.md#create_payment_intent-payment_method) を PaymentMethod の ID に設定し、[customer](https://docs.stripe.com/api.md#create_payment_intent-customer) を Customer の ID に設定します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ -d confirm=true ``` ## ユーザー主導の支払い方法のキャンセル [サーバー側] 顧客は、PayPal アカウントを通じてサブスクリプション(請求契約)をキャンセルできます。その際、Stripeは[mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated) Webhook を発行します。保存された支払い方法を使用する以降のすべての PaymentIntent は、有効な同意書を持つ支払い方法に変更するまで失敗します。サブスクリプションの支払いが失敗すると、ステータスは[自動回収設定](https://docs.stripe.com/invoicing/automatic-collection.md) で設定したサブスクリプションのステータスに変更されます。顧客に失敗を通知し、[別の支払い方法で支払う](https://docs.stripe.com/billing/subscriptions/overview.md#requires-payment-method)。 ## Optional: PayPal 支払いを事前設定し、支払いをキャプチャする [サーバー側] [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成する際は、将来の利用に備えて PayPal 決済手段をセットアップすることも、請求を同時に行うこともできます。 [setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage) を `off_session` に設定し、将来使用するための支払い方法を事前設定する意思を示します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d "payment_method_types[]=paypal" \ -d setup_future_usage=off_session ``` ## Optional: オンセッションの支払いに対するリスクライブラリの実装を手動で処理する [サーバー側] 保存された PayPal 支払い方法でオンセッションの支払いを処理するには、Stripe.js を使用することをお勧めします。構築済みの [Fraudnet](https://developer.paypal.com/limited-release/fraudnet/) 実装が付属しているためです。ただし、次の手順に従って、お客様のサーバーで PayPal の PaymentIntent を手動で確定することもできます。 PayPal のリスクライブラリ (ウェブ用は [Fraudnet](https://developer.paypal.com/limited-release/fraudnet/)、モバイル用は [Magnes](https://developer.paypal.com/limited-release/magnes/)) を実装して、買い手が支払いセッションを実施しているときに PayPal がリスクデータを収集できるようにします。これにより、不正が減少し、オンセッションの支払いのコンバージョン率が向上します。Stripe に対する API コールを実行するときは、ライブラリの初期化に使用するクライアントメタデータ ID (リスク相関 ID とも呼ばれます) が必要です。 ライブラリが読み込まれたら、次のように支払いのクライアントメタデータ ID、金額、通貨を指定して PaymentIntent を作成できます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=eur \ -d customer={{CUSTOMER_ID}} \ -d "payment_method_types[]=paypal" \ -d "payment_method_options[paypal][risk_correlation_id]={{RISK_CORRELATION_ID}}" \ -d confirm=true \ -d payment_method={{PAYMENT_METHOD_ID}} ``` オンセッションの支払いの確定時に [risk_correlation_id](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-payment_method_options-paypal-risk_correlation_id) パラメーターを渡さないと、`paypal_risk_correlation_id_missing` メッセージコードが返されます。 ## Optional: 保存された PayPal アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した PayPal アカウントを削除できます。PayPal の決済手段の関連付けを解除すると、[同意書](https://docs.stripe.com/api/mandates.md)が取り消され、さらに関連付けられた PayPal 請求契約をキャンセルするための PayPal API の呼び出しも行われます。 ```curl curl -X POST https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/detach \ -u "<>:" ```