Skip to main content
The Cashfree iOS SDK integrates the Cashfree Payment Gateway into your iOS app and works with apps that run on iOS 13 and later. The SDK offers two checkout flows: Web Checkout, which uses a WebView, and UPI Intent checkout, which uses a native interface.

Key benefits

The iOS SDK offers the following advantages for your payment integration:
  • Simplified integration: Provides a pre-built SDK that delivers an optimised payment experience for iOS applications.
  • Secure and compliant: Securely handles payment processing with built-in security measures managed by the SDK.
  • Multiple checkout flows: Supports Web Checkout and UPI Intent checkout.
  • Multiple integration methods: Supports both Swift Package Manager (recommended) and CocoaPods for easy installation.

Prerequisites

Ensure you complete the following tasks before starting the integration: The iOS integration consists of three essential steps:

Step 1

Create an order

Step 2

Open the payment page

Step 3

Confirm the payment
The step-by-step guide for each step of the integration process is as follows:

Step 1: Create an order Server-side

To integrate the Cashfree Payment Gateway, you must first create an order. Complete this step before you process any payments. Configure an endpoint on your server to handle order creation. You can’t call this API from the client-side.
Create orders through your server as this API requires your secret key. Don’t call it 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.
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 the complete API request and response for /orders in the Create Order API.
To show a short purchase description on checkout, such as a booking reference or route, add checkout_context to the order_tags object in the Create Order request above.
For details about how checkout displays this text in a context card, see Contextual Checkout.

Step 2: Open the payment page Client-side

After you create the order, set up the SDK and use Web Checkout or UPI Intent checkout to accept the payment from your customer.

1. Set up the SDK

Swift Package Manager (recommended)
The recommended way to integrate the Cashfree iOS SDK is by using Swift Package Manager. You can do this through the Xcode interface. To add the Cashfree iOS SDK to your project, follow these steps:
  1. Open your project in Xcode.
  2. Go to File > Add Package Dependencies.
  3. Enter the repository address: https://github.com/cashfree/core-ios-sdk.git.
  4. Select the version rule (Recommended: Up to Next Major Version).
  5. Choose the products you need:
    • CashfreePG: Complete Payment Gateway SDK (recommended)
    • CashfreePGCoreSDK: Core payment processing
    • CashfreePGUISDK: UI components
    • CashfreeAnalyticsSDK: Analytics and tracking
    • CFNetworkSDK: Networking layer
CocoaPods
In your pod file add the following line pod 'CashfreePG', '2.5.3'. Install the package using pod install.
iOS configuration
The SDK uses canOpenURL to check which UPI apps are installed on the customer’s device. iOS allows this check only for the URL schemes that your app declares. A URL scheme is the prefix that opens an app, such as phonepe://. If you do not declare the scheme for a UPI app, the SDK cannot detect that app and does not show it to the customer. Add the following LSApplicationQueriesSchemes entry to your app’s Info.plist file:
Info.plist

2. Select a payment flow

The iOS SDK offers the following payment flows:
In this flow, the SDK opens the Cashfree checkout page in a WebView (an in-app browser view). The customer enters their payment details on this page and completes the payment. The SDK manages all payment logic, UI components, and security measures.To use this flow, see Complete the payment using Web Checkout.
In this flow, the SDK shows a native bottom sheet (a panel at the bottom of the screen) that lists the UPI apps installed on the customer’s device. The customer selects an app, and the SDK opens that app so that the customer can complete the payment. You need only a CFSession object and one method call.To use this flow, see Complete the payment using UPI Intent checkout.

3. Complete the payment using Web Checkout

To complete the payment using Web Checkout, follow these steps:
  1. Create a CFSession object.
  2. Create a Web Checkout Payment object.
  3. Set payment callback.
  4. Initiate the payment using the payment object created.

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).

Create a web checkout payment object

Use CFWebCheckoutPayment to create the payment object. This object accepts a CFSession, like the one created in the previous step.

Setup callback

Set up callback handlers to handle events after payment processing. The callback implements CFResponseDelegate to handle payment responses and errors. Initialise it in viewDidLoad by calling CFPaymentGatewayService.getInstance().setCallback(self).
  • onError: Handles payment failures by displaying an alert with error details
  • verifyPayment: Called when payment needs merchant verification, shows status alert to user
Set the callback in your view controller’s viewDidLoad method so that it is in place before you initiate the payment.

Sample code

4. Complete the payment using UPI Intent checkout

UPI Intent checkout lets the customer pay with any supported UPI app installed on their device. You start the flow with a single call to doUPIPayment. The SDK then shows the list of UPI apps, opens the app that the customer selects, and tracks the payment status. UPI Intent checkout is available to all merchants who use iOS SDK version 2.5.3 or later.
Before you start, complete the following:
  • Create an order on your server and get the order_id and payment_session_id. For details, see Step 1: Create an order.
  • Add Cashfree iOS SDK version 2.5.3 or later to your project. Earlier versions do not include doUPIPayment. For details, see Set up the SDK.
  • Declare the UPI app URL schemes in your Info.plist file. If a scheme is missing, the SDK does not show that app to the customer. For details, see iOS configuration.
