Cordova Integration Guide

Setting Up SDK

The Cordova SDK is hosted on npm.org you can get the sdk here. It supports Android SDK version 19 and above and iOS minimum deployment target of 11 and above. Navigate to your project and run the following command

npm install cordova-plugin-cashfree-pg
iOS

For iOS run the following commands

cd ios
pod install --repo-update
Installing the Plugin

Step 1: Creating an Order

The first step in the Cashfree Payment Gateway integration is to create an Order. You need to do this before any payment can be processed. You can add an endpoint to your server which creates this order and is used for communication with your frontend.

Order creation must happen from your backend (as this API uses your secret key). Please do not call this directly from your mobile application.
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.

You can find the SDKs here.

After successfully creating an order, you will receive a unique 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

Once the order is created, the next step is to open the payment page so the customer can make the payment. Cordova SDK offer below payment flow:

To complete the payment, we can follow the following steps:

  1. Create a CFSession object.
  2. Set payment callback.
  3. Initiate the payment using the payment object.

Create a Session

This object contains essential information about the order, including the payment session ID (payment_session_id) and order ID (order_id) obtained from Step 1. It also specifies the environment (sandbox or production).

let session = {
  payment_session_id: "payment_session_id",
  orderID: "order_id",
  environment: "SANDBOX", //"SANDBOX" or "PRODUCTION"
};

Setup payment callback

The SDK exposes an interface CFCallback to receive callbacks from the SDK once the payment flow ends. The callback supports two methods:

onVerify(result)
onError(error)
function onDeviceReady() {
  const callbacks = {
    onVerify: function (result) {
      let details = {
        orderID: result.orderID,
      };
      console.log(details);
    },
    onError: function (error) {
      let errorObj = {
        orderID: error.orderID,
        status: error.status,
        code: error.code,
        type: error.type,
        message: error.message,
      };
      console.log(errorObj);
    },
  };
  CFPaymentGateway.setCallback(callbacks);
}
Always call setCallback before calling doPayment method of SDK

Initiate the Payment

Step 3: Sample Code

Sample Github Code

Sample UPI Test apk

Step 4: Confirming the Payment

Once the payment is completed, you need to confirm whether the payment was successful by checking the order status. Once the payment finishes, the user will be redirected back to your activity.

You must always verify payment status from your backend. Before delivering the goods or services, please ensure you call check the order status from your backend. Ensure you check the order status from your server endpoint.

To verify an order you can call our /pg/orders endpoint from your backend. You can also use our SDK to achieve the same.

Testing

You should now have a working checkout button that redirects your customer to Cashfree Checkout. If your integration isn’t working:

  1. Open the Network tab in your browser’s developer tools.
  2. Click the button and check the console logs.
  3. Use console.log(session) inside your button click listener to confirm the correct error returned.

Error Codes

To confirm the error returned in your android application, you can view the error codes that are exposed by the SDK.