Learn how to set up the Javascript SDK to display Klarna's widget.
During the checkout, your customers can see Klarna as a payment option, select it, and go ahead with the purchase. To enable these customer interactions, you need to perform two main actions:
The two actions take place on the client side. You have to set up Klarna's JavaScript SDK and make the load()
call for displaying the widget and the authorize()
call for getting the purchase authorization.
To make the call for displaying the widget, first you need to add and initialize the SDK and then place a container in your page.
Add the SDK by including the following script in the body section of your checkout page.
You do not need to install the JavaScript SDK beforehand. It's enough to include the previous script in the HTML file of your checkout page.
Initialize the SDK by making an init()
call and passing the client_token
from the Initiate a payment step.
The client_token
relies on the created session, which expires after 48 hours. The purchase might fail after this time.
Add a container in the HTML file of your checkout page. This container specifies where to place the widget.
At checkout, your customers can choose to pay with Klarna. Then, a pop-up window is opened where your customers log into their Klarna account and select their preferred Klarna payment method.
You need to use our JavaScript SDK to display Klarna in your checkout. This interaction is possible through the load() call happening on the client side.
The following information is applicable for both scenarios, one-time and recurring payments.
At checkout, display Klarna as a payment option by using the load()
call. Specify the following parameter:
container
, the identifier of the container you added to your checkout pageFor more information about load()
call parameters, refer to the Klarna payments SDK reference.
For a better user experience, call load()
when loading your checkout. In this way, you ensure that the container of Klarna payments loads immediately in a hidden container, and it's ready to appear when needed.
Don't forget to use the Klarna logo that you received in Step 1: Initiate a payment and add the payment method name. From the payload you received in Step 1, use:
identifier
to dynamically load the payment optionname
to display the correct and localized payment method titleasset_urls
to contain the Klarna logoYou can manage the name and Klarna descriptor when adding the Klarna payments 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 Klarna payments is availableerror,
containing details of potential error messagesWe 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:
If the response flag is show_form: true
, your checkout displays Klarna as a payment option for this purchase. This is the standard response.
If the response flag is show_form: false , your store is not able to offer Klarna as a payment option.
This negative response results from the pre-assessment that Klarna executes for the purchase.
To handle the negative response, you can set up one of the following visual arrangements in your checkout:
When your customer selects Klarna to pay, you need to use our JavaScript SDK to get authorization for the purchase.
In this step, you send all the necessary customer details for Klarna to assess and decide whether or not to accept this purchase. When the authorization is successful, you receive an authorization token as a response, useful for Step 3: Create an order.
The authorization is possible through the authorize()
call happening on the client side, however you should implement the authorization callback to received the authorization token is also on the server side and ensure that no authorization is missed.
The following information is useful for both scenarios, one-time and recurring payments.
Get authorization for the purchase by using the authorize()
call. We recommend sending the following parameters in the data object of the authorize call:
billing_address
, containing the customer's billing address.shipping_address
, containing the customer's shipping address. If you don't provide a shipping address, we copy the billing address and use it as the shipping address.If no billing or shipping address is provided, new Klarna customers have to manually enter their full billing address on the Klarna payment page. Providing the billing address in the authorize()
call enables Klarna to prefill the signup form for new customers.
We recommend to avoid any delays or asynchronous operations (for example, sending calls to your backend) between the user clicking the Buy button and you calling authorize()
. Longer delays or asynchronous operations between the actual user interaction and the authorize()
call cause the browser to block the Klarna pop-up from opening.
To update the Klarna session, provide all necessary information when calling authorize()
. If you need to update your backend session, we recommend doing so when the customer returns from Klarna's purchase flow.
In some cases, Klarna requires additional information about the customer and purchase. We recommend you send it in the authorize()
call. For more information, see the Extra merchant data section.
For GDPR (General Data Protection Regulation) reasons, you shouldn't send customer data before the authorize()
call.
Klarna uses all this information to make the risk assessment. To see what data you need per market and how to format it, check the customer data requirements.
During the authorize()
call and until you receive the callback, Klarna runs a purchase flow that includes a risk assessment. You need to visually indicate to your customer that an ongoing process is happening. For a better user experience, we suggest:
authorize()
call (for example, disable the buy button)Utilizing the server-side authorization callback is critical for maintaining a seamless transaction process. This method ensures that you can get the necessary authorization_token
and session_id
on the back-end, even when facing client-side communication problems. This capability is crucial for creating orders and enhancing the customer experience, even during front-end issues. It underscores the importance of the authorization callback in maintaining transaction continuity and ensuring a smooth process for consumers.
For more details, see the authorization callbacks documentation.
There are three potential cases that you need to handle based on the response:
If the response is approved: true
, Klarna has approved the authorization for this purchase and as a response you will received the response including:
approved
, containing the authorization result. It's a boolean value that indicates approved or denied.show_form
, showing the availability of Klarna as a payment option. It's a boolean value that indicates displayed or hidden.authorization_token
, containing the token that allows you to place the order. This is only returned if it's an approved authorization.error
, contanining details of potential error messages.authorization_token
allows you to create an order for both scenarios, recurring and one-time payments. The token is valid for 60 minutes.
If the response of the authorize is not successful with show_form: true
and an error
object containing invalid_fields
, something fixable is wrong and the customer needs to take action. An error message is displayed asking the customer to make corrections before you re-authorize the purchase. The error message points out which fields are incorrect.
It's also possible that the response is approved: false
and show_form: true
, but the callback doesn't include error
. This means the customer has terminated a required interaction in the widget, such as authentication or sign-up flows. In this case, you should keep Klarna visible so that the customer can make another purchase attempt.
We suggest you use the error message in the authorize()
response to highlight a specific input field on your page.
If the response is show_form: false
, your store is not able to offer Klarna as a payment option. You should disable Klarna from your checkout, and your customer might select another payment method.
This negative response results from the pre-assessment that Klarna executes for the purchase.