Android Integration
Learn more about the android sdk integration in your app
Payment Gateway Android Guide
This integration allows you to integrate with a Cashfree Android SDK in your app. You can view the integration video on Youtube.
Setting Up SDK
Add to your module’s build.gradle
file if your gradle version is less than 7.+ or below.
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.
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 Payment Page
Once the order is created, the next step is to open the payment page so the customer can make the payment. Cashfree Android SDK allows you to open a payment page with all payment methods (cards, UPI, Net Banking).
To complete the payment, we can follow the following steps:
- Create a
CFSession
object. - Create a
CFTheme
object. - Create a Web Checkout Payment object.
- Set payment callback.
- Initiate the payment using the payment object created from [step 3]
We go through these step by step below.
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).
Customize Theme (Optional)
You can customize the appearance of the checkout screen using CFTheme. This step is optional but can help maintain consistency with your app’s design.
Specify Payment Methods
Use CFPaymentComponent
to define which payment methods you want to offer to your customers. You can add or remove payment modes as needed.
Create Checkout Object
This object combines all the configurations (session, payment modes, and theme) into a single checkout configuration. Finally, call doPayment()
to open the Cashfree checkout screen. This will present the user with the payment options and handle the payment process.
UPI Intent Object (Optional)
This flow is for merchants who wants to quickly provide UPI functionality using cashfree’s mobile SDK without handling other modes like Cards or Net banking.
Setup callback
The SDK exposes an interface CFCheckoutResponseCallback
to receive callbacks from the SDK once the payment journey ends.
This class consists of 2 methods:
public void onPaymentVerify(String orderID)
public void onPaymentFailure(CFErrorResponse cfErrorResponse, String orderID)
Open Checkout Screen
Finally, call doPayment()
to open the Cashfree checkout screen. This will present the user with the payment options and handle the payment process.
Step 3: Sample Code
Step 4: Confirming the Payment
After 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 to your implementation of the CFCheckoutResponseCallback
interface.
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:
- Open the Network tab in your browser’s developer tools.
- Click the button and check the console logs.
- 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.
Other Options
Was this page helpful?