# 将来の Klarna の支払いを設定する Klarna の詳細を保存し、後で顧客に請求する方法は以下のとおりです。 Klarna を顧客の*支払い方法* (PaymentMethods represent your customer's payment instruments, used with the Payment Intents or Setup Intents APIs)として保存し、将来の支払いに対して請求して以下をサポートできます。 - [無料トライアル](https://docs.stripe.com/billing/subscriptions/trials.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) の自動決済。 - 非サブスクリプション製品も含む注文のサブスクリプション自動決済。 - Klarna をウォレットに保存すると、顧客の再認証を必要とせずに、将来の*オンデマンド* (When a customer stores their payment method with a business, they can make on-demand future purchases without re-authenticating, such as ordering a ride in their ride-share app)購入を効率化できます。 このガイドでは、即座にまたは後で請求できる支払い方法として Klarna を保存する方法を説明します。このガイドは、[Stripe Billing](https://docs.stripe.com/billing.md) を使用する導入向けではありません。Stripe Billing を使用する場合は、[Klarna for Subscriptions](https://docs.stripe.com/billing/subscriptions/klarna.md) をご覧ください。 > #### 利用可能なKlarna決済オプションはユースケースと買い手の国によって異なります > > 導入を開始する前に、顧客が利用できる[決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) を確認してください。 [Stripe Checkout](https://docs.stripe.com/payments/checkout.md)を使用して、Klarna を支払い方法として保存することをお勧めします。 # Checkout > This is a Checkout for when payment-ui is checkout. View the full page at https://docs.stripe.com/payments/klarna/set-up-future-payments?payment-ui=checkout. このガイドの第1部では、[Stripe Checkout](https://docs.stripe.com/payments/checkout.md)を使用してKlarnaを支払い方法として保存する方法を説明します。最後のステップでは、サブスクリプションまたは *オンデマンド* (When a customer stores their payment method with a business, they can make on-demand future purchases without re-authenticating, such as ordering a ride in their ride-share app) 決済のために保存された支払い方法を請求する方法を説明します。 ## 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 を作成または取得する [サーバー側] 今後の支払いに Klarna 支払い方法を再利用するには、これを *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 セッションを作成する [クライアント側] [サーバー側] Klarna 支払いを受け付ける前に、顧客は、お客様が Stripe Checkout を通じて今後の支払いに Klarna アカウントを使用することを承認する必要があります。 サーバー側のエンドポイントを呼び出す**オーソリ**ボタンをウェブサイトに追加して [Checkout Session](https://docs.stripe.com/api/checkout/sessions.md) を作成します。 ```html Authorize
``` #### サブスクリプション Checkout を使用してサブスクリプションを設定する際は、サブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/checkout/sessions \ -u "<>:" \ -d mode=setup \ -d customer={{CUSTOMER_ID}} \ -d currency=usd \ --data-urlencode "success_url=https://example.com/success?session_id={CHECKOUT_SESSION_ID}" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][name]=Customer-facing name for subscription" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=10000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` #### オンデマンド支払い ```curl curl https://api.stripe.com/v1/checkout/sessions \ -u "<>:" \ -d mode=setup \ -d customer={{CUSTOMER_ID}} \ -d currency=usd \ --data-urlencode "success_url=https://example.com/success?session_id={CHECKOUT_SESSION_ID}" ``` Checkout セッションを作成したら、レスポンスで返された [URL](https://docs.stripe.com/api/checkout/sessions/object.md#checkout_session_object-url) に顧客をリダイレクトします。 ## Checkout からのリダイレクトを処理する [クライアント側] [サーバー側] 顧客が決済手段の詳細を入力すると、`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) は、顧客のKlarna 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) は、今後の支払いに利用できるように顧客のKlarna 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) は、今後の支払いに利用できるように顧客のKlarna account情報を設定するために使用されるオブジェクトです。返されるオブジェクトには `payment_method` ID が含まれます。 ## 設定後のイベントを処理する [サーバー側] [Webhook](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) などの方法で、顧客が請求契約を承認したことを確認します。顧客が支払いステータスページに戻ることに依存しないようにしてください。 顧客が請求契約の承認を正常に完了すると、Stripe は [payment_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.succeeded) または [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) イベントを送信し、インテントのステータスは `succeeded` に移行します。生成された [payment_method](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-payment_method) ID を保存し、保存した PaymentMethod を使用して後で支払いを行います。 顧客が請求契約の承認を正常に行えなかった場合、Stripe は [payment_intent.payment_failed](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.payment_failed) または [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信し、インテントのステータスは `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) を作成します。その他いくつかのパラメーターを設定して、*オフセッションの支払い* (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 に設定します。 - [return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url)を指定して、顧客が Klarna の Web サイトから戻った後に Stripe がリダイレクトする必要がある場所を指定します。 更新のたびにサブスクリプションの詳細と項目を送信します。決済手段を設定したときと同じ `reference` をサブスクリプションに使用します。決済手段の保存後にサブスクリプションの詳細が変更された場合は、同じ `reference` を指定して新しい情報を送信します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` #### オンデマンド支払い 顧客に支払う準備ができたら、*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) を作成します。*オフセッションの決済* (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)を`false`に設定して、この支払い試行中に顧客が決済フローにいることを示します。認証が必要なエラーなどが発生した場合、顧客は Klarna のページにリダイレクトされ、認証するか問題を解決します。 - 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 に設定します。 以下の例では、Klarna のリスク評価とオーソリ率の向上に役立つ [payment_method_options](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options) を使用したオプション詳細が含まれています。これには、今後の注文予定額や決済の頻度など、保存された Klarna 支払い方法を将来どのように使用すべきかの詳細が含まれます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=false \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=1099" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]={{SUBSCRIPTION_ID}}" \ -d "payment_method_options[klarna][on_demand][average_amount]=1000" \ -d "payment_method_options[klarna][on_demand][minimum_amount]=100" \ -d "payment_method_options[klarna][on_demand][maximum_amount]=10000" \ -d "payment_method_options[klarna][on_demand][interval]=year" \ -d "payment_method_options[klarna][on_demand][interval_count]=1" ``` ## 再利用可能な決済方法の取り消しを処理する [サーバー側] 再利用可能な決済方法を取り消すには、次の 2 つの方法があります。 - 顧客は、Klarna モバイルアプリケーションで再利用可能な決済方法を無効にすることができます。この場合、Stripe は [mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated)イベントを送信します。これを処理するには、[webhook](https://docs.stripe.com/webhooks.md)イベントをサブスクライブし、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化します。 - サポートされている場合、顧客は、UIで再利用可能な決済方法を無効にすることもできます。このシナリオでは、サーバーは [PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化を処理できます。 どちらの場合も、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出した後、Stripe から [payment_method.detached](https://docs.stripe.com/api/events/types.md#event_types-payment_method.detached)イベントが送信されます。 ## 導入をテストする Checkout の実装状態をテストする際は、決済手段として Klarna を選択し、**保存** ボタンをクリックします。テスト環境では、Klarna のリダイレクト内でさまざまな結果をシミュレートできます。 > Klarna はセッションの追跡に クッキーを使用します。さまざまな顧客の所在地をテストするには、前のセッションで Klarna サンドボックスからログアウトし、関連するトリガーを使用します。 下記では、現在対応している顧客の国のテストデータを特別に選択しています。サンドボックスでは、Klarna は指定されたメールアドレスに基づいて取引を承認または拒否します。 #### オーストラリア | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 03-05-1994 | | 名 | テスト | John | | 姓 | Person-au | snow | | 町名・番地 | Wharf St | Silverwater Rd | | 番地等 | 4 | 1-5 | | 郵便番号 | 4877 | 2128 | | 市区町村 | Port Douglas | Silverwater | | 地域 | QLD | NSW | | 電話番号 | +61473752244 | +61473763254 | | メールアドレス | customer@email.au | customer+denied@email.au | #### オーストリア | | 承認済み | 拒否 | | ------- | ------------------ | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-at | Person-at | | メールアドレス | customer@email.at | customer+denied@email.at | | 町名・番地 | Mariahilfer Straße | Mariahilfer Straße | | 番地 | 47 | 47 | | 市区町村 | Wien | Wien | | 郵便番号 | 1060 | 1060 | | 電話番号 | +4306762600456 | +4306762600745 | #### ベルギー | | 承認済み | 拒否済み | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-be | Person-be | | メールアドレス | customer@email.be | customer+denied@email.be | | 町名・番地 | Grote Markt | Grote Markt | | 番地 | 1 | 1 | | 市 | Brussel | Brussel | | 郵便番号 | 1000 | 1000 | | 電話番号 | +32485121291 | +32485212123 | #### カナダ | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-ca | Person-ca | | 町名・番地 | 2693 Byron Rd | 2693 Byron Rd | | 郵便番号 | V7H 1L9 | V7H 1L9 | | 市区町村 | North Vancouver | North Vancouver | | 地域 | BC | BC | | 電話番号 | +15197438620 | +15197308624 | | メールアドレス | customer@email.ca | customer+denied@email.ca | #### チェコ | | 承認 | 拒否 | | ------- | ------------------ | ------------------------ | | 生年月日 | 01-01-1970 | 27-06-1992 | | 名 | テスト | テスト | | 姓 | Person-cz | Person-cz | | メールアドレス | customer@email.cz | customer+denied@email.cz | | 町名・番地 | Zazvorkova 1480/11 | Zázvorkova 1480/11 | | 郵便番号 | 155 00 | 155 00 | | 市区町村 | Praha | PRAHA 13 | | 電話番号 | +420771613715 | +420771623691 | #### デンマーク | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1980 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-dk | Person-dk | | メールアドレス | customer@email.dk | customer+denied@email.dk | | 町名・番地 | Dantes Plads | Nygårdsvej | | 番地 | 7 | 65 | | 市区町村 | København Ø | København Ø | | 郵便番号 | 1556 | 2100 | | 電話番号 | +4542555628 | +4552555348 | #### フィンランド | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1999 | 01-01-1999 | | 名 | テスト | Person FI | | 姓 | Person-fi | テスト | | メールアドレス | customer@email.fi | customer+denied@email.fi | | 町名・番地 | Mannerheimintie | Mannerheimintie | | 番地 | 34 | 34 | | 市区町村 | Helsinki | Helsinki | | 郵便番号 | 00100 | 00100 | | 電話番号 | +358401234567 | +358401234568 | #### フランス | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 10-07-1990 | 10-07-1990 | | 出生地 | パリ | パリ | | 名 | テスト | テスト | | 姓 | Person-fr | Person-fr | | メールアドレス | customer@email.fr | customer+denied@email.fr | | 町名・番地 | rue La Fayette | rue La Fayette | | 番地 | 33 | 33 | | 市区町村 | パリ | パリ | | 郵便番号 | 75009 | 75009 | | 電話番号 | +33689854321 | +33687984322 | #### ドイツ | | 承認済み | 拒否済み | | ------- | --------------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | 模擬 | テスト | | 姓 | 模擬 | Person-de | | メールアドレス | customer@email.de | customer+denied@email.de | | 町名・番地 | Neue Schönhauser Str. | Neue Schönhauser Str. | | 番地 | 2 | 2 | | 市 | Berlin | Berlin | | 郵便番号 | 10178 | 10178 | | 電話番号 | +49017614284340 | +49017610927312 | #### ギリシャ | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 納税者番号 | 090000045 | 090000045 | | 生年月日 | 01-01-1960 | 11-11-1970 | | 名 | テスト | テスト | | 姓 | Person-gr | Test-gr | | メールアドレス | customer@email.gr | customer+denied@email.gr | | 町名・番地 | Kephisias | Baralo | | 番地 | 37 | 56 | | 郵便番号 | 151 23 | 123 67 | | 市区町村 | Athina | Athina | | 電話番号 | +306945553624 | +306945553625 | #### アイルランド | | 承認済み | 拒否 | | -------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-ie | Person-ie | | メール | customer@email.ie | customer+denied@email.ie | | 町名・番地 | King Street South | King Street South | | 番地 | 30 | 30 | | 市区町村 | ダブリン | ダブリン | | EIR Code | D02 C838 | D02 C838 | | 電話番号 | +353855351400 | +353855351401 | #### イタリア | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1980 | 01-01-1980 | | 名 | テスト | テスト | | 姓 | Person-it | Person-it | | メールアドレス | customer@email.it | customer+denied@email.it | | 納税番号 | RSSBNC80A41H501B | RSSBNC80A41H501B | | 町名・番地 | Via Enrico Fermi | Via Enrico Fermi | | 番地 | 150 | 150 | | 市区町村 | Roma | Roma | | 郵便番号 | 00146 | 00146 | | 電話番号 | +393339741231 | +393312232389 | #### オランダ | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-nl | Person-nl | | メールアドレス | customer@email.nl | customer+denied@email.nl | | 町名・番地 | Osdorpplein | Osdorpplein | | 番地 | 137 | 137 | | 市区町村 | Amsterdam | Amsterdam | | 郵便番号 | 1068 SR | 1068 SR | | 電話番号 | +31689124321 | +31632167678 | #### ニュージーランド | | 承認 | 拒否 | | ------- | ------------------------ | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-nz | Person-nz | | 町名・番地 | Mount Wellington Highway | Mount Wellington Highway | | 番地等 | 286 | 286 | | 郵便番号 | 6011 | 6011 | | 市区町村 | Auckland | Wellington | | 電話番号 | +6427555290 | +642993007712 | | メールアドレス | customer@email.nz | customer+denied@email.nz | #### ノルウェー | | 承認済み | 拒否 | | ------- | ------------------- | ------------------------ | | 生年月日 | 01-08-1970 | 01-08-1970 | | 名 | Jane | テスト | | 姓 | テスト | Person-no | | メールアドレス | customer@email.no | customer+denied@email.no | | 個人番号 | NO1087000571 | NO1087000148 | | 町名・番地 | Edvard Munchs Plass | Sæffleberggate | | 番地 | 1 | 56 | | 市区町村 | Oslo | Oslo | | 郵便番号 | 0194 | 0563 | | 電話番号 | +4740123456 | +4740123457 | #### ポーランド | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 05-05-1967 | 05-05-1967 | | 名 | テスト | テスト | | 姓 | Person-pl | Person-pl | | 町名・番地 | Ul. Górczewska | Ul. Górczewska | | 番地等 | 124 | 124 | | 郵便番号 | 01-460 | 01-460 | | 市区町村 | Warszawa | Warszawa | | 電話番号 | +48795222223 | +48795223325 | | メールアドレス | customer@email.pl | customer+denied@email.pl | #### ポルトガル | | 承認 | 拒否 | | ------- | ------------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-pt | Person-pt | | 町名・番地 | Avenida Dom João II | Avenida Dom João II | | 番地等 | 40 | 40 | | 郵便番号 | 1990-094 | 1990-094 | | 市区町村 | Lisboa | Lisboa | | 電話番号 | +351935556731 | +351915593837 | | メールアドレス | customer@email.pt | customer+denied@email.pt | #### ルーマニア | | 承認されました | 拒否 | | ------------ | ----------------- | ------------------------ | | 生年月日 | 1970 年 12 月 25 日 | 1970 年 12 月 25 日 | | 名 | テスト | テスト | | 姓 | Person-ro | Person-ro | | メール | customer@email.ro | customer+denied@email.ro | | 町名・番地 | Drumul Taberei | Drumul Taberei | | 番地 | 35 | 35 | | 市区町村 | București | București | | セクター | Sectorul 6 | Sectorul 6 | | 郵便番号 | 061357 | 061357 | | 電話 | +40741209876 | +40707127444 | | 個人識別番号 (CNP) | 1701225193558 | | #### スペイン | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | DNI/NIE | 99999999R | 99999999R | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-es | Person-es | | メールアドレス | customer@email.es | customer+denied@email.es | | 町名・番地 | C. de Atocha | C. de Atocha | | 番地 | 27 | 27 | | 市区町村 | Madrid | Madrid | | 郵便番号 | 28012 | 28012 | | 電話番号 | +34672563009 | +34682425101 | #### スウェーデン | | 承認済み | 拒否 | | ------- | ----------------------- | ------------------------ | | 生年月日 | 21-03-1941 | 28-10-1941 | | 名 | Alice | テスト | | 姓 | テスト | Person-se | | メールアドレス | customer@email.se | customer+denied@email.se | | 町名・番地 | Södra Blasieholmshamnen | Karlaplan | | 番地 | 2 | 3 | | 市区町村 | Stockholm | Stockholm | | 郵便番号 | 11 148 | 11 460 | | 電話番号 | +46701740615 | +46701740620 | #### スイス | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1990 | 01-01-2000 | | 名 | 受付済み | 顧客 | | 姓 | Person-ch | Person-ch | | 町名・番地 | Augustinergasse | Bahnhofstrasse | | 番地等 | 2 | 77 | | 郵便番号 | 4051 | 8001 | | 市区町村 | Basel | Zürich | | 電話番号 | +41758680000 | +41758680001 | | メールアドレス | customer@email.ch | customer+denied@email.ch | #### イギリス | | 承認済み | 拒否 | | ------- | --------------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-uk | Person-uk | | メールアドレス | customer@email.uk | customer+denied@email.uk | | 町名・番地 | New Burlington Street | New Burlington Street | | 番地 | 10 | 10 | | アパート | Apt 214 | Apt 214 | | 郵便番号 | W1S 3BE | W1S 3BE | | 市区町村 | London | London | | 電話番号 | +447755564318 | +447355505530 | #### アメリカ | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 07-10-1970 | 07-10-1970 | | 名 | テスト | テスト | | 姓 | Person-us | Person-us | | メールアドレス | customer@email.us | customer+denied@email.us | | 町名・番地 | Amsterdam Ave | Amsterdam Ave | | 番地 | 509 | 509 | | 市区町村 | New York | New York | | 州 | New York | New York | | 郵便番号 | 10024-3941 | 10024-3941 | | 電話番号 | +13106683312 | +13106354386 | ### 2 段階認証 6 桁の数字であれば、2 段階認証コードとして有効です。`999999` を使用すると、認証は失敗します。 ### 返済方法 Klarna のフロー内では、以下のテスト値を使用し、さまざまな返済方法を試すことができます。 | タイプ | 値 | | -------- | ---------------------------------------------------------- | | 口座引き落とし | DE11520513735120710131 | | 銀行振込 | デモの銀行 | | クレジットカード | - 番号: 4111 1111 1111 1111 - CVV: 123 - 有効期限: 任意の将来日付 | | デビットカード | - カード番号:4012 8888 8888 1881 - CVV: 123 - 有効期限: 任意の将来日付 | ## Optional: 保存済みの決済手段をアップグレードする [サーバー側] 顧客がサブスクリプションをアップグレードした場合に、Klarna の決済フローを再度実行します。これにより、Klarna のサブスクリプション情報が正確になり、オーソリ率が最適化されます。 #### アップグレードと料金改訂 保存済みの決済手段に請求する場合と同じ構造の [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、`setup_future_usage` を含めて、既存の決済手段をアップグレードすることを示します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=10000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d setup_future_usage=off_session \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=10000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 生成された PaymentIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトを処理する方法](https://docs.stripe.com/payments/klarna/set-up-future-payments.md?web-or-mobile=web&payment-ui=elements#web-redirect-customer) をご覧ください。 #### アップグレードして後で決済 [SetupIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、既存の保存された決済手段を渡します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=20000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 生成された SetupIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md?web-or-mobile=web&payment-ui=elements#web-redirect-customer) 方法をご覧ください。 ## Optional: 保存された Klarna アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した Klarna アカウントを削除できます。Klarna アカウントは、顧客がサブスクリプションをキャンセルした場合など、二度と使用されないことがわかっている場合に削除します。 ```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/klarna/set-up-future-payments?payment-ui=direct-api. カスタマイズされた決済 UI を作成する場合は、[setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage)を指定して [Payment Intents API](https://docs.stripe.com/api/payment_intents.md) を使用するか、[Setup Intents API](https://docs.stripe.com/api/setup_intents.md) を使用して Klarna 決済手段の詳細を保存し、後で顧客に請求します。 ## Stripe を設定する [サーバー側] [クライアント側] アプリケーションから 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 を作成または取得する [サーバー側] 今後の支払いに Klarna 支払い方法を再利用するには、これを *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 "<>:" ``` ## PaymentIntent または SetupIntent を作成する [サーバー側] ユースケースに最適なシナリオを以下から選択してください。 #### 初回の支払いを伴うサブスクリプション サブスクリプションを設定し、初回決済を請求する場合は、[setup_future_usage](https://docs.stripe.com/API/payment_intents/create.md#create_payment_intent-setup_future_usage) を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### サブスクリプションと非サブスクリプション製品 1 回限りの商品に対する請求とサブスクリプションの設定を同時に行う場合は、[setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage)を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=100000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=One-off product" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][1][product_name]=Annual subscription" \ -d "amount_details[line_items][1][unit_cost]=5000" \ -d "amount_details[line_items][1][quantity]=1" \ -d "amount_details[line_items][1][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### トライアル付きのサブスクリプション トライアル期間付きのサブスクリプションを設定する場合は、Setup Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=10000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 結果として得られる SetupIntent には、[client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### オンデマンド支払い *オンデマンド* (When a customer stores their payment method with a business, they can make on-demand future purchases without re-authenticating, such as ordering a ride in their ride-share app)のユースケースでは、Payment Intents API または Setup Intents API のいずれかを使用できます。 支払いの処理と決済手段の保存を同時に行う場合は、Payment Intents API を使用してください。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d "customer={{CUSTOMER_ID}}" \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Your product name" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" ``` 後で使用するために決済手段の保存のみが必要で、支払いを処理しない場合は、Setup Intents API を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d usage=on_session ``` ## 顧客をリダイレクトする [クライアント側] 顧客が今後の支払いのために Klarna アカウントを設定する場合は、[Stripe.js](https://docs.stripe.com/js.md) を使用して PaymentIntent または SetupIntent を確定します。Stripe.js は、決済フローの構築の基盤となる JavaScript ライブラリです。以下で説明するリダイレクトなどの機能を自動的に処理し、将来的に他の決済手段にもシステムを拡張できるようにします。 ### Stripe.js を設定する Stripe.js スクリプトを決済ページに含めるには、このスクリプトを HTML ファイルの head に追加します。 ```html Checkout ``` 以下の JavaScript で Stripe.js のインスタンスをチェックアウトページに作成します。 ```javascript import {loadStripe} from '@stripe/stripe-js'; const stripe = await loadStripe('<>'); ``` クライアント側で設定を確定するには、ステップ 3 で作成した PaymentIntent または SetupIntent オブジェクトの client secret を渡します。 client secret は、Stripe API リクエストを認証する API キーとは異なります。これは支払いを完了できるため、慎重に扱う必要があります。記録したり、URL に埋め込んだり、当該の顧客以外に漏洩することがないようにしてください。 ### インテントの確定とリダイレクト PaymentIntent または SetupIntent を確定すると、Stripe.js は顧客を Klarna に自動的にリダイレクトします。顧客がプロセスを完了すると、Klarna は指定の `return_url` に顧客をリダイレクトします。 #### PaymentIntent ステップ 3 で PaymentIntent を作成したときに受け取った `client_secret` を渡します。client secret は、Stripe API リクエストを認証する API キーとは異なります。支払いが完了してしまう可能性があるため、client secret の処理には十分注意してください。ログに記録したり、URL に埋め込んだり、顧客以外に公開したりすることはできません。 ```javascript // Redirects away from the client const {error} = await stripe.confirmKlarnaPayment( '{{PAYMENT_INTENT_CLIENT_SECRET}}', { payment_method: { billing_details: { email: 'jenny@example.com', address: { country: 'DE' } } }, return_url: 'https://example.com/setup/complete' } ); if (error) { // Inform the customer that there was an error. } ``` #### SetupIntent ステップ 3 で SetupIntent を作成したときに受け取った `client_secret` を渡します。client secret は、Stripe API リクエストを認証する API キーとは異なります。支払いが完了してしまう可能性があるため、client secret の処理には十分注意してください。ログに記録したり、URL に埋め込んだり、顧客以外に公開したりすることはできません。 ```javascript // Redirects away from the client const {error} = await stripe.confirmKlarnaSetup( '{{SETUP_INTENT_CLIENT_SECRET}}', { payment_method: { billing_details: { email: 'jenny@example.com', address: { country: 'DE', }, }, }, 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. } ``` ## Webhook を監視する [サーバー側] [Webhook](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) などの方法で、顧客が請求契約を承認したことを確認します。顧客が支払いステータスページに戻ることに依存しないようにしてください。 顧客が請求契約の承認を正常に完了すると、Stripe は [payment_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.succeeded) または [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) イベントを送信し、インテントのステータスは `succeeded` に移行します。生成された [payment_method](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-payment_method) ID を保存し、保存した PaymentMethod を使用して後で支払いを行います。 顧客が請求契約の承認を正常に行えなかった場合、Stripe は [payment_intent.payment_failed](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.payment_failed) または [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信し、インテントのステータスは `requires_payment_method` に戻ります。 ## 保存済みの Klarna の決済手段に請求する [サーバー側] #### サブスクリプションの更新 オフセッションで顧客に請求する準備ができたら、*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) を作成します。その他いくつかのパラメーターを設定して、*オフセッションの支払い* (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 に設定します。 - [return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url)を指定して、顧客が Klarna の Web サイトから戻った後に Stripe がリダイレクトする必要がある場所を指定します。 更新のたびにサブスクリプションの詳細と項目を送信します。決済手段を設定したときと同じ `reference` をサブスクリプションに使用します。決済手段の保存後にサブスクリプションの詳細が変更された場合は、同じ `reference` を指定して新しい情報を送信します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` #### オンデマンド支払い 顧客に支払う準備ができたら、*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) を作成します。*オフセッションの決済* (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)を`false`に設定して、この支払い試行中に顧客が決済フローにいることを示します。認証が必要なエラーなどが発生した場合、顧客は Klarna のページにリダイレクトされ、認証するか問題を解決します。 - 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 に設定します。 以下の例では、Klarna のリスク評価とオーソリ率の向上に役立つ [payment_method_options](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options) を使用したオプション詳細が含まれています。これには、今後の注文予定額や決済の頻度など、保存された Klarna 支払い方法を将来どのように使用すべきかの詳細が含まれます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=false \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=1099" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]={{SUBSCRIPTION_ID}}" \ -d "payment_method_options[klarna][on_demand][average_amount]=1000" \ -d "payment_method_options[klarna][on_demand][minimum_amount]=100" \ -d "payment_method_options[klarna][on_demand][maximum_amount]=10000" \ -d "payment_method_options[klarna][on_demand][interval]=year" \ -d "payment_method_options[klarna][on_demand][interval_count]=1" ``` ## 再利用可能な決済方法の取り消しを処理する [サーバー側] 再利用可能な決済方法を取り消すには、次の 2 つの方法があります。 - 顧客は、Klarna モバイルアプリケーションで再利用可能な決済方法を無効にすることができます。この場合、Stripe は [mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated)イベントを送信します。これを処理するには、[webhook](https://docs.stripe.com/webhooks.md)イベントをサブスクライブし、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化します。 - サポートされている場合、顧客は、UIで再利用可能な決済方法を無効にすることもできます。このシナリオでは、サーバーは [PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化を処理できます。 どちらの場合も、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出した後、Stripe から [payment_method.detached](https://docs.stripe.com/api/events/types.md#event_types-payment_method.detached)イベントが送信されます。 ## 導入をテストする テスト環境でシステムをテストする場合、Klarna へのリダイレクト結果をさまざまなパターンでシミュレートできます。 > Klarna はセッションの追跡に クッキーを使用します。さまざまな顧客の所在地をテストするには、前のセッションで Klarna サンドボックスからログアウトし、関連するトリガーを使用します。 下記では、現在対応している顧客の国のテストデータを特別に選択しています。サンドボックスでは、Klarna は指定されたメールアドレスに基づいて取引を承認または拒否します。 #### オーストラリア | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 03-05-1994 | | 名 | テスト | John | | 姓 | Person-au | snow | | 町名・番地 | Wharf St | Silverwater Rd | | 番地等 | 4 | 1-5 | | 郵便番号 | 4877 | 2128 | | 市区町村 | Port Douglas | Silverwater | | 地域 | QLD | NSW | | 電話番号 | +61473752244 | +61473763254 | | メールアドレス | customer@email.au | customer+denied@email.au | #### オーストリア | | 承認済み | 拒否 | | ------- | ------------------ | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-at | Person-at | | メールアドレス | customer@email.at | customer+denied@email.at | | 町名・番地 | Mariahilfer Straße | Mariahilfer Straße | | 番地 | 47 | 47 | | 市区町村 | Wien | Wien | | 郵便番号 | 1060 | 1060 | | 電話番号 | +4306762600456 | +4306762600745 | #### ベルギー | | 承認済み | 拒否済み | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-be | Person-be | | メールアドレス | customer@email.be | customer+denied@email.be | | 町名・番地 | Grote Markt | Grote Markt | | 番地 | 1 | 1 | | 市 | Brussel | Brussel | | 郵便番号 | 1000 | 1000 | | 電話番号 | +32485121291 | +32485212123 | #### カナダ | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-ca | Person-ca | | 町名・番地 | 2693 Byron Rd | 2693 Byron Rd | | 郵便番号 | V7H 1L9 | V7H 1L9 | | 市区町村 | North Vancouver | North Vancouver | | 地域 | BC | BC | | 電話番号 | +15197438620 | +15197308624 | | メールアドレス | customer@email.ca | customer+denied@email.ca | #### チェコ | | 承認 | 拒否 | | ------- | ------------------ | ------------------------ | | 生年月日 | 01-01-1970 | 27-06-1992 | | 名 | テスト | テスト | | 姓 | Person-cz | Person-cz | | メールアドレス | customer@email.cz | customer+denied@email.cz | | 町名・番地 | Zazvorkova 1480/11 | Zázvorkova 1480/11 | | 郵便番号 | 155 00 | 155 00 | | 市区町村 | Praha | PRAHA 13 | | 電話番号 | +420771613715 | +420771623691 | #### デンマーク | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1980 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-dk | Person-dk | | メールアドレス | customer@email.dk | customer+denied@email.dk | | 町名・番地 | Dantes Plads | Nygårdsvej | | 番地 | 7 | 65 | | 市区町村 | København Ø | København Ø | | 郵便番号 | 1556 | 2100 | | 電話番号 | +4542555628 | +4552555348 | #### フィンランド | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1999 | 01-01-1999 | | 名 | テスト | Person FI | | 姓 | Person-fi | テスト | | メールアドレス | customer@email.fi | customer+denied@email.fi | | 町名・番地 | Mannerheimintie | Mannerheimintie | | 番地 | 34 | 34 | | 市区町村 | Helsinki | Helsinki | | 郵便番号 | 00100 | 00100 | | 電話番号 | +358401234567 | +358401234568 | #### フランス | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 10-07-1990 | 10-07-1990 | | 出生地 | パリ | パリ | | 名 | テスト | テスト | | 姓 | Person-fr | Person-fr | | メールアドレス | customer@email.fr | customer+denied@email.fr | | 町名・番地 | rue La Fayette | rue La Fayette | | 番地 | 33 | 33 | | 市区町村 | パリ | パリ | | 郵便番号 | 75009 | 75009 | | 電話番号 | +33689854321 | +33687984322 | #### ドイツ | | 承認済み | 拒否済み | | ------- | --------------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | 模擬 | テスト | | 姓 | 模擬 | Person-de | | メールアドレス | customer@email.de | customer+denied@email.de | | 町名・番地 | Neue Schönhauser Str. | Neue Schönhauser Str. | | 番地 | 2 | 2 | | 市 | Berlin | Berlin | | 郵便番号 | 10178 | 10178 | | 電話番号 | +49017614284340 | +49017610927312 | #### ギリシャ | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 納税者番号 | 090000045 | 090000045 | | 生年月日 | 01-01-1960 | 11-11-1970 | | 名 | テスト | テスト | | 姓 | Person-gr | Test-gr | | メールアドレス | customer@email.gr | customer+denied@email.gr | | 町名・番地 | Kephisias | Baralo | | 番地 | 37 | 56 | | 郵便番号 | 151 23 | 123 67 | | 市区町村 | Athina | Athina | | 電話番号 | +306945553624 | +306945553625 | #### アイルランド | | 承認済み | 拒否 | | -------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-ie | Person-ie | | メール | customer@email.ie | customer+denied@email.ie | | 町名・番地 | King Street South | King Street South | | 番地 | 30 | 30 | | 市区町村 | ダブリン | ダブリン | | EIR Code | D02 C838 | D02 C838 | | 電話番号 | +353855351400 | +353855351401 | #### イタリア | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1980 | 01-01-1980 | | 名 | テスト | テスト | | 姓 | Person-it | Person-it | | メールアドレス | customer@email.it | customer+denied@email.it | | 納税番号 | RSSBNC80A41H501B | RSSBNC80A41H501B | | 町名・番地 | Via Enrico Fermi | Via Enrico Fermi | | 番地 | 150 | 150 | | 市区町村 | Roma | Roma | | 郵便番号 | 00146 | 00146 | | 電話番号 | +393339741231 | +393312232389 | #### オランダ | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-nl | Person-nl | | メールアドレス | customer@email.nl | customer+denied@email.nl | | 町名・番地 | Osdorpplein | Osdorpplein | | 番地 | 137 | 137 | | 市区町村 | Amsterdam | Amsterdam | | 郵便番号 | 1068 SR | 1068 SR | | 電話番号 | +31689124321 | +31632167678 | #### ニュージーランド | | 承認 | 拒否 | | ------- | ------------------------ | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-nz | Person-nz | | 町名・番地 | Mount Wellington Highway | Mount Wellington Highway | | 番地等 | 286 | 286 | | 郵便番号 | 6011 | 6011 | | 市区町村 | Auckland | Wellington | | 電話番号 | +6427555290 | +642993007712 | | メールアドレス | customer@email.nz | customer+denied@email.nz | #### ノルウェー | | 承認済み | 拒否 | | ------- | ------------------- | ------------------------ | | 生年月日 | 01-08-1970 | 01-08-1970 | | 名 | Jane | テスト | | 姓 | テスト | Person-no | | メールアドレス | customer@email.no | customer+denied@email.no | | 個人番号 | NO1087000571 | NO1087000148 | | 町名・番地 | Edvard Munchs Plass | Sæffleberggate | | 番地 | 1 | 56 | | 市区町村 | Oslo | Oslo | | 郵便番号 | 0194 | 0563 | | 電話番号 | +4740123456 | +4740123457 | #### ポーランド | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 05-05-1967 | 05-05-1967 | | 名 | テスト | テスト | | 姓 | Person-pl | Person-pl | | 町名・番地 | Ul. Górczewska | Ul. Górczewska | | 番地等 | 124 | 124 | | 郵便番号 | 01-460 | 01-460 | | 市区町村 | Warszawa | Warszawa | | 電話番号 | +48795222223 | +48795223325 | | メールアドレス | customer@email.pl | customer+denied@email.pl | #### ポルトガル | | 承認 | 拒否 | | ------- | ------------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-pt | Person-pt | | 町名・番地 | Avenida Dom João II | Avenida Dom João II | | 番地等 | 40 | 40 | | 郵便番号 | 1990-094 | 1990-094 | | 市区町村 | Lisboa | Lisboa | | 電話番号 | +351935556731 | +351915593837 | | メールアドレス | customer@email.pt | customer+denied@email.pt | #### ルーマニア | | 承認されました | 拒否 | | ------------ | ----------------- | ------------------------ | | 生年月日 | 1970 年 12 月 25 日 | 1970 年 12 月 25 日 | | 名 | テスト | テスト | | 姓 | Person-ro | Person-ro | | メール | customer@email.ro | customer+denied@email.ro | | 町名・番地 | Drumul Taberei | Drumul Taberei | | 番地 | 35 | 35 | | 市区町村 | București | București | | セクター | Sectorul 6 | Sectorul 6 | | 郵便番号 | 061357 | 061357 | | 電話 | +40741209876 | +40707127444 | | 個人識別番号 (CNP) | 1701225193558 | | #### スペイン | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | DNI/NIE | 99999999R | 99999999R | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-es | Person-es | | メールアドレス | customer@email.es | customer+denied@email.es | | 町名・番地 | C. de Atocha | C. de Atocha | | 番地 | 27 | 27 | | 市区町村 | Madrid | Madrid | | 郵便番号 | 28012 | 28012 | | 電話番号 | +34672563009 | +34682425101 | #### スウェーデン | | 承認済み | 拒否 | | ------- | ----------------------- | ------------------------ | | 生年月日 | 21-03-1941 | 28-10-1941 | | 名 | Alice | テスト | | 姓 | テスト | Person-se | | メールアドレス | customer@email.se | customer+denied@email.se | | 町名・番地 | Södra Blasieholmshamnen | Karlaplan | | 番地 | 2 | 3 | | 市区町村 | Stockholm | Stockholm | | 郵便番号 | 11 148 | 11 460 | | 電話番号 | +46701740615 | +46701740620 | #### スイス | | 承認 | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 01-01-1990 | 01-01-2000 | | 名 | 受付済み | 顧客 | | 姓 | Person-ch | Person-ch | | 町名・番地 | Augustinergasse | Bahnhofstrasse | | 番地等 | 2 | 77 | | 郵便番号 | 4051 | 8001 | | 市区町村 | Basel | Zürich | | 電話番号 | +41758680000 | +41758680001 | | メールアドレス | customer@email.ch | customer+denied@email.ch | #### イギリス | | 承認済み | 拒否 | | ------- | --------------------- | ------------------------ | | 生年月日 | 1970/10/07 | 1970/10/07 | | 名 | テスト | テスト | | 姓 | Person-uk | Person-uk | | メールアドレス | customer@email.uk | customer+denied@email.uk | | 町名・番地 | New Burlington Street | New Burlington Street | | 番地 | 10 | 10 | | アパート | Apt 214 | Apt 214 | | 郵便番号 | W1S 3BE | W1S 3BE | | 市区町村 | London | London | | 電話番号 | +447755564318 | +447355505530 | #### アメリカ | | 承認済み | 拒否 | | ------- | ----------------- | ------------------------ | | 生年月日 | 07-10-1970 | 07-10-1970 | | 名 | テスト | テスト | | 姓 | Person-us | Person-us | | メールアドレス | customer@email.us | customer+denied@email.us | | 町名・番地 | Amsterdam Ave | Amsterdam Ave | | 番地 | 509 | 509 | | 市区町村 | New York | New York | | 州 | New York | New York | | 郵便番号 | 10024-3941 | 10024-3941 | | 電話番号 | +13106683312 | +13106354386 | ### 2 段階認証 6 桁の数字であれば、2 段階認証コードとして有効です。`999999` を使用すると、認証は失敗します。 ### 返済方法 Klarna のフロー内では、以下のテスト値を使用し、さまざまな返済方法を試すことができます。 | タイプ | 値 | | -------- | ---------------------------------------------------------- | | 口座引き落とし | DE11520513735120710131 | | 銀行振込 | デモの銀行 | | クレジットカード | - 番号: 4111 1111 1111 1111 - CVV: 123 - 有効期限: 任意の将来日付 | | デビットカード | - カード番号:4012 8888 8888 1881 - CVV: 123 - 有効期限: 任意の将来日付 | ## Optional: Klarna リダイレクトを手動で処理する [サーバー側] クライアント側で Klarna のリダイレクトと請求のオーソリを処理する場合は、Stripe.js を使用することをお勧めします。ネイティブ SDK を使用すると、システムを他の決済手段に拡張できます。ただし、次の手順に従って、顧客をサーバーに手動でリダイレクトすることも可能です。 `confirm: true` を追加して、作成時に PaymentIntent または 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)します。ユーザーが Klarna のウェブサイトまたはモバイルアプリケーションで設定を完了した後、Stripe がユーザーをリダイレクトする場所を `return_url` で指定します。 Intent のステータスは `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": ["klarna"], "single_use_mandate": null, "usage": "off_session" } ``` `next_action.redirect_to_url.url` プロパティで指定された URL に顧客をリダイレクトします。 ## Optional: 保存済みの決済手段をアップグレードする [サーバー側] 顧客がサブスクリプションをアップグレードした場合に、Klarna の決済フローを再度実行します。これにより、Klarna のサブスクリプション情報が正確になり、オーソリ率が最適化されます。 #### アップグレードと料金改訂 保存済みの決済手段に請求する場合と同じ構造の [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、`setup_future_usage` を含めて、既存の決済手段をアップグレードすることを示します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=10000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d setup_future_usage=off_session \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=10000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 生成された PaymentIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトを処理する方法](https://docs.stripe.com/payments/klarna/set-up-future-payments.md?web-or-mobile=web&payment-ui=elements#web-redirect-customer) をご覧ください。 #### アップグレードして後で決済 [SetupIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、既存の保存された決済手段を渡します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=20000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 生成された SetupIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md?web-or-mobile=web&payment-ui=elements#web-redirect-customer) 方法をご覧ください。 ## Optional: 保存された Klarna アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した Klarna アカウントを削除できます。Klarna アカウントは、顧客がサブスクリプションをキャンセルした場合など、二度と使用されないことがわかっている場合に削除します。 ```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/klarna/set-up-future-payments?payment-ui=mobile&platform=ios. [Payment Intents API](https://docs.stripe.com/api/payment_intents.md) と一緒に [setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage) または [Setup Intents API](https://docs.stripe.com/api/setup_intents.md) を使用して、Klarna の決済手段情報を保存します。 ## 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 iOS SDK](https://github.com/stripe/stripe-ios) はオープンソースです。[詳細なドキュメントが提供されており](https://stripe.dev/stripe-ios/index.html)、iOS 13 以降をサポートするアプリと互換性があります。 #### Swift Package Manager SDK をインストールするには、以下のステップに従います。 1. Xcode で、**File (ファイル)** > **Add Package Dependencies… (パッケージ依存関係を追加)** を選択し、リポジトリー URL として `https://github.com/stripe/stripe-ios-spm` を入力します。 1. [リリースページ](https://github.com/stripe/stripe-ios/releases)から最新のバージョン番号を選択します。 1. **StripePaymentsUI** 製品を[アプリのターゲット](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app)に追加します。 #### CocoaPods 1. まだインストールしていない場合は、[CocoaPods](https://guides.cocoapods.org/using/getting-started.html) の最新バージョンをインストールします。 1. 既存の [Podfile](https://guides.cocoapods.org/syntax/podfile.html) がない場合は、以下のコマンドを実行して作成します。 ```bash pod init ``` 1. この行を `Podfile` に追加します。 ```podfile pod 'StripePaymentsUI' ``` 1. 以下のコマンドを実行します。 ```bash pod install ``` 1. これ以降は、Xcode でプロジェクトを開く際に、`.xcodeproj` ファイルではなく、必ず `.xcworkspace` ファイルを使用するということを忘れないでください。 1. 今後、SDK の最新バージョンに更新するには、以下を実行します。 ```bash pod update StripePaymentsUI ``` #### Carthage 1. まだインストールしていない場合は、[Carthage](https://github.com/Carthage/Carthage#installing-carthage) の最新バージョンをインストールします。 1. この行を `Cartfile` に追加します。 ```cartfile github "stripe/stripe-ios" ``` 1. [Carthage のインストール手順](https://github.com/Carthage/Carthage#if-youre-building-for-ios-tvos-or-watchos)に従います。必ず、[こちら](https://github.com/stripe/stripe-ios/tree/master/StripePaymentsUI/README.md#manual-linking)にリストされている必要なフレームワークのすべてを埋め込んでください。 1. 今後、SDK の最新バージョンに更新するには、以下のコマンドを実行します。 ```bash carthage update stripe-ios --platform ios ``` #### 手動のフレームワーク 1. Stripe の [GitHub リリースページ](https://github.com/stripe/stripe-ios/releases/latest)に移動して、**Stripe.xcframework.zip** をダウンロードして解凍します。 1. **StripePaymentsUI.xcframework** を、Xcode プロジェクトの **General (一般) ** 設定の **Embedded Binaries (埋め込みバイナリー)** セクションにドラッグします。**Copy items if needed (必要に応じてアイテムをコピーする)** を必ず選択してください。 1. [こちら](https://github.com/stripe/stripe-ios/tree/master/StripePaymentsUI/README.md#manual-linking)にリストされている必要なフレームワークのすべてに対して、ステップ 2 を繰り返します。 1. 今後、Stripe の SDK の最新バージョンに更新するには、ステップ 1 から 3 を繰り返します。 > SDK の最新リリースおよび過去バージョンの詳細については、GitHub の [Releases (リリース)](https://github.com/stripe/stripe-ios/releases) ページをご覧ください。リポジトリの[リリースをウォッチ](https://help.github.com/en/articles/watching-and-unwatching-releases-for-a-repository#watching-releases-for-a-repository)して、新しいリリースの公開時に通知を受け取ることも可能です。 アプリの起動時に Stripe [公開可能キー](https://dashboard.stripe.com/test/apikeys)を使用して SDK を設定します。これにより、アプリが Stripe API にリクエストを送信できるようになります。 #### Swift ```swift import UIKitimportStripePaymentsUI @main class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {StripeAPI.defaultPublishableKey = "<>" // do any other necessary launch configuration return true } } ``` > テストおよび開発時には[テストキー](https://docs.stripe.com/keys.md#obtain-api-keys)を使用し、アプリの公開時には[本番環境](https://docs.stripe.com/keys.md#test-live-modes)キーを使用します。 ## Customer を作成または取得する [サーバー側] 今後の支払いに Klarna 支払い方法を再利用するには、これを *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 "<>:" ``` ## PaymentIntent または SetupIntent を作成する [サーバー側] ユースケースに最適なシナリオを以下から選択してください。 #### 初回の支払いを伴うサブスクリプション サブスクリプションを設定し、初回決済を請求する場合は、[setup_future_usage](https://docs.stripe.com/API/payment_intents/create.md#create_payment_intent-setup_future_usage) を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### サブスクリプションと非サブスクリプション製品 1 回限りの商品に対する請求とサブスクリプションの設定を同時に行う場合は、[setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage)を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=100000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=One-off product" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][1][product_name]=Annual subscription" \ -d "amount_details[line_items][1][unit_cost]=5000" \ -d "amount_details[line_items][1][quantity]=1" \ -d "amount_details[line_items][1][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### トライアル付きのサブスクリプション トライアル期間付きのサブスクリプションを設定する場合は、Setup Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=10000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 結果として得られる SetupIntent には、[client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### オンデマンド支払い *オンデマンド* (When a customer stores their payment method with a business, they can make on-demand future purchases without re-authenticating, such as ordering a ride in their ride-share app)のユースケースでは、Payment Intents API または Setup Intents API のいずれかを使用できます。 支払いの処理と決済手段の保存を同時に行う場合は、Payment Intents API を使用してください。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d "customer={{CUSTOMER_ID}}" \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Your product name" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" ``` 後で使用するために決済手段の保存のみが必要で、支払いを処理しない場合は、Setup Intents API を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d usage=on_session ``` ## 支払い方法の詳細を収集する [クライアント側] #### Swift ```swift // Klarna doesn't require additional parameters so we only need to pass the initialized // STPPaymentMethodKlarnaParams instance to STPPaymentMethodParams let klarnaParams = STPPaymentMethodKlarnaParams() let paymentMethodParams = STPPaymentMethodParams(klarna: klarnaParams, billingDetails: nil, metadata: nil) ``` ## Stripe に支払いを送信する [クライアント側] 作成したインテントから client secret を取得し、[confirmPaymentIntent](https://stripe.dev/stripe-ios/stripepayments/documentation/stripepayments/stppaymenthandler/confirmpayment\(_:with:completion:\)) または [confirmSetupIntent](https://stripe.dev/stripe-ios/stripepayments/documentation/stripepayments/stppaymenthandler/confirmsetupintent\(_:with:completion:\)) を呼び出します。WebView が表示され、顧客はそこで Klarna での支払いを完了できます。その後、支払いの結果とともに完了ブロックが呼び出されます。 #### PaymentIntent #### Swift ```swift let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret) paymentIntentParams.paymentMethodParams = paymentMethodParams paymentIntentParams.returnURL = "payments-example://stripe-redirect" STPPaymentHandler.shared().confirmPayment(withParams: paymentIntentParams, authenticationContext: self) { (handlerStatus, paymentIntent, error) in switch handlerStatus { case .succeeded: // Payment succeeded case .canceled: // Payment was canceled case .failed: // Payment failed @unknown default: fatalError() } } ``` #### SetupIntent #### 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) などの方法で、顧客が請求契約を承認したことを確認します。顧客が支払いステータスページに戻ることに依存しないようにしてください。 顧客が請求契約の承認を正常に完了すると、Stripe は [payment_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.succeeded) または [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) イベントを送信し、インテントのステータスは `succeeded` に移行します。生成された [payment_method](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-payment_method) ID を保存し、保存した PaymentMethod を使用して後で支払いを行います。 顧客が請求契約の承認を正常に行えなかった場合、Stripe は [payment_intent.payment_failed](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.payment_failed) または [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信し、インテントのステータスは `requires_payment_method` に戻ります。 ## 保存済みの Klarna の決済手段に請求する [サーバー側] #### サブスクリプションの更新 オフセッションで顧客に請求する準備ができたら、*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) を作成します。その他いくつかのパラメーターを設定して、*オフセッションの支払い* (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 に設定します。 - [return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url)を指定して、顧客が Klarna の Web サイトから戻った後に Stripe がリダイレクトする必要がある場所を指定します。 更新のたびにサブスクリプションの詳細と項目を送信します。決済手段を設定したときと同じ `reference` をサブスクリプションに使用します。決済手段の保存後にサブスクリプションの詳細が変更された場合は、同じ `reference` を指定して新しい情報を送信します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` #### オンデマンド支払い 顧客に支払う準備ができたら、*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) を作成します。*オフセッションの決済* (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)を`false`に設定して、この支払い試行中に顧客が決済フローにいることを示します。認証が必要なエラーなどが発生した場合、顧客は Klarna のページにリダイレクトされ、認証するか問題を解決します。 - 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 に設定します。 以下の例では、Klarna のリスク評価とオーソリ率の向上に役立つ [payment_method_options](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options) を使用したオプション詳細が含まれています。これには、今後の注文予定額や決済の頻度など、保存された Klarna 支払い方法を将来どのように使用すべきかの詳細が含まれます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=false \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=1099" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]={{SUBSCRIPTION_ID}}" \ -d "payment_method_options[klarna][on_demand][average_amount]=1000" \ -d "payment_method_options[klarna][on_demand][minimum_amount]=100" \ -d "payment_method_options[klarna][on_demand][maximum_amount]=10000" \ -d "payment_method_options[klarna][on_demand][interval]=year" \ -d "payment_method_options[klarna][on_demand][interval_count]=1" ``` ## 再利用可能な決済方法の取り消しを処理する [サーバー側] 再利用可能な決済方法を取り消すには、次の 2 つの方法があります。 - 顧客は、Klarna モバイルアプリケーションで再利用可能な決済方法を無効にすることができます。この場合、Stripe は [mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated)イベントを送信します。これを処理するには、[webhook](https://docs.stripe.com/webhooks.md)イベントをサブスクライブし、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化します。 - サポートされている場合、顧客は、UIで再利用可能な決済方法を無効にすることもできます。このシナリオでは、サーバーは [PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化を処理できます。 どちらの場合も、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出した後、Stripe から [payment_method.detached](https://docs.stripe.com/api/events/types.md#event_types-payment_method.detached)イベントが送信されます。 ## Optional: Klarna リダイレクトを手動で処理する [サーバー側] クライアント側で Klarna のリダイレクトと請求のオーソリを処理する場合は、iOS SDK を使用することをお勧めします。ネイティブ SDK を使用すると、システムを他の決済手段に拡張できます。ただし、次の手順に従って、顧客をサーバーに手動でリダイレクトすることも可能です。 `confirm: true` を追加して、作成時に PaymentIntent または 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)します。ユーザーが Klarna のウェブサイトまたはモバイルアプリケーションで設定を完了した後、Stripe がユーザーをリダイレクトする場所を `return_url` で指定します。 作成後、Intent のステータスは `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": ["klarna"], "single_use_mandate": null, "usage": "off_session" } ``` `next_action.redirect_to_url.url` プロパティで指定された URL に顧客をリダイレクトします。 ## Optional: 保存済みの決済手段をアップグレードする [サーバー側] 顧客がサブスクリプションをアップグレードした場合は、Klarna 決済フローを再度実行します。これにより、Klarna のサブスクリプション情報が正確になり、オーソリ率が最適化されます。 #### アップグレードと料金改訂 保存済みの決済手段に請求する場合と同じ構造の [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、`setup_future_usage` を含めて、既存の決済手段をアップグレードすることを示します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=10000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d setup_future_usage=off_session \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=10000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 生成された PaymentIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md#ios-submit-payment) 方法をご覧ください。 #### アップグレードして後で決済 [SetupIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、既存の保存された決済手段を渡します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=20000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 生成された SetupIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md#ios-submit-payment) 方法をご覧ください。 ## Optional: 保存された Klarna アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した Klarna アカウントを削除できます。Klarna アカウントは、顧客がサブスクリプションをキャンセルした場合など、二度と使用されないことがわかっている場合に削除します。 ```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/klarna/set-up-future-payments?payment-ui=mobile&platform=android. [Payment Intents API](https://docs.stripe.com/api/payment_intents.md) と一緒に [setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage) または [Setup Intents API](https://docs.stripe.com/api/setup_intents.md) を使用して、Klarna の決済手段情報を保存します。 ## 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.8.0") // Include the financial connections SDK to support US bank account as a payment method implementation("com.stripe:financial-connections:23.8.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 を作成または取得する [サーバー側] 今後の支払いに Klarna 支払い方法を再利用するには、これを *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 "<>:" ``` ## PaymentIntent または SetupIntent を作成する [サーバー側] ユースケースに最適なシナリオを以下から選択してください。 #### 初回の支払いを伴うサブスクリプション サブスクリプションを設定し、初回決済を請求する場合は、[setup_future_usage](https://docs.stripe.com/API/payment_intents/create.md#create_payment_intent-setup_future_usage) を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### サブスクリプションと非サブスクリプション製品 1 回限りの商品に対する請求とサブスクリプションの設定を同時に行う場合は、[setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage)を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=100000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=One-off product" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][1][product_name]=Annual subscription" \ -d "amount_details[line_items][1][unit_cost]=5000" \ -d "amount_details[line_items][1][quantity]=1" \ -d "amount_details[line_items][1][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### トライアル付きのサブスクリプション トライアル期間付きのサブスクリプションを設定する場合は、Setup Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=10000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 結果として得られる SetupIntent には、[client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### オンデマンド支払い *オンデマンド* (When a customer stores their payment method with a business, they can make on-demand future purchases without re-authenticating, such as ordering a ride in their ride-share app)のユースケースでは、Payment Intents API または Setup Intents API のいずれかを使用できます。 支払いの処理と決済手段の保存を同時に行う場合は、Payment Intents API を使用してください。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d "customer={{CUSTOMER_ID}}" \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Your product name" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" ``` 後で使用するために決済手段の保存のみが必要で、支払いを処理しない場合は、Setup Intents API を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d usage=on_session ``` ## 支払い方法の詳細を収集する [クライアント側] #### Kotlin ```swift // Klarna doesn't require additional parameters so we only need to pass the initialized // STPPaymentMethodKlarnaParams instance to STPPaymentMethodParams val klarnaParams = STPPaymentMethodKlarnaParams() val paymentMethodParams = STPPaymentMethodParams.paramsWithKlarna(klarnaParams, null, null) ``` ## Stripe に支払い方法の詳細を送信する [クライアント側] 作成した PaymentIntent または SetupIntent から client secret を取得し、[PaymentLauncher confirm](https://stripe.dev/stripe-android/payments-core/com.stripe.android.payments.paymentlauncher/-payment-launcher/confirm.html)を呼び出します。WebView が表示され、顧客はそこで Klarna での支払いを完了できます。完了後、支払いの結果とともに、指定された `PaymentResultCallback` が呼び出されます。 #### PaymentIntent #### Kotlin ```kotlin class KlarnaPaymentActivity : 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 PaymentIntent on your backend and return the client_secret here val paymentIntentClientSecret = // … val klarnaParams = PaymentMethodCreateParams.createKlarna() val confirmParams = ConfirmPaymentIntentParams.create( paymentMethodCreateParams = klarnaParams, clientSecret = paymentIntentClientSecret, ) paymentLauncher.confirm(confirmParams) } private fun onPaymentResult(paymentResult: PaymentResult) { // Handle the setup result… } } ``` #### SetupIntent #### Kotlin ```kotlin class KlarnaSetupActivity : 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 klarnaParams = PaymentMethodCreateParams.createKlarna() val confirmParams = ConfirmSetupIntentParams.create( paymentMethodCreateParams = klarnaParams, 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) などの方法で、顧客が請求契約を承認したことを確認します。顧客が支払いステータスページに戻ることに依存しないようにしてください。 顧客が請求契約の承認を正常に完了すると、Stripe は [payment_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.succeeded) または [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) イベントを送信し、インテントのステータスは `succeeded` に移行します。生成された [payment_method](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-payment_method) ID を保存し、保存した PaymentMethod を使用して後で支払いを行います。 顧客が請求契約の承認を正常に行えなかった場合、Stripe は [payment_intent.payment_failed](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.payment_failed) または [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信し、インテントのステータスは `requires_payment_method` に戻ります。 ## 保存済みの Klarna の決済手段に請求する [サーバー側] #### サブスクリプションの更新 オフセッションで顧客に請求する準備ができたら、*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) を作成します。その他いくつかのパラメーターを設定して、*オフセッションの支払い* (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 に設定します。 - [return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url)を指定して、顧客が Klarna の Web サイトから戻った後に Stripe がリダイレクトする必要がある場所を指定します。 更新のたびにサブスクリプションの詳細と項目を送信します。決済手段を設定したときと同じ `reference` をサブスクリプションに使用します。決済手段の保存後にサブスクリプションの詳細が変更された場合は、同じ `reference` を指定して新しい情報を送信します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` #### オンデマンド支払い 顧客に支払う準備ができたら、*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) を作成します。*オフセッションの決済* (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)を`false`に設定して、この支払い試行中に顧客が決済フローにいることを示します。認証が必要なエラーなどが発生した場合、顧客は Klarna のページにリダイレクトされ、認証するか問題を解決します。 - 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 に設定します。 以下の例では、Klarna のリスク評価とオーソリ率の向上に役立つ [payment_method_options](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options) を使用したオプション詳細が含まれています。これには、今後の注文予定額や決済の頻度など、保存された Klarna 支払い方法を将来どのように使用すべきかの詳細が含まれます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=false \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=1099" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]={{SUBSCRIPTION_ID}}" \ -d "payment_method_options[klarna][on_demand][average_amount]=1000" \ -d "payment_method_options[klarna][on_demand][minimum_amount]=100" \ -d "payment_method_options[klarna][on_demand][maximum_amount]=10000" \ -d "payment_method_options[klarna][on_demand][interval]=year" \ -d "payment_method_options[klarna][on_demand][interval_count]=1" ``` ## 再利用可能な決済方法の取り消しを処理する [サーバー側] 再利用可能な決済方法を取り消すには、次の 2 つの方法があります。 - 顧客は、Klarna モバイルアプリケーションで再利用可能な決済方法を無効にすることができます。この場合、Stripe は [mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated)イベントを送信します。これを処理するには、[webhook](https://docs.stripe.com/webhooks.md)イベントをサブスクライブし、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化します。 - サポートされている場合、顧客は、UIで再利用可能な決済方法を無効にすることもできます。このシナリオでは、サーバーは [PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化を処理できます。 どちらの場合も、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出した後、Stripe から [payment_method.detached](https://docs.stripe.com/api/events/types.md#event_types-payment_method.detached)イベントが送信されます。 ## Optional: Klarna リダイレクトを手動で処理する [サーバー側] クライアントで Klarna のリダイレクトと請求のオーソリを処理する場合は、ネイティブ SDK を使用することをお勧めします。ネイティブ SDK を使用すると、システムを他の決済手段に拡張できます。ただし、次の手順に従って、顧客をサーバーに手動でリダイレクトすることも可能です。 `confirm: true` を追加して、作成時に PaymentIntent または 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)します。ユーザーが Klarna のウェブサイトまたはモバイルアプリケーションで設定を完了した後、Stripe がユーザーをリダイレクトする場所を `return_url` で指定します。 Intent のステータスが `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": ["klarna"], "single_use_mandate": null, "usage": "off_session" } ``` `next_action.redirect_to_url.url` プロパティで指定された URL に顧客をリダイレクトします。 ## Optional: 保存済みの決済手段をアップグレードする [サーバー側] 顧客がサブスクリプションをアップグレードした場合は、Klarna 決済フローを再度実行します。これにより、Klarna のサブスクリプション情報が正確になり、オーソリ率が最適化されます。 #### アップグレードと料金改訂 保存済みの決済手段に請求する場合と同じ構造の [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、`setup_future_usage` を含めて、既存の決済手段をアップグレードすることを示します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=10000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d setup_future_usage=off_session \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=10000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 生成された PaymentIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md#submit-payment) 方法をご覧ください。 #### アップグレードして後で決済 [SetupIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、既存の保存された決済手段を渡します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=20000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 生成された SetupIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md#submit-payment) 方法をご覧ください。 ## Optional: 保存された Klarna アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した Klarna アカウントを削除できます。Klarna アカウントは、顧客がサブスクリプションをキャンセルした場合など、二度と使用されないことがわかっている場合に削除します。 ```curl curl -X POST https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/detach \ -u "<>:" ``` # React Native > This is a React Native for when payment-ui is mobile and platform is react-native. View the full page at https://docs.stripe.com/payments/klarna/set-up-future-payments?payment-ui=mobile&platform=react-native. [Payment Intents API](https://docs.stripe.com/api/payment_intents.md) と一緒に [setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage) または [Setup Intents API](https://docs.stripe.com/api/setup_intents.md) を使用して、Klarna の決済手段情報を保存します。 ## Stripe を設定する [サーバー側] [クライアント側] まず、Stripe アカウントが必要です。[今すぐご登録ください](https://dashboard.stripe.com/register)。 ### サーバー側 この導入には、 Stripe API と通信するエンドポイントがサーバー上に必要です。サーバーからStripe API にアクセスするには、公式ライブラリを使用します。 #### 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' ``` ### クライアント側 [React Native SDK](https://github.com/stripe/stripe-react-native) はオープンソースであり、詳細なドキュメントが提供されています。内部では、[ネイティブの iOS](https://github.com/stripe/stripe-ios) および [Android](https://github.com/stripe/stripe-android) の SDK を使用します。Stripe の React Native SDK をインストールするには、プロジェクトのディレクトリーで (使用するパッケージマネージャーによって異なる) 次のいずれかのコマンドを実行します。 #### yarn ```bash yarn add @stripe/stripe-react-native ``` #### npm ```bash npm install @stripe/stripe-react-native ``` 次に、その他の必要な依存関係をインストールします。 - iOS の場合は、**ios** ディレクトリに移動して `pod install` を実行し、必要なネイティブ依存関係もインストールします。 - Android の場合は、依存関係をインストールする必要はありません。 > [公式の TypeScript ガイド](https://reactnative.dev/docs/typescript#adding-typescript-to-an-existing-project)に従って TypeScript のサポートを追加することをお勧めします。 ### Stripe の初期化 React Native アプリで Stripe を初期化するには、決済画面を `StripeProvider` コンポーネントでラップするか、`initStripe` 初期化メソッドを使用します。`publishableKey` の API [公開可能キー](https://docs.stripe.com/keys.md#obtain-api-keys)のみが必要です。次の例は、`StripeProvider` コンポーネントを使用して Stripe を初期化する方法を示しています。 ```jsx import { useState, useEffect } from 'react'; import { StripeProvider } from '@stripe/stripe-react-native'; function App() { const [publishableKey, setPublishableKey] = useState(''); const fetchPublishableKey = async () => { const key = await fetchKey(); // fetch key from your server here setPublishableKey(key); }; useEffect(() => { fetchPublishableKey(); }, []); return ( {/* Your app code here */} ); } ``` > テストおよび開発時には API の[テストキー](https://docs.stripe.com/keys.md#obtain-api-keys)を使用し、アプリの公開時には[本番環境](https://docs.stripe.com/keys.md#test-live-modes)キーを使用します。 ## 戻り先 URL を設定する (iOS のみ) [クライアント側] 顧客がアプリを終了すると (Safari やバンキングアプリで認証するなど)、自動的にアプリに戻るための方法を提供します。多くの決済手段タイプで、戻り先 URL の指定が「必須」です。戻り先 URL を有効にしていても、指定がされていないと、戻り先 URL が必要な決済手段をユーザーに提示できません。 戻り先 URL を指定するには、以下のようにします。 1. カスタム URL を[登録](https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app#Register-your-URL-scheme)します。ユニバーサルリンクはサポートされていません。 1. カスタム URL を[設定](https://reactnative.dev/docs/linking) します。 1. 以下のように、URL を Stripe SDK に転送するようにルートコンポーネントを設定します。 > Expo を使用している場合は、`app.json` ファイルで[スキームを設定](https://docs.expo.io/guides/linking/#in-a-standalone-app)します。 ```jsx import { useEffect, useCallback } from 'react'; import { Linking } from 'react-native'; import { useStripe } from '@stripe/stripe-react-native'; export default function MyApp() { const { handleURLCallback } = useStripe(); const handleDeepLink = useCallback( async (url: string | null) => { if (url) { const stripeHandled = await handleURLCallback(url); if (stripeHandled) { // This was a Stripe URL - you can return or add extra handling here as you see fit } else { // This was NOT a Stripe URL – handle as you normally would } } }, [handleURLCallback] ); useEffect(() => { const getUrlAsync = async () => { const initialUrl = await Linking.getInitialURL(); handleDeepLink(initialUrl); }; getUrlAsync(); const deepLinkListener = Linking.addEventListener( 'url', (event: { url: string }) => { handleDeepLink(event.url); } ); return () => deepLinkListener.remove(); }, [handleDeepLink]); return ( ); } ``` ネイティブ URL スキームの詳細については、[Android](https://developer.android.com/training/app-links/deep-linking) および [iOS](https://developer.apple.com/documentation/xcode/allowing_apps_and_websites_to_link_to_your_content/defining_a_custom_url_scheme_for_your_app) のドキュメントをご覧ください。 ## 設定前に Customer を作成または取得する [サーバー側] 今後の支払いに Klarna 支払い方法を再利用するには、これを *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 "<>:" ``` ## PaymentIntent または SetupIntent を作成する [サーバー側] ユースケースに最適なシナリオを以下から選択してください。 #### 初回の支払いを伴うサブスクリプション サブスクリプションを設定し、初回決済を請求する場合は、[setup_future_usage](https://docs.stripe.com/API/payment_intents/create.md#create_payment_intent-setup_future_usage) を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### サブスクリプションと非サブスクリプション製品 1 回限りの商品に対する請求とサブスクリプションの設定を同時に行う場合は、[setup_future_usage](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-setup_future_usage)を指定して Payment Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=100000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=One-off product" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][1][product_name]=Annual subscription" \ -d "amount_details[line_items][1][unit_cost]=5000" \ -d "amount_details[line_items][1][quantity]=1" \ -d "amount_details[line_items][1][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" ``` 結果として得られる PaymentIntent には、[client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### トライアル付きのサブスクリプション トライアル期間付きのサブスクリプションを設定する場合は、Setup Intents API を使用します。サブスクリプションを設定する際にサブスクリプションの詳細を渡す必要があります。 サブスクリプション詳細の受け渡し: - 顧客が該当するすべての [Klarna決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) にアクセスできるようにします。これには、特定の期間のサブスクリプションでのみ利用可能な 3 回または 4 回分割決済が含まれます。 - Klarna が顧客のサブスクリプションデータをレンダリングする Klarna アプリで、購入詳細の不明確さによるサポート率や顧客の離脱を削減します。 [payment_method_options](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-payment_method_options-klarna)パラメーターを使用して、サブスクリプションの詳細を渡します。サブスクリプションの[リファレンス](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options-Klarna-Subscriptions-reference)は、顧客が表示しない任意の識別子です(たとえば、`GYM_ANNUAL_MEMBERSHIP` や `ID_123`など)。 > #### 保存された支払い方法を請求する際は同じリファレンスを使用してください > > 保存された支払い方法を請求する際は、同じサブスクリプションリファレンスを使用してください。お客様の導入で後でそれを使用する場合、リファレンス値は`amount_details[line_items][payment_method_options][klarna][subscription_reference]` フィールドの値と一致する必要があります。値と一致しない場合、エラーが返されます。 以下の例は、年間サブスクリプションの詳細を渡す方法を示しています。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=10000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 結果として得られる SetupIntent には、[client_secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) が含まれています。クライアント側にシークレットを渡して買い手を Klarna にリダイレクトし、保存されている決済手段を承認します。 #### オンデマンド支払い *オンデマンド* (When a customer stores their payment method with a business, they can make on-demand future purchases without re-authenticating, such as ordering a ride in their ride-share app)のユースケースでは、Payment Intents API または Setup Intents API のいずれかを使用できます。 支払いの処理と決済手段の保存を同時に行う場合は、Payment Intents API を使用してください。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d "customer={{CUSTOMER_ID}}" \ -d setup_future_usage=off_session \ -d "amount_details[line_items][0][product_name]=Your product name" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][quantity]=1" ``` 後で使用するために決済手段の保存のみが必要で、支払いを処理しない場合は、Setup Intents API を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d usage=on_session ``` ## 顧客をリダイレクトする [クライアント側] Stripe React Native SDK を使用して、PaymentIntent または SetupIntent を確定します。以下で説明するリダイレクトなどの機能を自動的に処理し、将来的に他の決済手段にもシステムを拡張できるようにします。 顧客は Klarna の請求契約ページにリダイレクトされ、今後の支払いに Klarna アカウントを使用することの承認を求められます。アクションが完了した後、再びアプリにリダイレクトされます。 #### PaymentIntent ```js import { confirmPayment } from '@stripe/stripe-react-native'; // In your component... const { error, paymentIntent } = await confirmPayment( '{{PAYMENT_INTENT_CLIENT_SECRET}}', { paymentMethodType: 'Klarna', paymentMethodData: { billingDetails: { email: 'jenny@example.com', address: { country: 'DE' } }, mandateData: { // Add mandate data... } } }); if (error) { // Something went wrong } ``` #### SetupIntent ```js import { confirmSetupIntent } from '@stripe/stripe-react-native'; // In your component... const { error, setupIntent } = await confirmSetupIntent( '{{SETUP_INTENT_CLIENT_SECRET}}', { paymentMethodType: 'Klarna', paymentMethodData: { billingDetails: { email: 'jenny@example.com', address: { country: 'DE' } }, mandateData: { // Add mandate data... } } }); if (error) { // Something went wrong } ``` ## Webhook を監視する [サーバー側] [Webhook](https://docs.stripe.com/payments/payment-intents/verifying-status.md#webhooks) などの方法で、顧客が請求契約を承認したことを確認します。顧客が支払いステータスページに戻ることに依存しないようにしてください。 顧客が請求契約の承認を正常に完了すると、Stripe は [payment_intent.succeeded](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.succeeded) または [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) イベントを送信し、インテントのステータスは `succeeded` に移行します。生成された [payment_method](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-payment_method) ID を保存し、保存した PaymentMethod を使用して後で支払いを行います。 顧客が請求契約の承認を正常に行えなかった場合、Stripe は [payment_intent.payment_failed](https://docs.stripe.com/api/events/types.md#event_types-payment_intent.payment_failed) または [setup_intent.setup_failed](https://docs.stripe.com/api/events/types.md#event_types-setup_intent.setup_failed) *Webhook* (A webhook is a real-time push notification sent to your application as a JSON payload through HTTPS requests) イベントを送信し、インテントのステータスは `requires_payment_method` に戻ります。 ## 保存済みの Klarna の決済手段に請求する [サーバー側] #### サブスクリプションの更新 オフセッションで顧客に請求する準備ができたら、*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) を作成します。その他いくつかのパラメーターを設定して、*オフセッションの支払い* (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 に設定します。 - [return_url](https://docs.stripe.com/api/payment_intents/confirm.md#confirm_payment_intent-return_url)を指定して、顧客が Klarna の Web サイトから戻った後に Stripe がリダイレクトする必要がある場所を指定します。 更新のたびにサブスクリプションの詳細と項目を送信します。決済手段を設定したときと同じ `reference` をサブスクリプションに使用します。決済手段の保存後にサブスクリプションの詳細が変更された場合は、同じ `reference` を指定して新しい情報を送信します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=5000 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=true \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=5000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` #### オンデマンド支払い 顧客に支払う準備ができたら、*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) を作成します。*オフセッションの決済* (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)を`false`に設定して、この支払い試行中に顧客が決済フローにいることを示します。認証が必要なエラーなどが発生した場合、顧客は Klarna のページにリダイレクトされ、認証するか問題を解決します。 - 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 に設定します。 以下の例では、Klarna のリスク評価とオーソリ率の向上に役立つ [payment_method_options](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_options) を使用したオプション詳細が含まれています。これには、今後の注文予定額や決済の頻度など、保存された Klarna 支払い方法を将来どのように使用すべきかの詳細が含まれます。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=1099 \ -d currency=usd \ -d confirm=true \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d off_session=false \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=1099" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]={{SUBSCRIPTION_ID}}" \ -d "payment_method_options[klarna][on_demand][average_amount]=1000" \ -d "payment_method_options[klarna][on_demand][minimum_amount]=100" \ -d "payment_method_options[klarna][on_demand][maximum_amount]=10000" \ -d "payment_method_options[klarna][on_demand][interval]=year" \ -d "payment_method_options[klarna][on_demand][interval_count]=1" ``` ## 再利用可能な決済方法の取り消しを処理する [サーバー側] 再利用可能な決済方法を取り消すには、次の 2 つの方法があります。 - 顧客は、Klarna モバイルアプリケーションで再利用可能な決済方法を無効にすることができます。この場合、Stripe は [mandate.updated](https://docs.stripe.com/api/events/types.md#event_types-mandate.updated)イベントを送信します。これを処理するには、[webhook](https://docs.stripe.com/webhooks.md)イベントをサブスクライブし、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化します。 - サポートされている場合、顧客は、UIで再利用可能な決済方法を無効にすることもできます。このシナリオでは、サーバーは [PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出して無効化を処理できます。 どちらの場合も、[PaymentMethod をデタッチ](https://docs.stripe.com/api/payment_methods/detach.md)を呼び出した後、Stripe から [payment_method.detached](https://docs.stripe.com/api/events/types.md#event_types-payment_method.detached)イベントが送信されます。 ## Optional: Klarna リダイレクトを手動で処理する [サーバー側] クライアント側で `confirmSetupIntent` を使用して Klarna のリダイレクトおよび請求のオーソリを処理するには、Stripe React Native SDK を使用することをお勧めします。ネイティブ SDK を使用すると、構築済みのシステムを他の支払い方法に拡張できるようになります。ただし、以下のステップを使用して、自社のサーバーで顧客を手動でリダイレクトすることもできます。 `confirm: true` を設定して、作成時に 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)します。SetupIntent を確定するときに `return_url` を指定して、ユーザーが Klarna の Web サイトまたはモバイルアプリで設定を完了した後に Stripe がユーザーをリダイレクトする必要がある場所を示す必要があります。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d "customer={{CUSTOMER_ID}}" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_types[]=klarna" \ -d "payment_method_data[type]=klarna" \ --data-urlencode "payment_method_data[billing_details][email]=jenny@example.com" \ -d "payment_method_data[billing_details][address][country]=DE" \ -d usage=off_session \ -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": ["klarna"], "single_use_mandate": null, "usage": "off_session" } ``` `next_action.redirect_to_url.url` プロパティで指定された URL に顧客をリダイレクトします。 ## Optional: 保存済みの決済手段をアップグレードする [サーバー側] 顧客がサブスクリプションをアップグレードした場合は、Klarna 決済フローを再度実行します。これにより、Klarna のサブスクリプション情報が正確になり、オーソリ率が最適化されます。 #### アップグレードと料金改訂 保存済みの決済手段に請求する場合と同じ構造の [PaymentIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、`setup_future_usage` を含めて、既存の決済手段をアップグレードすることを示します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/payment_intents \ -u "<>:" \ -d amount=10000 \ -d currency=usd \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ -d setup_future_usage=off_session \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "amount_details[line_items][0][product_name]=Annual subscription renewal" \ -d "amount_details[line_items][0][quantity]=1" \ -d "amount_details[line_items][0][unit_cost]=10000" \ -d "amount_details[line_items][0][payment_method_options][klarna][subscription_reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=year" ``` 生成された PaymentIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md#react-native-redirect-customer) 方法をご覧ください。 #### アップグレードして後で決済 [SetupIntent](https://docs.stripe.com/api/payment_intents.md) を作成し、既存の保存された決済手段を渡します。新しいサブスクリプション情報を渡し、決済手段を最初に保存したときと同じ `reference` を使用します。 ```curl curl https://api.stripe.com/v1/setup_intents \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d payment_method={{PAYMENT_METHOD_ID}} \ --data-urlencode "return_url=https://example.com/setup/complete" \ -d "payment_method_options[klarna][currency]=usd" \ -d "payment_method_options[klarna][subscriptions][0][reference]=EXAMPLE_REFERENCE" \ -d "payment_method_options[klarna][subscriptions][0][interval]=annual" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][amount]=20000" \ -d "payment_method_options[klarna][subscriptions][0][next_billing][date]=2026-01-01" ``` 生成された SetupIntent では、Klarna へのリダイレクトを完了する必要があります。[リダイレクトの処理](https://docs.stripe.com/payments/klarna/set-up-future-payments.md#react-native-redirect-customer) 方法をご覧ください。 ## Optional: 保存された Klarna アカウントを削除する [サーバー側] [detach](https://docs.stripe.com/api/payment_methods/detach.md) API を使用して、顧客が決済手段として保存した Klarna アカウントを削除できます。Klarna アカウントは、顧客がサブスクリプションをキャンセルした場合など、二度と使用されないことがわかっている場合に削除します。 ```curl curl -X POST https://api.stripe.com/v1/payment_methods/{{PAYMENT_METHOD_ID}}/detach \ -u "<>:" ```