Klarna

Start accepting payments using Klarna, a favorite payment method in Europe.

We offer Klarna's Pay later and Slice it payment methods, allowing your customers to either pay up to 30 days later via an invoice, or split the payment into three equal installments.

šŸ“˜

To start accepting Klarna payments, please contact your customer success manager.

Overview

To integrate Klarna into your checkout process and start accepting payments, follow these five steps:

  1. Create a session. When the customer reaches your checkout page, create a session with Klarna.
  2. Display widget. Initialize and load the Klarna widget to present your customer with the payment methods.
  3. Authorize. When the customer presses the buy/review order button on your page, make an authorization request.
  4. Place the order. After the order has been authorized, place the order by using the authorization token generated in step three.
  5. Capture the payment. Finally, once you've shipped the order or made it available to the customer, you can capture the payment.

Process a Klarna payment

Step 1: Create a session

First, you need to create a Klarna session for your customer.

You should provide all known order details at this stage to enable pre-assessment and to personalize the experience for your customer.

If, however, your checkout process is designed in a way that you don't have all the customer's details at this point, you can add them later on when you load the Klarna widget or when you come to authorize the payment.

When you create a session, you will receive the available payment_method_categories, a session_id and a client_token. You can use the session_id to update the session using our API, and the client_token should be passed to the browser. A session is valid for 48 hours.

Endpoints

Live

https://api.checkout.com/klarna/credit-sessions

Sandbox

https://api.sandbox.checkout.com/klarna-external/credit-sessions

Header parameters

HeaderValue
AuthorizationRequired

public key

Use the public key from your Checkout.com account. You can find this in the Hub.

Content-Type\ Requiredapplication/json

Body parameters

Field nameDescription
purchase_countryStringRequiredThe country in which the purchase is being made (two-letter ISO 3166 code).
currency\ String\ RequiredThe currency in which the payment is being made (three-letter ISO 4217 code).
locale\ String\ RequiredThe customer's locale (RFC 1766 code).
amount\ Integer\ RequiredThe total amount of the order, including tax and any discounts.
tax_amount\ Integer\ RequiredThe total tax amount of the order.
productsArray<object>RequiredThe order details.
This object is passed directly to Klarna as order_lines.1
🚧

purchase_country and locale will affect the Klarna widget's validation rules. For example, when your customer is prompted for their address, the widget's postal code field will validate against available post codes in the country specified in purchase_country.

Request example

{
  "purchase_country": "GB",
  "currency": "GBP",
  "locale": "en-GB",
  "amount": 1000,
  "tax_amount": 1,
  "products": [
    {
      "name": "Brown leather belt",
      "quantity": 1,
      "unit_price": 1000,
      "tax_rate": 0,
      "total_amount": 1000,
      "total_tax_amount": 0
    }
  ]
}

Response examples

{
  "session_id": "kcs_55xomnyd3ftujckeoyuvtkx2ue",
  "client_token": "eyJhbGciOiJub25lIn0.ewogICJzZXNzaW9uX2lkIiA6ICI1MGEzYTNiOS02NGE5LTYwNjMtODNmNC1hMzRlM2Q4MjRiNGIiLAogICJiYXNlX3VybCIgOiAiaHR0cHM6Ly9rbGFybmEtcGF5bWVudHMtZXUucGxheWdyb3VuZC5rbGFybmEuY29tIiwKICAiZGVzaWduIiA6ICJrbGFybmEiLAogICJsYW5ndWFnZSIgOiAiZW4iLAogICJwdXJjaGFzZV9jb3VudHJ5IiA6ICJHQiIsCiAgImFuYWx5dGljc19wcm9wZXJ0eV9pZCIgOiAiVUEtMzYwNTMxMzctMTEiLAogICJ0cmFjZV9mbG93IiA6IGZhbHNlLAogICJlbnZpcm9ubWVudCIgOiAicGxheWdyb3VuZCIsCiAgIm1lcmNoYW50X25hbWUiIDogIlBsYXlncm91bmQgRGVtbyBNZXJjaGFudCIsCiAgInNlc3Npb25fdHlwZSIgOiAiUEFZTUVOVFMiLAogICJjbGllbnRfZXZlbnRfYmFzZV91cmwiIDogImh0dHBzOi8vZXZ0LnBsYXlncm91bmQua2xhcm5hLmNvbSIKfQ.",
  "payment_method_categories": [
    {
      "asset_urls": {
        "descriptive": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/slice_it/descriptive/pink.svg",
        "standard": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/slice_it/standard/pink.svg"
      },
      "identifier": "pay_over_time",
      "name": "Slice it."
    },
    {
      "asset_urls": {
        "descriptive": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/pay_later/descriptive/pink.svg",
        "standard": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/pay_later/standard/pink.svg"
      },
      "identifier": "pay_later",
      "name": "Pay later."
    }
  ],
  "_links": {
    "self": {
      "href": "http://localhost:5100/credit-sessions/kcs_55xomnyd3ftujckeoyuvtkx2ue"
    }
  }
}
{
  "request_id": "0HLK56US9BH1E:0000005B",
  "error_type": "request_invalid",
  "error_codes": [
    "purchase_country_required"
  ]
}

