> ## Documentation Index
> Fetch the complete documentation index at: https://www.cashfree.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# iOS Integration

> Integrate the Cashfree Subscription iOS SDK in your iOS app to authorise mandates and run recurring payments across UPI, cards, and netbanking from one library.

### Setting up SDK  <Badge color="orange">Client-side</Badge>

##### CocoaPods

In your pod file add the following line `pod 'CashfreePG', '2.4.0'`. 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.

```shell theme={"dark"}
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>phonepe</string>
  <string>tez</string>
  <string>paytmmp</string>
  <string>bhim</string>
  <string>amazonpay</string>
  <string>credpay</string>
</array>
```

### Step 1: Creating a subscription <Badge color="green">Server-side</Badge>

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.

<Tip>Subscription creation must happen from your backend (as this API uses your secret key). Please don't call this directly from your mobile application. You can use below-mentioned backend SDKs</Tip>

<AccordionGroup>
  <Accordion title="Node">
    [Creating Subscription from Node SDK](https://github.com/cashfree/cashfree-pg-sdk-nodejs/blob/main/api.ts#L31393)
  </Accordion>

  <Accordion title="Java">
    [Creating Subscription from Java SDK](https://github.com/cashfree/cashfree-pg-sdk-java/blob/main/src/main/java/com/cashfree/pg/Cashfree.java)
  </Accordion>

  <Accordion title="Golang">
    [Creating Subscription from Golang SDK](https://github.com/cashfree/cashfree-pg/blob/main/api_subscription.go#L1288)
  </Accordion>

  <Accordion title="Dotnet">
    [Creating Subscription from Dotnet SDK](https://github.com/cashfree/cashfree-pg-sdk-dotnet/blob/master/src/cashfree_pg/Client/ApiClient.cs#L9375)
  </Accordion>

  <Accordion title="Python">
    [Creating Subscription from Python SDK](https://github.com/cashfree/cashfree-pg-sdk-python/blob/master/cashfree_pg/api_client.py#L8502)
  </Accordion>

  <Accordion title="PHP">
    [Creating Subscription from PHP SDK](https://github.com/cashfree/cashfree-pg-sdk-php)
  </Accordion>
</AccordionGroup>

Once you create subscription, you will get the `subscription_id` and `subscription_session_id`

### Step 2: Opening the subscription checkout payment page <Badge color="orange">Client-side</Badge>

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

<CodeGroup>
  ```swift swift theme={"dark"}
  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)
  }
  ```
</CodeGroup>

#### Create subscription payment object

Use `CFSubscriptionPaymentBuilder` to create the payment object. This object accepts a `CFSubscriptionSession`, like the one created in the previous step.

<CodeGroup>
  ```swift swift theme={"dark"}
  let subscriptionWebCheckoutPayment = try CFSubscriptionPayment.CFSubscriptionPaymentBuilder()
                      .setSession(session)
                      .build()
  ```
</CodeGroup>

#### 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

<CodeGroup>
  ```swift swift theme={"dark"}
  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)
  }
  ```
</CodeGroup>

#### Sample code

<CodeGroup>
  ```swift swift theme={"dark"}
  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)
              }
      }
  }

  ```
</CodeGroup>

### Step 3: Confirming the payment <Badge color="green">Server-side</Badge>

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.

<Note>You must always verify payment status from your backend. Before delivering the goods or services, please ensure you check the subscription status from your backend. Ensure you check the subscription status from your server endpoint.</Note>
