Event callback handler.
"Register an event handler for the widgeterror event. Anytime an error occurs
during the widget flow, the error event is triggered. If you define the error
event handler, Klarna emits all errors to it instead of throwing them upwards
in the widget method.
Register this handler before calling {@docs-portal-link Customer.Transaction.widget} so that errors during widget initialization are captured."
klarna.Customer.Transaction.on("widgeterror", async (error, widgetData) => {
console.error("Widget error:", error.errorMessage);
if (widgetData.status === "NO_STATE_CHANGE") {
// The widget encountered an error but the payment state is unchanged
// You may retry or show an error message to the customer
}
});
The event name.
Event callback handler.
"Triggers when the widget closes and the widget state has changed. A
STATUS_CHANGE status in the callback data indicates the customer completed
the action (e.g. approved a payment or updated their details). Use this to
refresh your UI or proceed with the next step in your flow."
The event name.
Event callback handler.
"Triggers when the widget closes and the payment state has not changed.
This typically means the customer dismissed or closed the widget without
completing the action. The NO_STATE_CHANGE status confirms that no
updates occurred — your application can safely treat this as a no-op."
A token received from Klarna API that authenticates the widget.
"Load the Klarna widget for the action. The widget token is obtained from the Klarna API on your server and authenticates the widget session.
Register your event handlers (widgeterror, widgetcomplete, widgetcancel)
before calling this method so that all events are captured from the start.
The z-index of elements in the application page calling widget() should
not exceed 10000."
// 1. Register event handlers first
klarna.Customer.Transaction.on("widgeterror", async (error, widgetData) => {
console.error("Widget error:", error.errorMessage);
});
klarna.Customer.Transaction.on("widgetcomplete", async (widgetData) => {
if (widgetData.status === "STATE_CHANGE") {
await refreshOrderStatus();
}
});
klarna.Customer.Transaction.on("widgetcancel", async (widgetData) => {
console.log("Customer closed the widget.");
});
// 2. Then load the widget with the token from your server
klarna.Customer.Transaction.widget("eyJhbGciOi...");
The event name.