Additional error responses:

  • 400 Bad request
  • 401 Unauthorized to access this resource

Step 2: Display widget

Next, load the widget on your page so your customer can choose one of the Klarna payment methods.

2.1: Add the JavaScript SDK to your page

To do so, you first need to insert the below code into your page to load the Klarna Payments Library in your client.

<script>
  window.klarnaAsyncCallback = function () {
 
    // This is where you start calling Klarna's JS SDK functions
    //
    // Klarna.Payments.init({ ... })
 
  };
</script>
<script src="https://x.klarnacdn.net/kp/lib/v1/api.js" async></script>

2.2: Initialize the SDK

Then initialize the Klarna Payments Library. To do this, call init and pass the client_token that you got in the response when you created the session in the first step.

try {
    Klarna.Payments.init(
        {
            client_token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIifQ.dtxWM6MIcgoeMgH87tGvsNDY6cH"
        }
    );
} catch (e) {
    // Handle error
}

2.3: Add a container to your page

Next, specify where you want the widget by adding a container to your checkout page with the following snippet.

<div id="klarna_container"></div>
🚧

The Klarna Payments iframe requires a minimum width of 280px.

2.4: Display available payment methods

To show your customer the Klarna payment options open to them (i.e. Pay later and Slice it), use the following payment_method_categories array, which you'll find in the response you received when you created the session in step one.

{
  // ...
  "payment_method_categories": [
    {
      "asset_urls": {
        "descriptive": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/slice_it/descriptive/pink.svg",
        "standard": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/slice_it/standard/pink.svg"
      },
      "identifier": "pay_over_time",
      "name": "Slice it."
    },
    {
      "asset_urls": {
        "descriptive": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/pay_later/descriptive/pink.svg",
        "standard": "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_gb/pay_later/standard/pink.svg"
      },
      "identifier": "pay_later",
      "name": "Pay later."
    }
  ]
  // ...
}

2.5: Load the Klarna widget

When that's done, present the Klarna widget to your customer by calling load, passing the ID of the container you added earlier and specifying the payment_method_categories.

If your customer has changed, say, their order or billing details since the session was created, you can also use load to pass these updates to the session by adding the relevant information to the data object.

Field nameDescription
optionsobjectRequiredUse this to contain the container ID, payment_method_categories, and instance_id.
options.container\ string\ RequiredThe ID you specified for the Klarna container above.
options.payment_method_categories\ array\ RequiredThe identifiers of the payment_method_categories you want to present to the customer. (i.e. pay_later and/or slice_it)
options.instance_id\ string\ RequiredThe ID you'll use to identify this instance of the Klarna Payments client.\ You should include this same instance_id for subsequent operations, like authorize, to indicate which instance the operation applies to.
data\ object\ OptionalIf the customer has changed any of their order or billing information, this can be used to update the session.
data.purchase_country\ string\ OptionalThe country in which the purchase is being made (two-letter ISO 3166 code).
data.purchase_currency\ string\ OptionalThe currency in which the payment is being made (three-letter ISO 4217 code).
data.locale\ string\ OptionalThe customer's locale (RFC 1766 code).
data.billing_address\ object\ OptionalDetails about the customer's billing address.
data.shipping_address\ object\ OptionalDetails about the customer's shipping address.
data.order-amount\ string\ OptionalThe total amount of the order, including tax and any discounts.
data.order_tax_amount\ integer\ OptionalTotal tax amount of the order.
data.order_lines\ array<object>\ OptionalDetails about the order.
This object is passed directly to Klarna as order_lines.1
data.customer\ object\ OptionalDetails about the customer.
callback\ function\ RequiredCatches the response from Klarna.Payments.load() for further processing.
šŸ“˜

