Why choose Cashfree?
- Simplified checkout: Provides a pre-built, easy-to-use checkout page that delivers an optimised payment experience. It accepts payments from over 120 payment methods easily and securely.
- Secure and PCI compliant: Securely collects payment details and submits them directly to Cashfree servers, removing the need for Payment Card Industry Data Security Standard (PCI DSS) compliance requirements at the merchant’s end.
- Personalised: Customise your payment methods, branding, and various other elements to align seamlessly with your company’s specific theme.
Pre-requisites
- Create a Cashfree Merchant Account.
- Log in to the Merchant Dashboard and generate App ID and Secret Key. Learn how to generate API keys.
- Ensure you have whitelisted your website domain for integration. Read more about Domain Whitelisting.
- Creating an order.
- Opening the checkout page.
- Confirming the payment.
Step 1: Creating an order
To integrate the Cashfree Payment Gateway, the first step is to create an order. This must be done before any payment processing can occur. Set up an endpoint on your server to handle order creation as you cannot call this API from client side.Order creation must be done through your backend, as this API requires your secret key. It should not be called directly from the client-side.
API request for creating an order
Here’s a sample request for creating an order using your desired backend language. Cashfree offers backend SDKs to simplify the integration process.order_id and payment_session_id that you need for subsequent steps.
You can view all the complete API request and response for /orders here.
Step 2: Opening the checkout page
1. Include JS SDK in your client code
To integrate the Cashfree checkout, you must include our JavaScript SDK within your JS file.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 a test environment, whereas production is used for production mode.
3. Open Cashfree checkout
To open the checkout, you can use thecashfree.checkout() method. This method can take in the following parameters :
| Property Value | Description |
|---|---|
| _self(default) | Opens the payment link in the same frame as it was clicked. |
| _blank | Opens the payment link in a new window or tab. |
| _top | Opens the linked document in the full body of the window. |
| _modal | Opens the payment link in a pop-up window on the current page. |
| DOM element | Opens the payment link directly within a specified DOM element. |
When using pop-up and inline checkout, the approach varies slightly. You need to manage the promise returned by
cashfree.checkout() to execute any additional code following the payment attempt.Step 3: Confirming the payment
Once the payment is completed, you need to confirm whether the payment was successful by checking the order status.We recommend that you provide a return URL while creating an order. This will improve the overall user experience by ensuring your customers don’t land on
broken or duplicated pages. Also, remember to add context of the order in your return URL so that you can identify the order once the customer has returned to
this URL.
cashfree.checkout() function returns a promise which should be handled by the merchant.
Order status verification
To verify an order you can call our/pg/orders endpoint from your backend. You can also use our SDK to achieve the same.
Before delivering services to the customer, always verify the status of the order, which can be done using the Get Order API. An order is considered successful when the
order_status is PAID.Testing
You should now have a working checkout button that opens the Cashfree-hosted payment page. If your integration isn’t working, try the following steps:- Open the Network tab in your browser’s developer tools.
- Click the button and check the console logs.
- Ensure that the correct environment and payment session ID are being passed.
- Use
console.log()inside your button click listener to confirm whether the data is being passed correctly or not.
💻 Quick dev-to-dev talk
You clearly care about building better payment experiences for your clients, here’s a quick tip: Earn additional income doing exactly what you’re doing now!Join the Cashfree Affiliate Partner Program and get rewarded every time your clients use Cashfree.What’s in it for you?- Earn up to 0.25% commission on every transaction
- Be more than a dev - be the trusted fintech partner for your clients
- Get a dedicated partner manager, your go-to expert
- Instant activation, go live in minutes.
- Industry-best success rate across all payment modes.
- Effortlessly accept international payments in 140+ currencies