Klarna Docs - 2.1: Display the Klarna widget.

2.1: Display the Klarna widget.

After the JavaScript SDK is up and running, it's time to display the Klarna widget in your checkout.

Before proceeding, ensure you've initialized Klarna's Javascript SDK.

At checkout, the customer can select from the widget Klarna as the payment method for their purchase.

You need to use our JavaScript SDK to display the widget. This interaction is possible through the load() call happening on the client side.

The following information is useful for both scenarios, one-time and recurring payments.

Display the widget in the checkout by using the load() call. Specify the following parameters:

  • container, the id of the container you added to your checkout page (for more details, see the Set up JavaScript SDK section)
  • payment_method_category, the options to pay with Klarna that will appear in the widget (for more details, see the Payment methods section).
JAVASCRIPT
Klarna.Payments.load({
  container: '#klarna-payments-container',
  payment_method_category: 'pay_later'
}, function (res) {
  console.debug(res);
})

Sample of the load() call to display the widget.

For a better user experience, call load() when loading your checkout. In this way, you ensure that the widget loads immediately in a hidden container, and it's ready to appear whenever your customer selects Klarna as a payment method. This strategy is to avoid any loading delay after clicking on the Klarna payment method.

You can customize the look and feel of the widget according to your brand guidelines. See the additional features section to learn more.

Don't forget to use the Klarna logo that you received in Step 1: Initiate a payment and add the payment method title. For more information, see the Klarna payments best practices guide.

Managing the name and the Klarna logo takes place outside the Klarna widget. You can handle it when adding the widget in your checkout and use the name and asset_urls returned in Step 1: Initiate a payment.

After processing the load() call, the callback function is executed. The Javascript callback is an object containing the following parameters:

  • show_form, indicating if the Klarna payment methods are available in the widget
  • error, containing details of potential error messages.

We know that you want to give customers a great checkout experience. For this reason, we created a functionality to alert your store when your customer cannot use any Klarna payment method. This alert is through the show_form boolean parameter, which is used as a response flag to the load() call. We recommend you listen to it all the time.

There are two potential cases that you need to handle based on the response flag:

  • Success response: Your checkout offers Klarna payments in the widget.
  • Error response: Your checkout is not able to offer Klarna payments.

Success response.

If the response flag is show_form: true, the widget displays the Klarna payment methods available for this purchase. This is the standard response.

JSON
{
show_form: true
}

Sample of a success response to offer Klarna payments.

Error response.

If the response flag is show_form: false, your store is not able to offer the selected Klarna payment method in the loaded widget.

This negative response results from the pre-assessment that Klarna executes for the purchase.

JSON
{
show_form: false
}

Sample of an error response that prevents from offering Klarna payments.

To handle the negative response, you can set up one of the following visual arrangements in your checkout:

  • Hide the Klarna option from the checkout.
  • Grey out the Klarna option and disable clicking.
  • Keep the Klarna option and accept an error message in the widget. Klarna informs the customer that this option is not available.