We recommend passing the data object with every call to load, to ensure that the session reflects any changes the customer might have made to their order or billing information.

try {
  Klarna.Payments.load(
    // options
    {
      container: "#klarna_container",
      payment_method_categories: ["pay_later", "slice_it"],
      instance_id: "klarna-payments-instance"
    },
    // data
    {
      purchase_country: "GB",
      purchase_currency: "GBP",
      locale: "en-GB",
      billing_address: {
        given_name: "John",
        family_name: "Doe",
        email: "[email protected]",
        title: "Mr",
        street_address: "13 New Burlington St",
        street_address2: "Apt 214",
        postal_code: "W13 3BG",
        city: "London",
        region: "",
        phone: "01895808221",
        country: "GB"
      },
      shipping_address: {
        given_name: "John",
        family_name: "Doe",
        email: "[email protected]",
        title: "Mr",
        street_address: "13 New Burlington St",
        street_address2: "Apt 214",
        postal_code: "W13 3BG",
        city: "London",
        region: "",
        phone: "01895808221",
        country: "GB"
      },
      order_amount: 10,
      order_tax_amount: 0,
      order_lines: [
        {
          type: "physical",
          reference: "19-402",
          name: "Battery Power Pack",
          quantity: 1,
          unit_price: 1000,
          tax_rate: 0,
          total_amount: 1000,
          total_discount_amount: 0,
          total_tax_amount: 0,
          product_url: "https://www.estore.com/products/f2a8d7e34",
          image_url: "https://www.exampleobjects.com/logo.png"
        }
      ],
      customer: {
        date_of_birth: "1970-01-01",
        gender: "male"
      }
    },
    // callback
    function (response) {
      // ...
    }
  );
} catch (e) {
  // Handle error. The load~callback will have been called
  // with "{ show_form: false }" at this point.
}

Responses

{
  show_form: true
}
{
  show_form: true,
  error: {
    invalid_fields: ["billing_address.email"]
  }
}
{
  show_form: false
}

Step 3: Authorize

When your customer presses the "buy now" button, you can authorize the order. In return, you'll receive an authorization token, which you'll use to place the customer's order in step four.

🚧

The authorize call may require the customer to provide more details, meaning that the response could be instant, could take a very long time (i.e. the time it takes the customer to complete the form), or might not happen (if the customer drops out). The integration, therefore, should not rely on an immediate response and should not implement timeouts on the merchant side, but, rather, wait for the callback function to be called.

Field nameDescription
optionsobjectRequiredUse this to contain the instance_id.
options.instance_id\ string\ RequiredThe ID of the Klarna Payments instance you want to authorize.\ You should use the instance_id from the load call above.
data\ object\ OptionalUse this to provide any order or billing details that the customer has changed.
callback\ function\ RequiredCatches the response from Klarna.Payments.authorize() for further processing.
try {
  Klarna.Payments.authorize(
    // options
    {
      instance_id: "klarna-payments-instance" // Same as instance_id set in Klarna.Payments.load().
    },
    // data
    {
      billing_address: {
        // ...
      }
    },
    // callback
    function (response) {
      // ...
    }
  );
} catch (e) {
  // Handle error. The authorize~callback will have been called
  // with "{ show_form: false, approved: false }" at this point.
}

Responses

{
  authorization_token: "b4bd3423-24e3",
  approved: true,
  show_form: true
}
{
  approved: false,
  show_form: true,
  error: {
    invalid_fields: ["billing_address.email"]
  }
}
{
    approved: false,
    show_form: false
}

Step 4: Place the order

If the authorization was successful, you'll have received an authorization_token, which you can now use to place the customer's order.

ā—ļø

Do not auto-capture the payment!
When you're processing a transaction via Klarna, you must only capture the payment once the goods have been shipped to the customer, unless you are selling an online service that the customer may use immediately.

Endpoints

Live

https://api.checkout.com/payments

Sandbox

https://api.sandbox.checkout.com/payments

Header parameters

HeaderValue
AuthorizationRequired

secret key

Use the secret key from your Checkout.com account. You can find this in the Hub.

Content-Type\ Requiredapplication/json

Body parameters

