Example

window.errorCallback = function(data) {
    // If payment fails with status “error” SDK triggers the errorCallback.
    // In errorCallback we show error popup by checking if form_of_payment in data is “token_pay” or “redirect”.
    let validFormsOfPayments = ['token_pay', 'redirect'];
    if (validFormsOfPayments.includes(data.form_of_payment) || data.challenge_occurred) {
        const message = "Oops, something went wrong. Refresh the page and try again.";
        // displays popup with data.message if present else displays static message
        window.Checkout.showPopup("error", data.message || message);
    }
    console.log('Error callback', data);
    // Unfreeze the basket upon an error
    unfreezeBasket();
};

window.successCallback = function(data) {
    // If payment gets completed with status “success” SDK triggers the successCallback.
    // In successCallback we redirect the user to data.redirect_url
    window.location.href = data.redirect_url;
};

window.cancelCallback = function(data) {
    // If payment fails with status “canceled” SDK triggers the cancelCallback.
    // In cancelCallback we show error popup by checking if pg_name in data.payment_gateway_info is “kpay” or data.form_of_payment is “token_pay”.
    if (data.payment_gateway_info && data.payment_gateway_info.pg_name === "kpay") {
        // displays popup with pg_response as key value pairs
        window.Checkout.showPopup("error", '', data.payment_gateway_info.pg_response);
    } else if (data.form_of_payment === "token_pay" || data.challenge_occurred) {
        const message = "Oops, something went wrong. Refresh the page and try again.";
        // displays popup with data.message if present else displays static message
        window.Checkout.showPopup("error", data.message || message);
    } else if (data.redirect_url && data.status.toLowerCase() === 'canceled') {
        // If the cancellation is successful and the status is 'canceled', the customer will be redirected to the transaction details page
        window.location.href = data.redirect_url;
    }
    console.log('Cancel callback', data);
    // Unfreeze the basket upon an error
    unfreezeBasket();
};

// Before any payment action (Apple Pay, Google Pay, token payments, direct payments, etc.)
window.beforePayment = function(data) {
    return new Promise(function(resolve, reject) {
        fetch('https://api.yourdomain.com/basket/freeze', {
            method: 'POST'
        })
        .then(function(response) {
            if (response.ok) {
                if (data && data.redirect_url) {
                    window.Checkout.showPopup('redirect', data.message || 'Redirecting to the payment page', null);
                }
                resolve(true);
            } else {
                reject(new Error('Failed to freeze the basket.'));
            }
        })
        .catch(reject);
    });
};

function unfreezeBasket() {
    fetch('https://api.yourdomain.com/basket/unfreeze', {
        method: 'POST'
    });
    // Handle unfreeze basket responses or errors if necessary
}

Checkout.init({
    selector: "checkout",
    merchant_id: 'sandbox.ottu.net',
    session_id: '51436d465f77e59242ef25f15409c2f23fe54761',
    apiKey: '3bf2a041d0896419415feac3228b17e3ec53e793',
    lang: 'en',
    ‘displayMode’: ‘grid’, // default is column
});

HTML

JS

JS

Checkout init function

Last updated