# Klarna によるサブスクリプションを設定する Klarna を使用したサブスクリプションの作成と請求の方法をご紹介します。 このガイドを使用して、支払い方法として [Klarna](https://docs.stripe.com/payments/klarna.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決済オプションはユースケースと買い手の国によって異なります > > 導入を開始する前に、顧客が利用できる[決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options) を確認してください。 [Stripe Checkout](https://docs.stripe.com/payments/checkout.md)を使用して、Klarna を支払い方法として保存することをお勧めします。 # Stripe がオンラインで提供するページ > This is a Stripe がオンラインで提供するページ for when api-integration is checkout. View the full page at https://docs.stripe.com/billing/subscriptions/klarna?api-integration=checkout. まず、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' ``` ## 商品と価格を作成する [ダッシュボード] [Products (商品)](https://docs.stripe.com/api/products.md) は、販売しているアイテムまたはサービスを表します。[Prices (価格)](https://docs.stripe.com/api/prices.md) は、商品の価格と請求頻度を定義します。これには、商品の価格、受け付ける通貨、および 1 回限りの支払いか継続支払いかが含まれます。商品と価格が数個のみの場合は、ダッシュボードでそれらを作成および管理します。 このガイドでは、例としてストックフォトサービスを使用し、15 USD の月次サブスクリプションを顧客に請求します。これをモデル化するには、次のようにします。 1. [商品](https://dashboard.stripe.com/products?active=true)ページに移動し、**商品を作成**をクリックします。 1. 商品の**名前**を入力します。オプションで**説明**を追加して、商品の画像をアップロードできます。 1. **商品税コード**を選択します。[商品税コード](https://docs.stripe.com/tax/tax-codes.md)の詳細をご確認ください。 1. **継続**を選択します。次に、価格に**15**を入力し、通貨として**\**を選択します。 1. **価格に税金を含める**かどうかを選択します。[税金設定](https://dashboard.stripe.com/test/settings/tax)のデフォルト値を使用するか、値を手動で設定できます。この例では、**自動**を選択します。 1. **請求期間**で**月次**を選択します。 1. **その他の料金体系オプション**をクリックします。次に、この例の料金体系モデルとして**定額**を選択します。[定額料金](https://docs.stripe.com/products-prices/pricing-models.md#flat-rate)とその他の[料金体系モデル](https://docs.stripe.com/products-prices/pricing-models.md)の詳細をご確認ください。 1. 将来的に特定の価格を整理、クエリ、更新するために、内部**価格の説明**と[検索キー](https://docs.stripe.com/products-prices/manage-prices.md#lookup-keys) 追加します。 1. **次へ**をクリックします。次に、**商品を追加**をクリックします。 商品と価格を作成したら、価格 ID を記録しておき、後続のステップで使用できるようにします。ID は料金体系ページで `price_G0FvDp6vZvdwRZ` のように表示されます。 ## Checkout セッションを作成する [クライアント側] [サーバー側] サーバー側のエンドポイントを呼び出して Checkout セッションを作成する購入ボタンをウェブサイトに追加します。 ```html Checkout
``` 既存の [Price](https://docs.stripe.com/api/prices.md) の ID を使用して Checkout セッションを作成します。モードを `subscription` に設定し、1 つ以上の継続価格を渡します。継続価格に加えて、1 回限りの価格を追加できます。Checkout セッションを作成したら、レスポンスで返された [URL](https://docs.stripe.com/api/checkout/sessions/object.md#checkout_session_object-url) に顧客をリダイレクトします。 ```curl curl https://api.stripe.com/v1/checkout/sessions \ -u "<>:" \ -d "line_items[0][price]={{PRICE_ID}}" \ -d "line_items[0][quantity]=1" \ -d mode=subscription \ --data-urlencode "success_url=https://example.com/success?session_id={CHECKOUT_SESSION_ID}" ``` 顧客が支払いを正常に完了すると、Stripe は顧客を `success_url` にリダイレクトします。これはお客様のウェブサイト上にあり、支払いの成功を顧客に知らせるページです。上記の例のように `success_url` に `{CHECKOUT_SESSION_ID}` テンプレート変数を含めて、成功ページでセッション ID を使用できるようにします。 Checkout セッションは作成後 24 時間で期限が切れます。 > 決済開始の検出にあたっては、`success_url` へのリダイレクトのみに依存しないでください。その理由は次のとおりです。 > > - 悪意を持つユーザが、支払いをせずに `success_url` に直接アクセスし、商品やサービスにアクセスできるようになる可能性があります。 - 決済が成功した後で、顧客が `success_url` にリダイレクトされる前にブラウザタブを閉じる可能性あります。 ### トライアルを設定する [subscription_data](https://docs.stripe.com/api/checkout/sessions/create.md#create_checkout_session-subscription_data) パラメーターを使用して、期間、終了日、その他のトライアル設定に関する情報を指定し、Checkout セッションで無料トライアルを作成できます。 [決済手段を指定せずに無料トライアルが終了したときにキャンセルまたは一時停止するように設定する方法](https://docs.stripe.com/billing/subscriptions/trials/free-trials.md?how=checkout#create-free-trials-without-payment)をご覧ください。 ## サブスクリプションを取得する [サーバー側] 顧客が支払いの詳細を送信すると、Stripe は自動的にサブスクリプションを作成します。サブスクリプションは、`success_url` を使用して同期的に取得することも、*Webhook* を使用して非同期で取得することもできます。 顧客が支払いの成功後に必ず `success_url` に到達するとは限らないため (たとえば、リダイレクトが行われる前に顧客がブラウザータブを閉じることもあります)、サブスクリプションを同期的に取得するか非同期的に取得するかは、ドロップオフの許容度によって異なります。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_a1h2mO4eLbjemY0JWW9rCz5dcglwr3M5ldjLOvpGxWD37i1Oi5SeFhSup1", "object": "checkout.session", "billing_address_collection": null, "client_reference_id": null, "customer": null, "customer_email": null, "display_items": [], "mode": "setup","subscription": "sub_1Op9VFCvDOElLqwO6fs7Na4P", "submit_type": null, "success_url": "https://example.com/success" } }, "livemode": false, "pending_webhooks": 1, "request": { "id": null, "idempotency_key": null }, "type": "checkout.session.completed" } ``` `subscription` キーの値の記録を残しておいてください。これは、Checkout セッションから作成された、[Subscription (サブスクリプション)](https://docs.stripe.com/api/subscriptions.md) の ID です。 #### 成功時の URL ユーザーがリダイレクトによってサイトに戻ったら、URL から `session_id` を取得し、Session オブジェクトを [Retrieve (取得)](https://docs.stripe.com/api/checkout/sessions/retrieve.md) します。 ```curl curl -G https://api.stripe.com/v1/checkout/sessions/{{SESSION_ID}} \ -u "<>:" \ -d "expand[]=subscription" ``` > URL から `session_id` を確実に使用できるようにするには、Checkout セッションを作成する際に `success_url` に `session_id={CHECKOUT_SESSION_ID}` テンプレート変数を含めます。 レスポンスには、作成された [Subscription (サブスクリプション)](https://docs.stripe.com/api/subscriptions.md) が [subscription (サブスクリプション)](https://docs.stripe.com/api/checkout/sessions/object.md#checkout_session_object-subscription) キーの下に含まれます。 ## 実装内容をテストする > 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 - 有効期限: 任意の将来日付 | # 高度な連携機能 > This is a 高度な連携機能 for when api-integration is elements. View the full page at https://docs.stripe.com/billing/subscriptions/klarna?api-integration=elements. [Payment Element](https://docs.stripe.com/payments/payment-element.md)を使用してカスタマイズされたPayments UIを作成する場合は、[Subscriptions API](https://docs.stripe.com/api/subscriptions.md)使用してサブスクリプションを作成します。 まず、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' ``` ## 商品と価格を作成する [ダッシュボード] [Products (商品)](https://docs.stripe.com/api/products.md) は、販売しているアイテムまたはサービスを表します。[Prices (価格)](https://docs.stripe.com/api/prices.md) は、商品の価格と請求頻度を定義します。これには、商品の価格、受け付ける通貨、および 1 回限りの支払いか継続支払いかが含まれます。商品と価格が数個のみの場合は、ダッシュボードでそれらを作成および管理します。 このガイドでは、例としてストックフォトサービスを使用し、15 USD の月次サブスクリプションを顧客に請求します。これをモデル化するには、次のようにします。 1. [商品](https://dashboard.stripe.com/products?active=true)ページに移動し、**商品を作成**をクリックします。 1. 商品の**名前**を入力します。オプションで**説明**を追加して、商品の画像をアップロードできます。 1. **商品税コード**を選択します。[商品税コード](https://docs.stripe.com/tax/tax-codes.md)の詳細をご確認ください。 1. **継続**を選択します。次に、価格に**15**を入力し、通貨として**\**を選択します。 1. **価格に税金を含める**かどうかを選択します。[税金設定](https://dashboard.stripe.com/test/settings/tax)のデフォルト値を使用するか、値を手動で設定できます。この例では、**自動**を選択します。 1. **請求期間**で**月次**を選択します。 1. **その他の料金体系オプション**をクリックします。次に、この例の料金体系モデルとして**定額**を選択します。[定額料金](https://docs.stripe.com/products-prices/pricing-models.md#flat-rate)とその他の[料金体系モデル](https://docs.stripe.com/products-prices/pricing-models.md)の詳細をご確認ください。 1. 将来的に特定の価格を整理、クエリ、更新するために、内部**価格の説明**と[検索キー](https://docs.stripe.com/products-prices/manage-prices.md#lookup-keys) 追加します。 1. **次へ**をクリックします。次に、**商品を追加**をクリックします。 商品と価格を作成したら、価格 ID を記録しておき、後続のステップで使用できるようにします。ID は料金体系ページで `price_G0FvDp6vZvdwRZ` のように表示されます。 ## サブスクリプションを作成する [サーバー側] [payment_behavior](https://docs.stripe.com/api/subscriptions/create.md#create_subscription-payment_behavior) パラメーターに `default_incomplete` の値を指定して、ステータスが `incomplete` の価格と顧客の [Subscription (サブスクリプション)](https://docs.stripe.com/api/subscriptions.md) を作成します。サブスクリプションが有効になったときに支払い方法を保存するには、`payment_settings.save_default_payment_method=on_subscription` パラメーターを設定します。 ```curl curl https://api.stripe.com/v1/subscriptions \ -u "<>:" \ -d customer={{CUSTOMER_ID}} \ -d payment_behavior=default_incomplete \ -d "items[0][price]={{PRICE_ID}}" \ -d "payment_settings[save_default_payment_method]=on_subscription" \ -d "expand[0]=latest_invoice.payments" \ -d "expand[1]=latest_invoice.confirmation_secret" ``` レスポンスには、*サブスクリプション* (A Subscription represents the product details associated with the plan that your customer subscribes to. Allows you to charge the customer on a recurring basis)の最初の[請求書)](https://docs.stripe.com/api/invoices.md)が含まれます。これにはインボイスの支払いが含まれます。これには、Stripe がこのインボイスに対して生成したデフォルトの PaymentIntent と、PaymentIntent オブジェクト全体を渡す代わりにクライアント側で支払いプロセスを安全に完了するために使用できる Confirmation Secret が含まれます。支払いの確定に使用する必要がある PaymentIntent ID を `latest_invoice.payments` から取得します。`latest_invoice.confirmation_secret.client_secret` をフロントエンドに返して、支払いを完了します。 ### トライアルを設定する 無料トライアル付きのサブスクリプションを設定できます。[トライアル期間がある有効なサブスクリプションに対する支払いを延期](https://docs.stripe.com/billing/subscriptions/trials.md)する方法をご紹介します。 ## 支払い情報を収集する [クライアント側] ### Stripe Elements を設定する Payment Element は Stripe.js の機能として自動的に使用できるようになります。決済ページに Stripe.js スクリプトを含めるには、HTML ファイルの `head` にスクリプトを追加します。常に js.stripe.com から Stripe.js を直接読み込むことにより、PCI 準拠が維持されます。スクリプトをバンドルに含めたり、そのコピーを自身でホストしたりしないでください。 ```html Checkout ``` 購入ページで次の JavaScript を使用して、Stripe のインスタンスを作成します。 ```javascript // Set your publishable key: remember to change this to your live publishable key in production // See your keys here: https://dashboard.stripe.com/apikeys const stripe = Stripe('<>'); ``` ### Payment Element をページに追加する 決済ページには Payment Element を配置する場所が必要です。決済フォームで、一意の ID を持つ空の DOM ノード (コンテナー) を作成します。 ```html
``` 上記のフォームが読み込まれたら、Payment Element のインスタンスを作成し、コンテナの DOM ノードにマウントします。[サブスクリプションの作成](https://docs.stripe.com/billing/subscriptions/klarna.md#pi-create-subscription)ステップでは、`client_secret` の値をフロントエンドに渡しました。Elements のインスタンスを作成する際は、この値をオプションとして渡します。 ```javascript const options = { clientSecret: '{{CLIENT_SECRET}}' }; // Set up Stripe.js and Elements to use in checkout form, passing the client secret obtained in step 5 const elements = stripe.elements(options); // Create and mount the Payment Element const paymentElementOptions = { layout: 'accordion'}; const paymentElement = elements.create('payment', paymentElementOptions); paymentElement.mount('#payment-element'); ``` Payment Element によって動的なフォームが表示され、顧客はここで支払い方法を選択できます。このフォームでは、顧客が選択した支払い方法で必要な支払い詳細のすべてが自動的に収集されます。 ### 支払いを完了する `stripe.confirmPayment` を使用して、Payment Element からの詳細を指定した支払いを完了し、サブスクリプションを有効化します。これにより PaymentMethod が作成され、不完全なサブスクリプションの最初の PaymentIntent が確定され、その結果、支払いが実行されます。 Stripe は、最初の支払いと将来の支払いを完了するために、顧客を Klarna にリダイレクトします。[return_url](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-return_url) を指定して、Klarna での支払いの完了後にユーザーをリダイレクトする URL を指示します。 ```javascript const form = document.getElementById('payment-form'); form.addEventListener('submit', async (event) => { event.preventDefault(); const {error} = await stripe.confirmPayment({ //`Elements` instance that was used to create the Payment Element elements, confirmParams: { return_url: "https://example.com/order/123/complete", mandate_data: { customer_acceptance: { type: "online", online: { infer_from_client: true } } } } }); if (error) { // This point will only be reached if there is an immediate error when // confirming the payment. Show error to your customer (for example, payment // details incomplete) const messageContainer = document.querySelector('#error-message'); messageContainer.textContent = error.message; } }); ``` ユーザーがリダイレクトされた後、PaymentIntent をクエリしてそのステータスを確認します。ステータスが `succeeded` の場合、支払いは成功しており、サブスクリプションは有効になっています。 ## 実装内容をテストする > 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 - 有効期限: 任意の将来日付 |