Field nameDescription
amountIntegerRequiredThe payment amount in the chosen currency.
currency\ String\ RequiredThe currency in which the payment is being made (three-letter ISO 4217 code).
capture\ Boolean\ RequiredChoose whether or not to capture the payment.\ Defaults to true, but must be false in this case.
source\ Object\ RequiredDetails about the payment source.
source.type\ String\ RequiredThe type of payment source. Set this to klarna.
source.authorization_token\ String\ RequiredThe Klarna authorization token, which you'll have received when you authorized the order.
source.locale\ String\ Required

The customer's locale (RFC 1766 code).

Default value taken from your business settings.

source.purchase_country\ String\ Required

The country in which the purchase is being made (two-letter ISO 3166 code).

Default value taken from your business settings.

source.auto-capture\ Boolean\ OptionalAllows you to trigger auto-capture of the payment.
source.tax_amount\ Integer\ RequiredThe total tax amount for the order.
source.billing_address\ Object\ RequiredDetails about the customer's billing address.
This object is passed directly to Klarna as billing_address.1
source.shipping_address\ Object\ OptionalDetails about the customer's shipping address.
This object is passed directly to Klarna as shipping_address.1
source.customer\ Object\ OptionalDetails about the customer.
This object is passed directly to Klarna as customer.1
source.products\ Array<object>\ RequiredDetails about the order.
This object is passed directly to Klarna as order_lines.1
source.merchant_reference1\ String\ Optional

Use this to store an internal order number or other reference. If set, this will be shown on the confirmation page as order number.

Maximum characters: 255.

source.merchant_reference2\ String\ Optional

Use this to store an internal order number or other reference.

Maximum characters: 255.

source.merchant_data\ String\ Optional

Pass-through field.

Maximum characters: 1024.

source.attachment\ Object\ OptionalAdditional purchase information required for some industries.
This object is passed directly to Klarna as attachment.1

Request example

{
  "amount": 1000,
  "currency": "GBP",
  "capture": false,
  "source": {
    "type": "klarna",
    "authorization_token": "b4bd3423-24e3",
    "locale": "en-GB",
    "purchase_country": "GB",
    "tax_amount": 0,
    "billing_address": {
      "given_name": "John",
      "family_name": "Doe",
      "email": "[email protected]",
      "title": "Mr",
      "street_address": "13 New Burlington St",
      "street_address2": "Apt 214",
      "postal_code": "W13 3BG",
      "city": "London",
      "phone": "01895808221",
        "country": "GB"
        },
      "customer": {
        "date_of_birth": "1970-01-01",
        "gender": "male"
      },
      "products": [
        {
          "name": "Battery Power Pack",
          "quantity": 1,
          "unit_price": 1000,
          "tax_rate": 0,
          "total_amount": 1000,
          "total_tax_amount": 0
        }
      ]
    }
  }
}

Response examples

šŸ“˜

Unlike our other supported alternative payment methods, Klarna is always synchronous. The redirect links contained in the response set Klarna-specific cookies, so that the customer is recognized in future payment requests.

{
  "id": "pay_y3oqhf46pyzuxjbcn2giaqnb44",
  "action_id": "act_y3oqhf46pyzuxjbcn2giaqnb44",
  "amount": 1000,
  "currency": "GBP",
  "approved": true,
  "status": "Authorized",
  "response_code": "10000",
  "response_summary": "Approved",
  "source": {
    "type": "klarna",
    "order_id": "5ca8d9ed-d327-61a8-a8f7-c947073142ee",
    "fraud_status": "ACCEPTED"
  },
  "customer": {
    "date_of_birth": "1970-01-01",
    "gender": "male"
  },
  "processed_on": "2019-01-15T10:59:51Z",
  "reference": "ORD-5023-4E89",
  "_links": {
    "self": {
      "href": "https://api.checkout.com/payments/pay_y3oqhf46pyzuxjbcn2giaqnb44"
    },
    "actions": {
      "href": "https://api.checkout.com/payments/pay_y3oqhf46pyzuxjbcn2giaqnb44/actions"
    },
    "klarna:payment-capture": {
      "href": "https://api.checkout.com/klarna/orders/pay_jfcxs5vllp5ufden4lhe2wy4b4/captures/"
    },
    "klarna:payment-void": {
      "href": "https://api.checkout.com/klarna/orders/pay_jfcxs5vllp5ufden4lhe2wy4b4/voids/"
    }
  }
}
šŸ‘

The 202 response with an approved: true flag confirms that Klarna will process your payout as soon as you capture the payment in the next step.

Step 5: Capture the payment

Once you've shipped your goods to the customer, you can capture the payment. If, however, you provide the customer with immediate access to your product (e.g., you provide them with an online service), you can capture the payment as soon as the order is placed.

