Setting Up SDK

CocoaPods
In your pod file add the following line pod 'CashfreePG', '2.2.4' . Ensure you are using the latest version of the SDK. Install the package using pod install. To provide UPI payments on iOS you will also need to enable the following permissions in your app. Open the info.plist file and add the below content.
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>phonepe</string>
  <string>tez</string>
  <string>paytmmp</string>
  <string>bhim</string>
  <string>amazonpay</string>
</array>

Step 1: Creating a Subscription

The first step in the Subscription Checkout integration is to create a subscription. You need to do this before any payment can be processed. You can add an endpoint to your server which creates this subscription and is used for communication with your frontend.
Subscription creation must happen from your backend (as this API uses your secret key). Please do not call this directly from your mobile application. You can use below-mentioned backend SDKs
Once you create subscription, you will get the subscription_id and subscription_session_id

Step 2: Opening the Subscription Checkout Payment Page

Once the subscription is created, the next step is to open the payment page so the customer can make the payment. To complete the payment, we can follow the following steps:
  1. Create a CFSubscriptionSession object.
  2. Create a CFSubscriptionPayment object.
  3. Set payment callback.
  4. Initiate the payment using the payment object created from [step 2]
We go through these step by step below.

Create a Session

This object contains essential information about the subscription, including the subscription session ID (subscription_session_id) and subscription ID (subscription_id) obtained from creation step. It also specifies the environment (sandbox or production).
do {
    let session = try CFSubscriptionSession.CFSubscriptionSessionBuilder()
                    .setEnvironment(CFENVIRONMENT.PRODUCTION) // or CFENVIRONMENT.SANDBOX
                    .setSubscriptionId(subscription_id)
                    .setSubscriptionSessionId(subscription_session_id)
                    .build()
} catch let e {
    let error = e as! CashfreeError
    self.createAlert(title: "Warning", message: error.localizedDescription)
}

Create Subscription Payment Object

Use CFSubscriptionPaymentBuilder to create the payment object. This object accepts a CFSubscriptionSession, like the one created in the previous step.
let subscriptionWebCheckoutPayment = try CFSubscriptionPayment.CFSubscriptionPaymentBuilder()
                    .setSession(session)
                    .build()

Setup callback

You need to set up callback handlers to handle events after payment processing. The callback implements CFResponseDelegate to handle payment responses and errors. It can be initialized in viewDidLoad by calling CFPaymentGatewayService.getInstance().setCallback(self) or call it before calling startSubscription.
  • onError: Handles payment failures by displaying an alert with error details
  • verifyPayment: Called when payment needs merchant verification, shows status alert to user
extension ViewController: CFResponseDelegate {

    func onError(_ error: CFErrorResponse, order_id: String) {
         print("Subscription Error", order_id)
    }
    
    func verifyPayment(order_id: String) {
        print("Subscription verifyPayment", order_id)
    }
}

// Class Variable
let pgService = CFPaymentGatewayService.getInstance()

override func viewDidLoad() {
super.viewDidLoad()
pgService.setCallback(self)
}

Step 3: Sample Code

import UIKit
import CashfreePGUISDK
import CashfreePGCoreSDK
import CashfreePG

class SubscriptionViewController: UIViewController, CFResponseDelegate {
    
    func onError(_ error: CashfreePGCoreSDK.CFErrorResponse, order_id: String) {
        print("Subscription Error", order_id)
    }
    
    @objc func verifyPayment(order_id: String){
        print("Subscription verifyPayment", order_id)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func openSubscriptionCheckoutPage(_ sender: Any) {
        do {
                let session = try CFSubscriptionSession.CFSubscriptionSessionBuilder()
                    .setEnvironment(CFENVIRONMENT.PRODUCTION) // or CFENVIRONMENT.SANDBOX
                    .setSubscriptionId(subscription_id)
                    .setSubscriptionSessionId(subscription_session_id)
                    .build()
                let subscriptionWebCheckoutPayment = try CFSubscriptionPayment.CFSubscriptionPaymentBuilder()
                    .setSession(session)
                    .build()
                let cfPaymentgatewat = CFPaymentGatewayService.getInstance()
                cfPaymentgatewat.setCallback(self)
                try cfPaymentgatewat.startSubscription(subscriptionWebCheckoutPayment, viewController: self)
            } catch let e {
                let err = e as! CashfreeError
                print(err.description)
            }
    }
}

Step 4: Confirming the Payment

After the payment is completed, you need to confirm whether the payment was successful by checking the subscription status. Once the payment finishes, the user will be redirected back to your activity.
You must always verify payment status from your backend. Before delivering the goods or services, please ensure you call check the subscription status from your backend. Ensure you check the subscription status from your server endpoint.