To complete the payment using UPI Intent checkout, follow these steps:
  1. Create a CFSession object.
  2. Implement CFResponseDelegate.
  3. Initiate the payment using doUPIPayment.

Create a session

UPI Intent checkout uses the same CFSession object as every other payment mode in the SDK. Build the session with the order ID and payment session ID from Step 1. Set the environment to .SANDBOX for testing or .PRODUCTION for live payments.
Always create the order on your server using the Create Order API. Never generate the order_id or payment_session_id on the device.
swift
The build() method throws one of the following CashfreeError values if a required field is missing:
  • ENVIRONMENT_MISSING
  • PAYMENT_SESSION_ID_MISSING
  • ORDER_ID_MISSING

Implement CFResponseDelegate

Implement CFResponseDelegate in your view controller to receive the result of the payment attempt. The delegate has two methods:
  • onError(_:order_id:): The SDK calls this method when the payment attempt fails. The CFErrorResponse object contains the code, message, status, type, and orderId properties. For all codes, see UPI Intent checkout error codes.
  • verifyPayment(order_id:): The SDK calls this method when it returns control to your app after a payment attempt.
UPI Intent checkout does not provide an onSuccess callback. A call to verifyPayment(order_id:) does not mean that the payment succeeded. It only signals that you should check the payment status. Before you treat the payment as successful, have your server check the order status with the Get Order API. This check applies to every payment mode in the SDK.
swift

Initiate the payment

Get the CFPaymentGatewayService instance, set your view controller as the callback, and call doUPIPayment with the session and the presenting view controller. Set the callback once, for example in viewDidLoad.
swift
UPI Intent checkout does not need a CFPayment or CFUPIPayment object. The session and the view controller are the only inputs.

Sample code

The following example shows a complete UPI Intent checkout integration in a view controller. The showAlert method and the yourBackend.verifyOrderStatus call are placeholders for your own code.
swift

How the SDK processes the payment

After you call doUPIPayment, the SDK manages the rest of the flow. You do not need to write any more code until the SDK calls onError or verifyPayment. The SDK does the following:
  1. Checks which supported UPI apps are installed on the device. If none are installed, the SDK calls onError immediately with the no_upi_apps_found code and does not display any UI.
  2. Displays a bottom sheet with a loading animation while it fetches your checkout configuration, including the order amount, your merchant name, and your preferred UPI app order.
  3. Replaces the loading animation with the list of UPI apps. Your preferred apps appear first, followed by any other installed apps. The order amount and your merchant name appear at the top of the sheet. To set your preferred app order in the Merchant Dashboard or per order in the Create Order API, see UPI App Priority Reordering.
  4. Closes the sheet when the customer selects an app, and opens that app using a UPI intent deep link (a link that opens a specific app).
  5. Displays a result screen while it checks the payment status. If the payment fails, the screen shows a Choose Different UPI option that returns the customer to the app list without fetching the configuration again.
  6. Calls verifyPayment(order_id:) when the payment attempt ends, so that you can confirm the status from your server.

SDK behaviour

The SDK behaves as follows in each environment and when the customer retries:
  • Sandbox and production behaviour: In the .SANDBOX environment, the SDK does not open a real UPI app. Instead, it displays a simulated web authentication screen, which lets you test on the iOS Simulator where you cannot install UPI apps. In the .PRODUCTION environment, the SDK opens the selected app using UIApplication.shared.open.
  • Retries: The Choose Different UPI option lets the customer retry with another app within the same attempt. To retry outside this option, call doUPIPayment again with a session that uses the same order_id and payment_session_id. You do not need to create a new order.

Step 3: Confirm the payment Server-side

After the customer completes the payment, you must confirm the payment status. After the payment finishes, the SDK returns control to your app. 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 before you deliver services to the customer. You can use the Get Order API for this. An order is successful when the order_status is PAID.

Testing

After you integrate the checkout functionality, test each payment flow that you use.

Test Web Checkout

To test Web Checkout, follow these steps:
  1. Tap the checkout button in your app.
  2. Verify that the Cashfree Checkout payment page opens.

Test UPI Intent checkout

To test UPI Intent checkout on the iOS Simulator, set the environment to .SANDBOX. The SDK displays a simulated web authentication screen instead of opening a UPI app.

Error codes

To confirm the error returned in your iOS application, you can view the error codes that are exposed by the SDK.
CashfreeError is an Enum that inherits Foundations Error class. The following are some of the error codes that are exposed by the SDK:

UPI Intent checkout error codes

UPI Intent checkout returns the following values in CFErrorResponse.code when the SDK calls onError:

Affiliate partner program

As a developer building payment experiences for your clients, you can earn additional income while providing them with industry-leading payment solutions.

Join the Cashfree affiliate partner program and get rewarded every time your clients use Cashfree.

What you get:

  • Earn up to 0.25% commission on every transaction.
  • Become a trusted fintech partner for your clients.
  • Access to a dedicated partner manager for expert support.

What your clients get:

  • Instant activation and go live in minutes.
  • Industry-best success rate across all payment modes.
  • Effortless acceptance of international payments in 140+ currencies.

Get started today. Become a partner now.