If you're shipping goods, it's a good idea to include the shipping_info for transparency as Klarna will include it in the correspondence with your customer.

Endpoints

Live

https://api.checkout.com/klarna/orders/{payment_id}/captures

Sandbox

https://api.sandbox.checkout.com/klarna-external/orders/{payment_id}/captures

Header and path parameters

HeaderValue
AuthorizationRequired

public key

Use the public key from your Checkout.com account. You can find this in the Hub.

Content-Type\ Requiredapplication/json
PathValue
payment_idRequiredThe payment ID (prefixed with pay_) you received in the response when you placed the order.

Body parameters

Field nameDescription
amountintegerOptionalThe amount you wish to capture in your chosen currency. If you don't specify, you will capture the full payment amount.
reference\ string\ OptionalA reference, such as an order number, you can later use to identify this payment.
metadata\ object\ OptionalA set of key-value pairs that you can attach to a payment. You can use this to store additional information in a structured format.
type\ string\ OptionalType which can be used as a discriminator for a more generic capture endpoint with property bags.
klarna\ object\ RequiredContains the order and shipping information.
klarna.description\ string\ OptionalA description of the order shown to the customer.\ Maximum 255 characters.
klarna.products\ array<object>\ OptionalThe order details.
This object is passed directly to Klarna as order_lines.1
klarna.shipping_info\ array\ OptionalThe shipping information for the order.
klarna.shipping_delay\ integer\ OptionalThe delay before the order is shipped. This field is currently not returned when reading the order.\ If you want to submit a value greater than 0, this needs to be enabled in your merchant account. Please contact Klarna for further information.

Request example

{
  "amount": 1000,
  "reference": "ORD-5023-4E89",
  "metadata": {},
  "type": "klarna",
  "klarna": {
    "description": "Your order with Checkout.com",
    "products":[
      {
        "name": "Battery Power Pack",
        "quantity": 1,
        "unit_price": 1000,
        "tax_rate": 0,
        "total_amount": 1000,
        "total_tax_amount": 0         
      }
    ],
    "shipping_info": [
      {
        "shipping_company": "DHL US",
        "shipping_method": "PickUpStore",
        "tracking_number": "63456415674545679874",
        "tracking_uri": "http://shipping.example/findmypackage?63456415674545679874",
        "return_shipping_company": "DHL US",
        "return_tracking_number": "93456415674545679888",
        "return_tracking_uri": "http://shipping.example/findmypackage?93456415674545679888"
      }
    ],
    "shipping_delay": 0
  }
}

Response examples

{
  "action_id": "act_4sz4seltcrzuvcgaetumnlamq4"
}
{
  "request_id": "beug8y83h978yh",
  "error_type": "request_invalid",
  "error_codes": [
    "customer_id_required",
    "given_name_exceed_length"
  ]
}

Additional error responses:

  • 403 Capture not allowed
  • 404 Payment not found

Refund a Klarna payment

Klarna supports both partial and full refunds. You can refund a payment through the Hub or using the refund API.

Void a Klarna payment

Endpoints

Live

https://api.checkout.com/klarna/orders/{payment_id}/voids

Sandbox

https://api.sandbox.checkout.com/klarna-external/orders/{payment_id}/voids

Header and path parameters

HeaderValue
AuthorizationRequiredpublic key\ Use the public key from your Checkout.com account. You can find this in the Hub.
Content-Type\ Requiredapplication/json
PathValue
payment_idRequiredThe payment ID (prefixed with pay_) you received in the response when you placed the order.

Body parameters

Field nameDescription
referenceStringOptionalA reference, such as an order number, you can later use to identify this payment.
metadata\ Object\ OptionalA set of key-value pairs that you can attach to a payment. You can use this to store additional information in a structured format.

Request example

{
  "reference": "ORD-5023-4E89",
  "metadata": {}
}

Response examples

{
  "action_id": "act_v6572a7elpuupbaljmoi4tk3ma"
}
{
  "request_id": "0HL80RJLS76I7",
  "error_type": "request_invalid",
  "error_codes": [
    "payment_source_required"
  ]
}

Additional error responses:

  • 403 Capture not allowed
  • 404 Payment not allowed

Footnotes

1 Please refer to Klarna's documentation for more information about the object definition.

Can we help?

Thanks for using Checkout.com. If you need any help or just have a question, please get in touch with our support team at [email protected].