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.
- Use Cashfree iOS SDK version 2.4.0 or above.
- Set your application’s minimum deployment target to iOS 13.0 or higher.
Step 1
Create a subscription
Step 2
Open the payment page
Step 3
Confirm the payment
Step 1: Create a subscription Server-side
Create a subscription from your backend server before you process any payment.This API requires your secret key. Create subscriptions through your server only. Do not call this API directly from your mobile application.
subscription_id and a subscription_session_id. Pass both values to your iOS client to proceed with the payment.
API request for creating a subscription
API request for creating a subscription
The following example shows how to create a subscription using the Create Subscription API:A successful response returns the For the full list of request parameters and response fields, see the Create Subscription API.
subscription_id and subscription_session_id. Use these values to build the CFSubscriptionSession object in Step 2. The API returns the following response on success:Step 2: Open the payment page Client-side
After you create the subscription, open the payment page so the customer can provide payment details.1. Set up the SDK
The Cashfree iOS SDK is available on CocoaPods. The latest integration version is 2.4.0. The SDK requires iOS 13.0 or higher. You can also integrate via Swift Package Manager (SPM) or a manual framework. See the iOS SDK on GitHub for those options. Add the following to yourPodfile:
pod install to install the dependency.
2. Select a payment method
The Subscription Element SDK supports the following payment methods:- Card
- UPI
- eNach (net banking)
In this flow, the customer enters their card details directly in your application UI. The SDK securely processes the card payment and initiates the subscription mandate. Use
.setChannel("link") on the card builder for the standard link-based card flow.3. Complete the payment
To complete the payment, follow these steps:- Create a
CFSubscriptionSessionobject. - Create the payment object for the selected payment method.
- Set up the payment callback.
- Initiate the payment using
doSubsPayment().
Create a session
TheCFSubscriptionSession object holds the session context for the payment. It accepts the subscription_session_id and subscription_id obtained from Step 1, and the Cashfree environment (.SANDBOX or .PRODUCTION).
Set the environment to .SANDBOX for testing or .PRODUCTION for live payments. The following example shows how to create the session object:
Create a payment object
The SDK provides a dedicated payment builder for each supported payment method. Build only the object that corresponds to the payment method your customer has selected.- Card
- UPI
- eNach (net banking)
Use the following builders to create a card payment object. Collect card details securely from user input. Do not hardcode values in production.
For non-PCI merchants who must not handle raw card numbers, use
CFSubsCardComponent instead of .setCardNumber(). See iOS Custom Card Component.Set up the payment callback
The SDK exposes theCFResponseDelegate protocol to receive callbacks when the subscription payment journey ends. This protocol consists of two methods:
Initiate the payment
CalldoSubsPayment() when the customer taps the pay button. The following example uses subsNBPayment. Replace it with subsCardPayment or subsUpiPayment for your selected payment method.
The following code registers the callback and initiates the subscription payment:
Sample code
The following example shows a complete card payment flow, including session creation, payment object setup, callback registration, and payment initiation.Card payment sample
Card payment sample
The following method demonstrates a complete card payment integration:
iOS Subscription Element integration
iOS Subscription Element integration
Step 3: Confirm the payment Server-side
After the SDK delivers a callback viaverifyPayment, 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.
Use the Fetch Details of All Payments of a Subscription API to retrieve the current payment status.
API request for fetching subscription payments
API request for fetching subscription payments
The following request fetches all payments for a subscription:A successful response returns an array of payment objects. Check the
payment_status field to determine the outcome. The API returns the following response on success:Always verify the subscription status from your backend before delivering goods or services to the customer. Proceed only when
payment_status is SUCCESS.Error codes
If a required field or object is missing when you initiate payment, the SDK returns an error through thecatch block or onError callback. The following table lists common SDK-level validation errors and when they occur.
Show error codes
Show error codes
The SDK validation errors are grouped by category as follows:
Session errors
These errors occur when theCFSubscriptionSession object or its required fields are not provided.| Error code | Message | Description |
|---|---|---|
SUBSCRIPTION_SESSION_OBJECT_MISSING | The “CFSubscriptionSession” is missing in the request. | The CFSubscriptionSession object was not passed to the payment builder. |
SUBSCRIPTION_SESSION_ID_MISSING | The “subscription_session_id” is missing in the request. | The subscription_session_id was not set on the CFSubscriptionSession builder. |
SUBSCRIPTION_ID_MISSING | The “subscription_id” is missing in the request. | The subscription_id was not set on the CFSubscriptionSession builder. |
ENVIRONMENT_MISSING | The “environment” is missing in the request. | The Cashfree environment (.SANDBOX or .PRODUCTION) was not set on the CFSubscriptionSession builder. |
Payment method object errors
These errors occur when a payment method object is absent or one of its required fields is not set.| Error code | Message | Description |
|---|---|---|
SUBSCRIPTION_CARD_OBJECT_MISSING | The “CFCardSubs” object is missing in the request. | The CFCardSubs object was not passed to the CFCardSubsPayment builder. |
SUBSCRIPTION_NB_OBJECT_MISSING | The “CFNetBankingSubs” object is missing in the request. | The CFNetBankingSubs object was not passed to the CFNetbankingSubsPayment builder. |
SUBSCRIPTION_UPI_OBJECT_MISSING | The “CFUPISubs” object is missing in the request. | The CFUPISubs object was not passed to the CFUPISubsPayment builder. |
SUBSCRIPTION_NB_ACCOUNT_HOLDER_NAME_MISSING | The “account holder name” is missing in the request. | The setAccountHolderName field was not set on the CFNetBankingSubs builder. |
SUBSCRIPTION_NB_ACCOUNT_NUMBER_MISSING | The “account number” is missing in the request. | The setAccountNumber field was not set on the CFNetBankingSubs builder. |
SUBSCRIPTION_NB_BANK_CODE_MISSING | The “bank code” is missing in the request. | The setBankAccountCode field was not set on the CFNetBankingSubs builder. |
SUBSCRIPTION_NB_ACCOUNT_TYPE_MISSING | The “account type” is missing in the request. | The setAccountType field was not set on the CFNetBankingSubs builder. |
SUBSCRIPTION_NB_AUTH_MODE_MISSING | The “auth mode” is missing in the request. | The setAuthMode field was not set on the CFNetBankingSubs builder. |
Callback errors
These errors occur when the payment callback is not registered before initiating payment.| Error code | Message | Description |
|---|---|---|
CALLBACK_MISSING | The “callback” cannot be null. | setCallback was not called on CFPaymentGatewayService before doSubsPayment(). Register the callback before you initiate payment. |
Other options
The following optional configurations let you customise the payment screen behaviour.(Optional) Enable full-screen payment
(Optional) Enable full-screen payment
To present the payment flow in full screen, call
setFullScreen(true) on the payment object before you call doSubsPayment():