Interface KlarnaPaymentButton

Interface to the Payment Button interface.

From the click event handler you should trigger initiate.

Example

klarna.Payment.button().on("click", (button) => {
Klarna.Payment.initiate({
currency: "USD",
amount: 1000
});
});

Note that awaiting any asynchronous code before the initiate call in the click handler will prevent popups from opening which will affect user experience negatively.

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('disabled', true);
button.toggleState('loading', true);
const response = await fetch("http://example.com/basket")
const cart = await response.json();
button.data["mycart"] = cart;
button.toggleState('disabled', false);
button.toggleState('loading', false);
}).on("click", (button) => {
return Klarna.Payment.initiate({
currency: button.data["mycart"].cartCurrency,
amount: button.data["mycart"].totalCartAmount;
});
}).mount('#button-container');
<div id="button-container"></div>
interface KlarnaPaymentButton {
    htmlElement: HTMLElement;
    id?: string;
    mount(containerSelector: string): this;
    mount(containerElement: HTMLElement): this;
    on(event: "render", callback: (button: this) => Promise<void>): this;
    on(event: "click", callback: (button: this) => Promise<void>): this;
    toggleState(state: "disabled" | "loading", value?: boolean): this;
    unmount(): this;
}

Hierarchy (View Summary)

Properties

htmlElement: HTMLElement
id?: string

Methods

  • Parameters

    • containerSelector: string

    Returns this

  • Parameters

    • containerElement: HTMLElement

    Returns this

  • The render event is triggered after the button is fully visible.

    Parameters

    • event: "render"

      render

    • callback: (button: this) => Promise<void>

      Render event handler callback, triggered on render.

    Returns this

  • The click event is triggered after the button is clicked.

    Parameters

    • event: "click"

      click

    • callback: (button: this) => Promise<void>

      Render event handler callback, triggered on click.

    Returns this

  • Parameters

    • state: "disabled" | "loading"

      State "disabled" disables/enables the button, e.g. makes it non-interactive/interactive State "loading" displays/hides the loading indicator in the button

    • Optionalvalue: boolean

      can be used to set the state to a specific value instead of toggling

    Returns this