Before you begin

This guide assumes that you know how to:

Accept payments with Klarna

Prepare the API

Klarna requires the following data to process a payment successfully. Pass the following data in the client session, or in the payment request (for manual payment creation).

Parameter NameRequiredDescription
3-letter currency code in ISO 4217 format, e.g. USD for US dollars
  1. order
Details of the line items of the order
  1. order

The country code of the user.

Learn more about the supported countries and currencies by visiting Klarna Documentation.

  1. customer

Customer's email address. Pass this value to pre-fill the Klarna payment form.

  1. customer

Customer's mobile number (make sure it is the correct format for the country code). Pass this value to pre-fill the Klarna payment form.

  1. customer

Customer's shipping address. Pass this value to pre-fill the Klarna payment form.

  1. customer

Customer's billing address. Pass this value to pre-fill the Klarna payment form.

When passing customer.billingAddress in the client session, ensure that Klarna has payment method categories configured for the given address.

If Klarna is not supported for the given billing address, no Klarna button will be rendered in the checkout.

  1. paymentMethod
  2. options
  3. KLARNA

Extra Merchant Data (EMD) package supported by Klarna can be passed via the client session. Any JSON object that will be accepted by Klarna can be sent. Available schemas are documented here

Prepare the SDK for payments

Handle payment method

Integration

The KlarnaPaymentMethodMethodManager is an SDK manager designed to streamline the headless integrations of the Klarna payment method that is comprised of an initial component/form for selecting a payment category and a redirect flow.

To integrate KlarnaPaymentMethodMethodManager, follow these steps:

  1. 1

    The PrimerHeadlessCheckout will help you configure the headless payment method and then start the payment process

  2. 2

    When calling configure provide a callback function for onAvailablePaymentMethodsLoaded as described here

  3. 3

    For the Klarna payment method,configure a handler for the KLARNA payment method manager type and create a payment method manager as described here `

  4. 4

    When the payment categories for the Klarna payment method have been retrieved, onPaymentMethodCategoriesChange callback will be called with a payload that will have a format as seen here

  5. 5

    You can use the payment categories retrieved through the onPaymentMethodCategoriesChange callback to create the custom component/form

  6. 6

    When the user has selected a payment category you should call the renderCategory function available in the manager. The renderCategory function requires, as parameters, the selected category id, a container id where to render the details about the payment category and an onHeightChange callback that will be called when the payment category height has changed. You can use the height returned by the onHeightChange callback to update the height of the container in which the payment category details will be displayed.

  7. 7

    After the user has selected a category and the details about this category have been displayed you can call the start function provided by the payment method manager created at step 3

Create the PrimerHeadlessCheckout in order to interact with the Primer API

You can follow the steps described here in order to configure the PrimerHeadlessCheckout.

Provide a custom handler for the KLARNA payment method manager

As described here the onAvailablePaymentMethodsLoad callback will be called with all available payment methods. Use this function and create a custom configuration for the KLARNA payment method manager type and afterwards start the payment process.

123456789101112131415161718192021
await headless.configure({    onAvailablePaymentMethodsLoad(paymentMethods) {        // Called when the available payment methods are retrieved
        for (const paymentMethod of paymentMethods) {            // `type` is a unique ID representing the payment method            const { type, managerType } = paymentMethod            if (managerType === 'KLARNA') {                configureKlarnaPaymentMethodManager()            }        }    },    onCheckoutComplete({ payment }) {        console.log('onCheckoutComplete', payment)    },    onCheckoutFail(error, { payment }, handler) {        return handler.showErrorMessage()    },})
await headless.start()
js
copy
Create the KlarnaPaymentMethodManager

Call the start function in order to start the flow of the component:

12345678910
function configureKlarnaPaymentMethodManager(){    paymentMethodManager = await headless.createPaymentMethodManager(        paymentMethod,        {        onPaymentMethodCategoriesChange:(categories: KlarnaPaymentMethodCategory[]) =>{            renderCustomComponent(categories)        },        },    );  }
js
copy

The onPaymentMethodCategoriesChange callback received a payload as described here The options array can be used to configure a custom component/form.

Handle data collection

As mentioned previously, the component allows you to collect the payment categories, and to request payment finalization by calling the start function from the payment method manager. You can use the result of the start function to handle any errors

123456789101112
async function onPaymentCategorySelected( categoryId: string ) {    const response  = await paymentMethodManager.start({      paymentMethodCategoryId: categoryId,    });
    if (response?.outcome === 'DENIED') {        console.log("Something went wrong.", result.validationErrors)        return;    }
    console.log("The payment finalized")}
js
copy

Vaulting

In order to vault Klarna accounts, pass the following data in the Client Session:

Parameter NameRequiredDescription
A unique identifier for your customer

Prepare the SDK for vaulting

To vault Klarna, follow the steps to vault after successful authorization.

Test

Visit Klarna Sample Data Page for testing the integration against the Klarna Playground environment.

Go live

You don’t need to do anything particular to go live — just make sure to use production credentials.