The button interface allows programmatic access to the button interface
const klarna = ...;
klarna.Payment.button()
.on("click", (button) => {
// Button click handler
})
.mount("#button-container");
<div id="#button-container"></div>
KlarnaButtonConfiguration
Cancels the payment request with the given ID. It's only possible to cancel payment requests created by the same client/credentials.
The ID of the payment request to fetch.
A Promise
that resolves to a PaymentRequest.
Fetches the payment request with the given ID. It's only possible to fetch payment requests created by the same client/credentials.
The ID of the payment request to fetch.
A Promise
that resolves to a PaymentRequest.
Initiates a payment request.
The payment request data. It can be one of the following:
Promise
resolving to a ["paymentRequestId"].Promise
resolving to a PaymentRequestData object.Optional
options: PaymentRequestOptionsOptional settings for the payment request.
A Promise
that resolves to a PaymentRequest or undefined if an error was thrown somewhere before completing.
Register an event handler for the abort
event.
A payment request is aborted when the customer decides to cancel the payment request by closing the popup or somehow cancel the process.
klarna.Payment.on("abort", (paymentRequest) => {
console.log(`The reason for the abort may be read by accessing ${paymentRequest.stateReason}`);
});
The event name ("abort").
Event callback handler.
Register an event handler for the complete
event.
A payment request is completed when the user has approved the purchase.
During a redirection flow the complete
handler will trigger once your page has loaded on the success page.
Pending updates are triggered even if the event handler is registered after the page has loaded
to avoid need for polling.
Example
klarna.Payment.on("complete", (paymentRequest) => {
console.log(`Confirm the payment request by sending the ${paymentRequest.stateContext.paymentToken} to your backend`);
// Return false if you want to prevent the redirection to the success page, if not, return anything or true.
return false
});
The event name ("complete").
Event callback handler.
Shipping address changed event handler, triggered when the consumer changes their shipping address during the payment flow.
Depending on your implementation you can return 3 different payloads from the handler.
Example
klarna.Payment.on("shippingaddresschange", (paymentRequest, shippingAddress) => {
return {
shippingOptions: [{
shippingOptionReference: string;
amount: number;
displayName: string;
description: string;
shippingType?: ShippingType;
}]
}
});
Example
klarna.Payment.on("shippingaddresschange", (paymentRequest, shippingAddress) => {
const { shippingOptions } = fetch("/shipping-options", {
method: "POST",
body: {
shipping_address: shippingAddress
}
})
const selectedShippingOption = shippingOptions[0]
amount } = fetch("/cart", {
method: "POST",
body: {
shipping_option: shippingOptions[0]
}
})
return {
amount,
lineItems: [
...lineItems,
{
name: selectedShippingOption.name,
quantity: 1,
totalAmount: selectedShippingOption.amount
}
],
selectedShippingOptionReference: selectedShippingOption.reference
shippingOptions: [{
shippingOptionReference: string;
amount: number;
displayName: string;
description: string;
shippingType?: ShippingType;
}]
}
});
Example
klarna.Payment.on("shippingaddresschange", (paymentRequest, shippingAddress) => {
return {
rejectionReason: klarna.Payment.ShippingRejectionReason.POSTAL_CODE_NOT_SUPPORTED
}
});
The available rejection reasons are:
If you did not return anything during the 10 seconds after receiving this event, consumer will see the error screen and payment request will be aborted.
onshippingaddresschange
Shipping address changed event handler callback
Shipping option selected event handler, triggered when the consumer selects a shipping option during the payment flow.
Depending on your implementation you can return 2 different payloads from the handler:
Example
klarna.Payment.on("shippingoptionselect", (paymentRequest, shippingOption) => {
amount } = fetch("/cart", {
method: "POST",
body: {
shipping_option: shippingOption
}
})
return {
amount,
lineItems: [
...lineItems,
{
name: 'Shipping option 1',
quantity: 1,
totalAmount: 500
}
]
}
});
Example
klarna.Payment.on("shippingoptionselect", (paymentRequest, shippingAddress) => {
return {
rejectionReason: klarna.Payment.ShippingOptionRejectionReason.INVALID_OPTION
}
});
The available rejection reasons are:
If you did not return anything during the 10 seconds after receiving this event, consumer will see the error screen and payment request will be aborted.
shippingoptionselect
Shipping option selected event handler callback
Check if the consumer can make a payment with a given a set of parameters.
The data for the payment check.
Optional
amount?: numberOptional
preference?: "KLARNA" | "PAY_NOW" | "PAY_LATER" | "PAY_OVER_TIME"Optional
requestCustomerToken?: {Optional
supplementaryPurchaseData?: {
The Klarna Payment interface provides access to the payment request API.
Accepting a payment using the Klarna Payment button