The actual custom element rendered in the DOM
Unique button identifier
Programmatically adds a button to the DOM tree.
Timings:
render
event is triggered after the button is fully visible.The container element or selector where the button will be mounted.
The render
event can be used to load in data asynchronously that would
render
Render event handler callback, triggered on render.
Button click event handler, triggered when the consumer clicks a payment button.
From the event handler you should trigger initiate
or prepare
.
If you do not wish to proceed with the payment request you may return from the handler without triggering any action.
Example
klarna.Payment.button().on("click", (paymentRequest, button) => {
paymentRequest.initiate({
currency: "USD",
paymentAmount: 1000
});
});
Note that the handler must run synchronously for the payment flow to be able to be able to open a popup window. If you are distributing with a QR code you may run asynchronous code inside the handler, but wrapped in then/catch.
If you need to asynchronously fetch data before the click handler runs, you can use the render
event and disable
the button during the fetching. You should indicate to the consumer that data is loading if using this pattern.
Example of fetching cart asynchronously data on-load
klarna.Payment.button({ id: "my-button" }).on("render", async (button) => {
button.toggleState('loading', true);
button.toggleState('disabled', true);
const response = await fetch("http://example.com/basket")
const cart = await response.json();
button.data["mycart"] = cart;
button.toggleState('loading', false);
button.toggleState('disabled', false);
}).on("click", (paymentRequest, button) => {
return paymentRequest.initiate({
currency: button.data["mycart"].cartCurrency,
paymentAmount: button.data["mycart"].totalCartAmount;
});
}).mount('#button-container');
<div id="button-container"></div>
click
Click event handler callback
State "disabled" disables/enables the button, e.g. makes it non-interactive/interactive State "loading" state displays/hides the loading indicator in the button
Optional
value: booleancan be used to set the state to a specific value instead of toggling
Removes the button from the DOM tree.
Interface to the Payment Button interface.