CFCardNumberView component captures and processes the card number entirely within the SDK, your application never receives the raw card number. This makes the integration suitable for non-PCI merchants, that is, merchants who are not certified to store, process, or transmit raw cardholder data, and who rely on the SDK to handle card data securely on their behalf.
This page covers the custom card component integration only. For the full Android Element integration, including net banking, wallet, and UPI, see Android Integration.
Prerequisites
Complete the following tasks before you start the integration:- Create a Cashfree Merchant Account.
- Log in to the Merchant Dashboard and generate an App ID and Secret Key. Learn how to generate API keys.
- Set your application’s
minSdkVersionto API level 19 or higher.
Step 1
Create an order
Step 2
Open the payment page
Step 3
Confirm the payment
Step 1: Create an order Server-side
Create an order from your backend server before you process any payment.This API requires your secret key. Create orders through your server only — do not call this API 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.order_id and payment_session_id that you need for subsequent steps.
You can view the complete API request and response for /orders in the Create Order API.
Step 2: Open the payment page Client-side
After you create the order, set up the card component and open the payment page so the customer can provide their card details.1. Set up the SDK
The Cashfree Android SDK is available on Maven Central. The latest version is 2.5.0. The SDK requires Android API level 19 or higher. Add the following dependency to your app-levelbuild.gradle file:
2. Complete the payment
To complete the payment, follow these steps:- Add the
CFCardNumberViewcomponent to your layout XML. - Initialise the card component in your activity or fragment.
- Create a
CFSessionobject. - Set up the
ICardInfocallback. - Set up the payment callback.
- Build the payment object and initiate the payment.
Add the card component to your layout
TheCFCardNumberView component extends TextInputLayout, which means all standard TextInputLayout properties and methods apply to it. Add it to your layout XML file as follows:
Because
CFCardNumberView extends TextInputLayout, you can also call standard TextInputLayout methods programmatically. The following example shows commonly used methods:
Initialise the card component
Obtain a reference to theCFCardNumberView in your activity or fragment:
Create a session
TheCFSession object holds the session context for the payment. It accepts the payment_session_id and order_id obtained from Step 1, and the Cashfree environment (.SANDBOX or .PRODUCTION).
Set up the ICardInfo callback
Call theinitialize() method on the CFCardNumberView object after you have created the session. The callback delivers card metadata to your application after each digit the customer enters.
JSONObject with the following structure:
The following log output illustrates how the callback data evolves as the customer enters their card number:
Set up the payment callback
The SDK exposes an interfaceCFCheckoutResponseCallback to receive callbacks from the SDK once the payment flow ends. This interface consists of two methods:
Build the payment object and initiate payment
When the customer fills in their card details and taps the pay button, build theCFCard and CFCardPayment objects and call doPayment() on the CFCardNumberView instance.
Call
doPayment() on the cfElementCard object, not on CFCorePaymentGatewayService. This is different from the raw card flow described in the Android Integration page.Sample code
The following example shows a complete custom card component payment flow, including session creation, card component initialisation, optional theme customisation, and payment initiation.Custom card component payment sample
Custom card component payment sample
Sample GitHub code
Android custom card component sample
Android custom card component sample
Step 3: Confirm the payment Server-side
After the SDK delivers a callback viaonPaymentVerify, confirm the payment status from your backend before taking any action. The SDK callback signals only that the payment flow has ended. It does not guarantee a successful payment.
To verify an order you can call our /pg/orders endpoint from your backend. You can also use our SDK to achieve the same.
Always verify the order status from your backend before you deliver goods or services to the customer. You can use the Get Order API for this. An order is successful when the
order_status is PAID.Error codes
To confirm the error returned in your Android application, you can view the error codes exposed by the SDK.Show error codes
Show error codes
Other options
The following optional configurations let you customise the payment screen appearance and enable SDK logging for troubleshooting.(Optional) Customise the theme
(Optional) Customise the theme
Apply a custom theme to the payment screen to match your application’s visual design. Use the
CFTheme builder to set colours for the navigation bar, buttons, and text. Apply the theme to your payment object before you call doPayment().(Optional) Enable logging to debug issues
(Optional) Enable logging to debug issues
To enable SDK logging, add the following entry to your The following logging levels are available, listed from least to most verbose:
values.xml file: