2. Open Cashfree Checkout

This is the second step in integrating Cashfree Checkout. Learn how to include, initialise the JS SDK, and finally open the Checkout Page.

Prerequisites

  1. Make sure you have created Cashfree Account and API keys are generated.
  2. You have created an order.

1. Include JS SDK in your client code

To integrate the Cashfree Checkout, you must include our JavaScript SDK within your JS file.

You can do this by

Option 1: Including the Script Tag. Import the URL in the snippet below to your HTML file.

<script src="https://sdk.cashfree.com/js/v3/cashfree.js"></script>

Option 2: Install from npm

npm install @cashfreepayments/cashfree-js
labs icon Try Cashfree DevStudio
Try Now

2. Initialise the SDK

You need to initialise the variable using the Cashfree() function. There are two modes applicable for this - "sandbox" or "production". Sandbox is used for test environment, whereas production is used for production mode.

const cashfree = Cashfree({
    mode:"sandbox" //or production
});

3. Open Cashfree Checkout

To open the checkout, you can use cashfree.checkout()method.
This method can take in following parameters -

  • paymentSessionId(required)- This is received in the response of Create Order API.

  • redirectTarget(optional) - This lets you decide how to open the Cashfree Checkout. Values allowed and their descriptions are mentioned in the table below.

    ValueDescription
    _blankOpens the payment link in a new window or tab.
    _self(default)Opens the payment link in the same frame as it was clicked.
    _modalOpens the payment link in a pop-up window on the current page.
    _topOpens the linked document in the full body of the window.
    <framename>Opens the linked document in the named iframe.

Open Cashfree Checkout in following ways:

For _self and _blank redirect targets:

let checkoutOptions = {
    paymentSessionId: "your-payment-session-id",
    redirectTarget: "_blank" //optional (_self or _blank)
}

cashfree.checkout(checkoutOptions)

This method is straightforward for opening the checkout in the same frame (_self) or a new window/tab (_blank).

For the _modal redirect target:

When using _modal, the checkout opens in a pop-up window on the current page. The usage differs slightly as you should handle the promise returned by cashfree.checkout() to execute additional code after the payment attempt:

let checkoutOptions = {
    paymentSessionId: "your-payment-session-id",
    redirectTarget: "_modal"
};

cashfree.checkout(checkoutOptions).then((result) => {
    if(result.error){
        // This will be true whenever user clicks on close icon inside the modal or any error happens during the payment
        console.log("User has closed the popup, Check for Payment Status");
        console.log(result.error);
    }
    if(result.redirect){
        // This will be true when the payment redirection page couldnt be opened in the same window
        // This is an exceptional case only when the page is opened inside an inAppBrowser
        // In this case the customer will be redirected to return url once payment is completed
        console.log("Payment will be redirected");
    }
    if(result.paymentDetails){
        // This will be called whenever the payment is completed irrespective of transaction status
        console.log("Payment has been completed, Check for Payment Status");
        console.log(result.paymentDetails.paymentMessage);
    }
});
labs icon Try Cashfree DevStudio
Try Now

Whitelist Domain

❗️

Attention

This integration will require whitelisting of your domain used for opening the checkout page. Any other domain used to open the checkout page will be blocked by Cashfree. You can request for domain whitelisting via Developers > Whitelisting from your merchant dashboard.

You can check the step-by-step process of whitelist request here.


What’s Next