The button interface allows programmatic access to the button interface
const klarna = ...;
klarna.Payment.button()
.on("click", (paymentRequest, button) => {
// Button click handler
})
.mount("#button-container");
<div id="#button-container"></div>
KlarnaPaymentButtonConfiguration
Register event handler to handle payment request state transitions. See PaymentRequestState for state transitions.
The handler is called when there is an update to the payment request, the handler may trigger even if there is no state transition.
The current state is available in paymentRequest.state
.
During a redirection flow the update
handler will trigger once your page has loaded.
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("update", (paymentRequest) => {
console.log(`Payment request is in state ${paymentRequest.state}`);
});
You can also use this event to handle scenarios where customer decided not to finish the purchase flow.
The state will go from IN_PROGRESS to SUBMITTED if customer decided to abort the payment request - either closed the window in the ON_PAGE flow or went back in the REDIRECT flow.
Example
klarna.Payment.on("update", (paymentRequest) => {
if(paymentRequest.previousState === "IN_PROGRESS" && paymentRequest.state === "SUBMITTED") {
console.log("The purchase flow was aborted");
}
});
update event, called on state transitions
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]
const { lineItems, paymentAmount } = fetch("/cart", {
method: "POST",
body: {
shipping_option: shippingOptions[0]
}
})
return {
paymentAmount,
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) => {
const { lineItems, paymentAmount } = fetch("/cart", {
method: "POST",
body: {
shipping_option: shippingOption
}
})
return {
paymentAmount,
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
The request() interface retrieves the on-going request or creates a new request. This interface is typically not required when working with the payment buttons as payment requests are implicitly created and available in event callbacks.
If this is called on a cancelled request, a new request is created. To cancel a request and restart the following pattern can be used.
await klarna.Payment.request().cancel();
klarna.Payment.request().initiate();
Optional
options: PaymentRequestOptions
The Klarna Payment interface provides access to the payment request API.
The payment interface can have one on-going payment request at a time. The SDK keeps track of the current on-going request.
Accepting a payment using the Klarna Payment button