# Klarna による支払い方法を受け付ける

世界で普及している後払いの支払い方法である Klarna を受け付ける方法をご紹介します。

[Klarna](https://docs.stripe.com/payments/klarna.md) では、顧客は決済時に Klarna アカウントにリダイレクトされます。Stripe をまだ導入していない場合は、Stripe Checkout を使用して[Stripe がオンラインで提供する決済ページを作成](https://docs.stripe.com/checkout/quickstart.md)し、ダッシュボードで[決済手段を設定](https://docs.stripe.com/payments/dashboard-payment-methods.md)することをお勧めします。

[Checkout Sessions API と Elements](https://docs.stripe.com/payments/quickstart.md) を使用して、ウェブサイトにカスタマイズされた決済画面を構築したり、モバイル連携を構築してウェブビューで決済を承認したりすることもできます。

> 導入を開始する前に、[決済手段設定](https://dashboard.stripe.com/settings/payment_methods)に移動して、アカウントがKlarnaの対象であることを確認してください。

# ダイレクト API


## Klarna を有効化

ダッシュボードで [Klarna を有効化](https://dashboard.stripe.com/settings/payment_methods)します。Stripe は、[動的な決済手段](https://docs.stripe.com/payments/payment-methods/dynamic-payment-methods.md)を使用して、対象となる顧客に Klarna を自動的に表示します。

### PaymentIntent を作成する

決済成功率を最大化し、不審請求の申請を減らすには、Klarna 決済の[PaymentIntent を作成](https://docs.stripe.com/api/payment_intents/create.md)する際に次のパラメーターを渡します。

- [shipping](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-shipping): `name`、`address.line1`、`city`、`country`、`postal_code` の各フィールドが定義され、空でないことを確認してください。
- [amount_details.line_items](https://docs.stripe.com/payments/payment-line-items.md)
- [payment_method_data.billing_details](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-payment_method_data-billing_details): `name`、`address.line1`、`city`、`country`、`postal_code` の各フィールドが定義され、空でないことを確認してください。

> [setup_future_usage](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-setup_future_usage) パラメーターを設定すると、その決済は 1 回限りの決済とは見なされません。これにより、利用[可能な決済オプション](https://docs.stripe.com/payments/klarna.md#payment-options)に影響が及び、[Payment Method Messaging Element](https://docs.stripe.com/elements/payment-method-messaging.md) と [Payment Element](https://docs.stripe.com/payments/payment-element.md) に顧客が利用できる決済プランが表示されなくなります。

```curl
curl https://api.stripe.com/v1/payment_intents \
  -u "<<YOUR_SECRET_KEY>>:" \
  -d "automatic_payment_methods[enabled]=true" \
  -d amount=1099 \
  -d currency=eur \
  -d "amount_details[line_items][0][product_name]=Your product name" \
  -d "amount_details[line_items][0][unit_cost]=1099" \
  -d "amount_details[line_items][0][quantity]=1" \
  -d "shipping[address][city]=Brothers" \
  -d "shipping[address][country]=US" \
  -d "shipping[address][line1]=27 Fredrick Ave" \
  -d "shipping[address][postal_code]=97712" \
  -d "shipping[address][state]=OR" \
  -d "payment_method_data[billing_details][address][city]=Brothers" \
  -d "payment_method_data[billing_details][address][country]=US" \
  -d "payment_method_data[billing_details][address][line1]=27 Fredrick Ave" \
  -d "payment_method_data[billing_details][address][postal_code]=97712" \
  -d "payment_method_data[billing_details][address][state]=OR" \
  --data-urlencode "payment_method_data[billing_details][email]=jenny.rosen@example.com" \
  -d "payment_method_data[billing_details][name]=Jenny Rosen"
```

### client secret を取得する

PaymentIntent には、クライアント側が支払いプロセスをセキュアに完了するために使用する *Client Secret* (The client secret is a unique key returned from Stripe as part of a PaymentIntent. This key lets the client access important fields from the PaymentIntent (status, amount, currency) while hiding sensitive ones (metadata, customer)) が含まれています。クライアント側に Client Secret を渡すには、さまざまな方法を使用できます。

#### 1 ページのアプリケーション

ブラウザーの `fetch` 関数を使用して、サーバーのエンドポイントから Client Secret を取得します。この方法は、クライアント側が 1 ページのアプリで、特に React などの最新のフロントエンドフレームワークで構築されている場合に最適です。Client Secret を処理するサーバーのエンドポイントを作成します。

#### Ruby

```ruby
get '/secret' do
  intent = # ... Create or retrieve the PaymentIntent
  {client_secret: intent.client_secret}.to_json
end
```

その後、クライアント側で JavaScript を使用して client secret を取得します。

```javascript
(async () => {
  const response = await fetch('/secret');
  const {client_secret: clientSecret} = await response.json();
  // Render the form using the clientSecret
})();
```

#### サーバ側のレンダリング

サーバーからクライアントに client secret を渡します。この方法は、アプリケーションがブラウザーへの送信前に静的なコンテンツをサーバーで生成する場合に最適です。

決済フォームに [client_secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) を追加します。サーバー側のコードで、PaymentIntent から client secret を取得します。

#### Ruby

```erb
<form id="payment-form" data-secret="<%= @intent.client_secret %>">
  <button id="submit">Submit</button>
</form>
```

```ruby
get '/checkout' do
  @intent = # ... Fetch or create the PaymentIntent
  erb :checkout
end
```

## Stripe に決済をする

このステップでは、[Stripe.js](https://docs.stripe.com/payments/elements.md) を使用してクライアントで Klarna の支払いを完了します。

### Stripe.js を設定する

顧客が Klarna での支払いをクリックするときは、Stripe.js を使用してその支払いを Stripe に送信することをお勧めします。Stripe.js は、支払いフローを構築するための Stripe の基本的な JavaScript ライブラリです。これにより、以下で説明するリダイレクトなどの複雑な処理が自動的に行われ、今後、組み込みを他の支払い方法にも簡単に拡張できます。Stripe.js スクリプトを HTML ファイルのヘッダーに追加することで、決済ページにこのスクリプトを含めます。

```html
<head>
  <title>Checkout</title>
  <script src="https://js.stripe.com/endive/stripe.js"></script>
</head>
```

決済ページで以下の JavaScript を使用して、Stripe.js のインスタンスを作成します。

```javascript
// Set your publishable key: remember to change this to your live publishable key in production
// See your keys here: https://dashboard.stripe.com/apikeys
var stripe = Stripe('<<YOUR_PUBLISHABLE_KEY>>');
```

PaymentIntent オブジェクト全体をクライアントに送信するのではなく、その [クライアントシークレット](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret) を使用します。これは、Stripe API リクエストを認証する API キーとは異なります。

クライアントシークレットは決済を確定できるため、慎重に取り扱う必要があります。記録したり、URL に埋め込んだり、当該の顧客以外に漏洩することがないようにしてください。

[stripe.confirmKlarnaPayment](https://docs.stripe.com/js/payment_intents/confirm_klarna_payment) を使用し、お客様のページからのリダイレクトを処理して決済を完了します。この機能に [return_url](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-return_url) を追加して、ユーザーが Klarna のウェブサイトまたはモバイルアプリで決済を完了した後に Stripe がユーザーをリダイレクトする場所を示します。

Klarna の決済ページで、顧客は自身の市場で提供されている決済オプションから選択します。[概要ページ](https://docs.stripe.com/payments/klarna.md#payment-options)の表で市場別の提供状況を確認してください。Klarna 決済ページのオプションを制限したり、事前選択したりすることはできません。この選択を消費者に任せることで、取引の機会が最大化されます。

```javascript
// Redirects away from the client
const {error} = await stripe.confirmKlarnaPayment(
  '{{PAYMENT_INTENT_CLIENT_SECRET}}',{
    return_url: 'https://example.com/checkout/complete',
  }
);
if (error) {
  // Inform the customer that there was an error.
}
```

顧客が支払いを送信すると、Stripe は顧客を `return_url` にリダイレクトし、以下の URL クエリーパラメーターを含めます。返品ページでは、これらを使用して PaymentIntent のステータスを取得し、顧客に支払いステータスを表示できます。

`return_url` を指定する際に、返品ページで使用する独自のクエリパラメーターを追加することもできます。

| パラメーター                         | 説明                                                                                                                                                                                                                                                                                                           |
| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `payment_intent`               | `PaymentIntent` の一意の識別子。                                                                                                                                                                                                                                                                                     |
| `payment_intent_client_secret` | `PaymentIntent` オブジェクトの [client secret](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-client_secret)。サブスクリプションの実装では、この client_secret は [confirmation_secret](https://docs.stripe.com/api/invoices/object.md#invoice_object-confirmation_secret) を通じて `Invoice` オブジェクトにも公開されます。 |

顧客が自社のサイトにリダイレクトされたら、`payment_intent_client_secret` を使用して PaymentIntent をクエリし、顧客に取引ステータスを表示できます。

顧客が選択した Klarna 決済オプションの詳細は、`charge` 時に [payment_method_details](https://docs.stripe.com/api/charges/object.md#charge_object-payment_method_details-klarna) プロパティで確認できます。使用可能な値は `pay_later`、`pay_with_financing`、`pay_now`、`pay_in_installments` の 4 種類です。これらのオプションの詳細については、Stripe の [Klarna 概要ページ](https://docs.stripe.com/payments/klarna.md#payment-options)をご覧ください。

Klarna の決済ページ各地域への適応に使用されるロケールは、[payment_method_details](https://docs.stripe.com/api/charges/object.md#charge_object-payment_method_details-klarna) プロパティでも確認できます。

```json
{
  "charges": {
    "data": [
      {
        "payment_method_details": {
          "klarna": {
            "payment_method_category": "pay_in_installments",
            "preferred_locale": "en-US"
          },
          "type": "klarna"
        },
        "id": "src_16xhynE8WzK49JbAs9M21jaR",
        "object": "source",
        "amount": 1099,
        "client_secret": "src_client_secret_UfwvW2WHpZ0s3QEn9g5x7waU",
        "created": 1445277809,
        "currency": "eur",
        "livemode": true,
        "statement_descriptor": null,
        "status": "succeeded",
        "type": "klarna",
        "usage": "single_use"
      }
    ],
    "object": "list",
    "has_more": false,
    "url": "/v1/charges?payment_intent=pi_1G1sgdKi6xqXeNtkldRRE6HT"
  },
  "payment_method_options": {
    "klarna": {}
  },
  "payment_method_types": [
    "klarna"
  ],
  "id": "pi_1G1sgdKi6xqXeNtkldRRE6HT",
  "object": "payment_intent",
  "amount": 1099,
  "client_secret": "pi_1G1sgdKi6xqXeNtkldRRE6HT_secret_h9B56ObhTN72fQiBAuzcVPb2E",
  "confirmation_method": "automatic",
  "created": 1579259303,
  "currency": "eur",
  "livemode": true,
  "next_action": null
}
```

## 実装内容をテストする

> 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             |

実働環境のテストのために、現地通貨の金額 `3500` を使用して、融資以外のすべての Klarna 支払いオプションをテストできます。たとえば、イタリアでの「3 回払い」をテストする場合は、35.00 EUR の取引を使用できます。

### 二段階認証

6 桁の数字であれば、2 段階認証コードとして有効です。`999999` を使用すると、認証は失敗します。

### 返済方法

Klarna のフロー内では、以下のテスト値を使用し、さまざまな返済方法を試すことができます。

| タイプ      | 値                                                        |
| -------- | -------------------------------------------------------- |
| 口座引き落とし  | DE11520513735120710131                                   |
| 銀行振込     | デモの銀行                                                    |
| クレジットカード | - 番号: 4111 1111 1111 1111
  - CVV: 123
  - 有効期限: 任意の将来日付 |
| デビットカード  | - 番号: 4687 3888 8888 8881
  - CVV: 123
  - 有効期限: 任意の将来日付 |

> #### アメリカで資金調達をテストする
> 
> アメリカでは、テスト用のデビットカードを使用して資金調達をテストします。Klarna で社会保障番号 (SSN) を求められた場合は、`123456789` を使用します。

## 失敗した決済

Klarna は、取引を受け付けるか拒否するかを決定する際に複数の要因を考慮します (顧客の Klarna の利用期間、顧客が返済する必要のある未払い額、今回の注文の金額など)。

顧客が後払いを選択すると、Klarna は取引を受け付ける前にリスク評価を行います。Klarna は、リスク評価の結果が良好でなかったり、取引金額が大きかったり、顧客に多額の未払いの負債があったりすると、取引を拒否することがあります。そのため、決済フローに `card` などの他の支払いオプションを提示することをお勧めします。このような場合、[PaymentMethod](https://docs.stripe.com/api/payment_methods/object.md) は解除され、[PaymentInten](https://docs.stripe.com/api/payment_intents/object.md)オブジェクトのステータスは自動的に `requires_payment_method` に変わります。

顧客は Klarna のサイトにリダイレクトされてから 3 時間以内に支払いを完了する必要があります。3 時間経過しても操作が行われない場合、[PaymentMethod](https://docs.stripe.com/api/payment_methods/object.md) の関連付けは解除され、[PaymentIntent](https://docs.stripe.com/api/payment_intents/object.md) オブジェクトのステータスは自動的に `requires_action` から `requires_payment_method` に移行します。

このようなケースでは、決済フローに表示される別の支払いオプションで再試行するように顧客に通知します。

### Klarna のレート制限

Klarna への API リクエストには、Stripe の [API 全体のレート制限](https://docs.stripe.com/rate-limits.md) を超える追加のレート制限が適用されます。これらの制限は、行う API リクエストの形状によって異なる場合があります。一般に、毎秒 50 を超えるリクエストを行うと、HTTP ステータスコード `400` または `402` の応答の形式でレート制限が表示されることがあります。使用量がこれらのレベルに達するのではないかとご心配な場合は、Klarna がケースバイケースでこれらの制限を増やすことができる可能性がありますので、Stripe まで[お問い合わせください](https://support.stripe.com)。

### エラーメッセージ

Klarna 支払いが失敗すると、通常、以下のエラーコードのいずれかが返されます。これらのコードは、[last_payment_error](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-last_payment_error) API オブジェクトに表示されます。

> `2023-08-16` API バージョンより前は、すべての Klarna エラーが [payment_intent_authentication_failure](https://docs.stripe.com/error-codes.md#payment_intent_authentication_failure) として報告されていました。API バージョンが最新であることを確認して、以下のエラー詳細をご覧ください。

| エラーコード                                                                                                                  | 説明                                             |
| ----------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------- |
| [payment_method_customer_decline](https://docs.stripe.com/error-codes.md#payment-method-customer-decline)               | 顧客が Klarna のページで決済をキャンセルしました                   |
| [payment_method_provider_decline](https://docs.stripe.com/error-codes.md#payment-method-provider-decline)               | Klarna が顧客の支払いを拒否しました                          |
| [payment_intent_payment_attempt_expired](https://docs.stripe.com/error-codes.md#payment-intent-payment-attempt-expired) | 顧客が Klarna のページで購入を完了しないまま、支払いセッションの有効期限が切れました |
| [payment_method_not_available](https://docs.stripe.com/error-codes.md#payment-method-not-available)                     | Klarna を使用しようとしたときに予期しないエラーが発生しました             |

## オプションのカスタマイズ

オプションで、Klarna 決済フロー(売上の承認とキャプチャーの分離など)と顧客とのやり取り(リダイレクトの処理など)に複数の異なるカスタマイズを実装できます。

### 承認とキャプチャーの分離

Klarna は[オーソリとキャプチャの分離](https://docs.stripe.com/payments/place-a-hold-on-a-payment-method.md)に対応しています。支払いから顧客への商品の配送までに遅延が生じた場合、まず支払いをオーソリし、後でキャプチャーします。キャプチャーされた時点で、Klarna は顧客に明細書を送り、必要となる以降の支払いの期日を作成します。**オーソリされた Klarna の支払いは、オーソリから 28 日以内にキャプチャーする必要があります** 。キャンセルされない場合、オーソリは自動的にキャンセルされ、支払いをキャプチャーできなくなります。

1. オーソリのみ行うように Stripe に指示する

   オーソリとキャプチャーを分離することを指定するには、PaymentIntent の作成時に、[capture_method](https://docs.stripe.com/api/payment_intents/create.md#create_payment_intent-capture_method) を `manual` に設定します。このパラメーターは、顧客の Klarna アカウントの金額のみをオーソリするよう Stripe に指示します。

   ```curl
   curl https://api.stripe.com/v1/payment_intents \
     -u "<<YOUR_SECRET_KEY>>:" \
     -d amount=1099 \
     -d currency=eur \
     -d "automatic_payment_methods[enabled]=true" \
     -d capture_method=manual
   ```

2. 売上をキャプチャーする

   承認が完了すると、PaymentIntent の [ステータス](https://docs.stripe.com/api/payment_intents/object.md#payment_intent_object-status)は `requires_capture` に移行します。承認済みの売上をキャプチャーするには、PaymentIntent の [キャプチャー](https://docs.stripe.com/api/payment_intents/capture.md)リクエストを作成します。デフォルトでは承認済みの総額がキャプチャーされます。これを超える金額をキャプチャーすることはできませんが、これより少ない金額をキャプチャーすることは可能です。

   ```curl
   curl https://api.stripe.com/v1/payment_intents/{{PAYMENTINTENT_ID}}/capture \
     -u "<<YOUR_SECRET_KEY>>:" \
     -d amount_to_capture=750
   ```

3. (任意)オーソリをキャンセルする

   Klarna は、Klarna 内での顧客の総計購買力に対して、承認された決済件数を計算します。このため、フルフィルメントを行えない承認済みの決済 (商品を配送できないなど) は、それが明らかになり次第、早急にキャンセルする必要があります。

   ```curl
   curl https://api.stripe.com/v1/payment_intents/{{PAYMENTINTENT_ID}}/cancel \
     -u "<<YOUR_SECRET_KEY>>:" \
     -d cancellation_reason=abandoned
   ```

### Klarna リダイレクトを手動で処理する

クライアント側で `confirmKlarnaPayment` を使用して Klarna のリダイレクトおよび支払いを処理するには、Stripe.js を使用することをお勧めします。Stripe.js を使用すると、他の決済手段にも対応するように実装を拡張できます。ただし、以下の手順に従って、お客様のサーバーに顧客を手動でリダイレクトすることもできます。

- [PaymentIntent](https://docs.stripe.com/api/payment_intents/object.md) を作成して確認します。`return_url` フィールドに、顧客向けの決済後のリダイレクト URL を指定する必要があります。この URL には独自のクエリパラメータを指定でき、リダイレクトフローの最終 URL にはそれらが含まれます。

  ```curl
  curl https://api.stripe.com/v1/payment_intents \
    -u "<<YOUR_SECRET_KEY>>:" \
    -d "automatic_payment_methods[enabled]=true" \
    -d amount=1099 \
    -d currency=eur \
    -d confirm=true \
    --data-urlencode "return_url=https://example.com/checkout/complete" \
    -d "payment_method_data[type]=klarna"
  ```

- 作成される `PaymentIntent` のステータスは `requires_action` であり、`next_action` のタイプは `redirect_to_url` です。

  #### JSON

  ```json
  {
    "status": "requires_action",
    "next_action": {
      "type": "redirect_to_url",
      "redirect_to_url": {
        "url": "https://hooks.stripe.com/...",
        "return_url": "https://example.com/checkout/complete"
      }
    },
    "id": "pi_1G1sgdKi6xqXeNtkldRRE6HT",
    "object": "payment_intent",
    "amount": 1099,
    "client_secret": "pi_1G1sgdKi6xqXeNtkldRRE6HT_secret_h9B56ObhTN72fQiBAuzcVPb2E",
    "confirmation_method": "automatic",
    "created": 1579259303,
    "currency": "eur",
    "livemode": true,
    "charges": {
      "data": [],
      "object": "list",
      "has_more": false,
      "url": "/v1/charges?payment_intent=pi_1G1sgdKi6xqXeNtkldRRE6HT"
    },
    "payment_method_options": {
      "klarna": {}
    },
    "payment_method_types": [
      "klarna"
    ]
  }
  ```

- `next_action.redirect_to_url.url` プロパティで指定した URL に顧客をリダイレクトします。ここに示すコード例はおおまかなものであり、リダイレクト方法は、ご使用のウェブフレームワークによって異なる場合があります。

  #### Ruby

  ```ruby
  if payment_intent.status == 'requires_action' && payment_intent.next_action.type == 'redirect_to_url'
    url = payment_intent.next_action.redirect_to_url.url
    redirect(url)
  end
  ```

決済プロセスを完了した顧客は、ステップ 1 で設定した `return_url` に送られます。`payment_intent` と `payment_intent_client_secret` の URL クエリパラメーターが含まれています。`return_url` にクエリパラメーターがすでに含まれていた場合は、それらも保持されます。

支払いのステータスを確認するには、[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) イベントを送信します。ダッシュボード、カスタム *Webhook* (A configurable endpoint that executes when it receives certain event notifications from an event destination)、またはパートナーソリューションを使用してこれらのイベントを受信し、また、顧客への注文確認メールの送信、データベースでの売上の記録、配送ワークフローの開始などのアクションを実行します。

クライアントからのコールバックを待つのではなく、これらのイベントをリッスンします。クライアント側では、コールバックが実行される前に顧客がブラウザーのウィンドウを閉じたり、アプリを終了したりする可能性があります。また、悪意を持つクライアントがレスポンスを不正操作する恐れもあります。非同期型のイベントをリッスンするよう構築済みのシステムを設定することで、これ以降はより多くの決済手段を簡単に受け付けられるようになります。[サポートされているすべての決済手段の違い](https://stripe.com/payments/payment-methods-guide)をご確認ください。

- **ダッシュボードでイベントを手動で処理する**

  ダッシュボードでは、[支払いの表示](https://dashboard.stripe.com/payments)、メールの領収書の送信、入金の処理、失敗した支払いの再試行を行えます。

- **Custom Webhook を構築する**

  [カスタム Webhook](https://docs.stripe.com/events/set-up-events.md#create-event-handler) ハンドラーを構築してイベントをリッスンし、カスタムの非同期決済フローを構築できます。Stripe CLI を使用すると、ローカルで Webhook 連携をテストしてデバッグできます。

- **構築済みアプリを導入する**

  パートナーアプリケーションを統合することで、[自動化](https://stripe.partners/?f_category=automation)や[マーケティング/セールス](https://stripe.partners/?f_category=marketing-and-sales)などの一般的なビジネスイベントを処理します。

### Klarna 決済ページをカスタマイズする

- **Klarna フォームを事前入力する**

  顧客が延べ払いオプションを使用して Klarna に決済することを選択すると (後払い、分割払い、融資など)、Klarna は、リスク評価と承認のために十分な情報を収集します。情報の種類は、顧客の国によって異なります。ほとんどのヨーロッパの国では、完全な請求先住所と生年月日です。この情報は API を通じて渡すことができ、顧客がページに到着する前にフォームが事前入力されます。

  ```curl
  curl https://api.stripe.com/v1/payment_intents \
    -u "<<YOUR_SECRET_KEY>>:" \
    -d "automatic_payment_methods[enabled]=true" \
    -d amount=1099 \
    -d currency=eur \
    -d confirm=true \
    --data-urlencode "return_url=https://example.com/checkout/complete" \
    -d "payment_method_data[type]=klarna" \
    --data-urlencode "payment_method_data[billing_details][email]=customer@example.com" \
    -d "payment_method_data[billing_details][name]=Jenny Rosen" \
    --data-urlencode "payment_method_data[billing_details][phone]=+4915142321555" \
    -d "payment_method_data[billing_details][address][line1]=Alexanderplatz 1" \
    -d "payment_method_data[billing_details][address][city]=Berlin" \
    -d "payment_method_data[billing_details][address][postal_code]=10551" \
    -d "payment_method_data[billing_details][address][country]=DE" \
    -d "payment_method_data[klarna][dob][day]=10" \
    -d "payment_method_data[klarna][dob][month]=10" \
    -d "payment_method_data[klarna][dob][year]=1990"
  ```
![API からの請求詳細が事前入力され、英語で表示されるようにカスタマイズされた Klarna の支払いページのスクリーンショット](https://b.stripecdn.com/docs-statics-srv/assets/klarna-kpp-prefilled-customized.75932cc9aa3223960aa2fcf769c1ce62.png)
  
  Klarna の支払いページは、API からの請求書詳細が事前入力され、カスタマイズされてドイツの顧客に英語で表示されます。

### PaymentIntent に項目を追加する

> #### Klarna との統合項目
> 
> Klarna を導入する際に承認率を最適化するには、`line_items` データを含めて買い物客のカートの中身を表します。先行提供版については、[支払い項目](https://docs.stripe.com/payments/payment-line-items.md)をご覧ください。

### ウェブサイトに決済手段のメッセージを表示する

[Payment Method Messaging Element](https://docs.stripe.com/js/elements_object/create_element?type=paymentMethodMessaging) は、顧客が購入時に商品、カート、支払いの各ページから、利用できる後払いの決済オプションを直接確認できるようにする、埋め込み可能な UI コンポーネントです。

ウェブサイトに Payment Method Messaging Element を追加するには、[決済手段のメッセージを表示する](https://docs.stripe.com/elements/payment-method-messaging.md)をご覧ください。

