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:- 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.
Step 1
Create an order
Step 2
Open the payment page
Step 3
Confirm the payment
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.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 For details about how checkout displays this text in a context card, see Contextual Checkout.
checkout_context to the order_tags object in the Create Order request above.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:- Open your project in Xcode.
- Go to File > Add Package Dependencies.
- Enter the repository address: https://github.com/cashfree/core-ios-sdk.git.
- Select the version rule (Recommended: Up to Next Major Version).
-
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 linepod 'CashfreePG', '2.5.3'. Install the package using pod install.
iOS configuration
The SDK usescanOpenURL 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:Web Checkout
Web Checkout
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.
UPI Intent checkout
UPI Intent 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:- Create a CFSession object.
- Create a Web Checkout Payment object.
- Set payment callback.
- 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
UseCFWebCheckoutPayment 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 inviewDidLoad 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
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 todoUPIPayment. 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_idandpayment_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.plistfile. If a scheme is missing, the SDK does not show that app to the customer. For details, see iOS configuration.
- Create a
CFSessionobject. - Implement
CFResponseDelegate. - Initiate the payment using
doUPIPayment.
Create a session
UPI Intent checkout uses the sameCFSession 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.
swift
build() method throws one of the following CashfreeError values if a required field is missing:
ENVIRONMENT_MISSINGPAYMENT_SESSION_ID_MISSINGORDER_ID_MISSING
Implement CFResponseDelegate
ImplementCFResponseDelegate 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. TheCFErrorResponseobject contains thecode,message,status,type, andorderIdproperties. 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.
swift
Initiate the payment
Get theCFPaymentGatewayService 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
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. TheshowAlert method and the yourBackend.verifyOrderStatus call are placeholders for your own code.
swift
How the SDK processes the payment
After you calldoUPIPayment, 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:
- Checks which supported UPI apps are installed on the device. If none are installed, the SDK calls
onErrorimmediately with theno_upi_apps_foundcode and does not display any UI. - 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.
- 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.
- 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).
- 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.
- 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
.SANDBOXenvironment, 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.PRODUCTIONenvironment, the SDK opens the selected app usingUIApplication.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
doUPIPaymentagain with a session that uses the sameorder_idandpayment_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:- Tap the checkout button in your app.
- 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.Show error codes
Show error codes
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 inCFErrorResponse.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.