# Quick Start

## [Video Tutorial: iOS SDK Integration](#video-tutorial-ios-sdk-integration)

This video walks you step-by-step through the iOS SDK integration process. Watch it to quickly understand setup, configuration, and key features in action.

{% embed url="<https://drive.google.com/file/d/10qN4Iuy-6dU6-rLNRZNodgjdB_AEHZCk/view?usp=sharing>" %}

## [Installation](#installation)

### [Minimum Requirements](#minimum-requirements)

Before integrating the Checkout SDK, please review the following requirements:

* Since the **minimum supported iOS version** is **14**, the **primary demo application** is implemented using **UIKit framework** (SwiftUI is recommended starting from iOS 15).\
  &#x20;[ottu-ios/Example at main · ottuco/ottu-ios](https://github.com/ottuco/ottu-ios/tree/main/Example?utm_source=chatgpt.com)
* However, there’s also a **minimalistic SwiftUI-based demo app**:\
  [ottu-ios/Example\_SwiftUI at main · ottuco/ottu-ios](https://github.com/ottuco/ottu-ios/tree/main/Example_SwiftUI?utm_source=chatgpt.com)

You can install the SDK via two paths:

* Swift Package Manager (Recommended)
* CocoaPods (Deprecated)

### [Installation with Swift Package Manager (Recommended)](#installation-with-swift-package-manager-recommended)

Add Ottu SDK as a dependency in `Package.swift`:

```swift
dependencies: [
    .package(url: "https://github.com/ottuco/ottu-ios.git", from: "2.1.8")
]
```

Or in Xcode:

1. Open your project
2. Go to **Project > Targets > Package Dependencies**
3. Add `ottu-ios` as a dependency

### [Installation with CocoaPods (Deprecated)](#installation-with-cocoapods-deprecated)

Add the following line to your **Podfile**:

{% code overflow="wrap" %}

```ruby
pod 'ottu_checkout_sdk', :git => 'https://github.com/ottuco/ottu-ios.git', :tag => '2.1.8'
```

{% endcode %}

**Run:**

```bash
pod install
```

{% hint style="success" %}
When ottu\_checkout\_sdk is added to the Podfile, the GitHub repository must also be specified as follows:

{% code overflow="wrap" %}

```bash
pod 'ottu_checkout_sdk', :git => 'https://github.com/ottuco/ottu-ios'
```

{% endcode %}
{% endhint %}

{% hint style="warning" %}
If you see *“could not find compatible versions for pod”*, run:

```bash
pod repo update
```

{% endhint %}

## [SDK Configuration](#sdk-configuration)

Once the SDK is installed, follow these steps to integrate it into your app.

{% stepper %}
{% step %}
**Import the SDK**

In your `ViewController.swift` (or any file responsible for presenting the SDK):

```swift
import ottu_checkout_sdk
```

{% endstep %}

{% step %}
**Conform to** `OttuDelegate`

Your view controller must implement the `OttuDelegate` protocol:

```swift
class ViewController: UIViewController, OttuDelegate 
```

{% endstep %}

{% step %}
**Declare a Checkout Member**

Inside your `ViewController`

```swift
private var checkout: Checkout?
```

{% endstep %}

{% step %}
**Initialize Checkout**

Inside `viewDidLoad`, initialize the Checkout SDK:

{% code overflow="wrap" %}

```swift
do {
    self.checkout = try Checkout(
        displaySettings: PaymentOptionsDisplaySettings(
          mode: PaymentOptionsDisplaySettings.PaymentOptionsDisplayMode.list,
        ),
        sessionId: sessionId,
        merchantId: merchantId,
        apiKey: apiKey,
        delegate: self
    )
} catch let error as LocalizedError {
    print(error)
    return
} catch {
    print("Unexpected error: \(error)")
    return
}
```

{% endcode %}

**Required parameters**

* `sessionId` → ID of the created transaction
* `merchantId` → Same as the domain address in API requests
* `apiKey` → Public API key for authorization
* `delegate` → Callback listener (usually `self`)

**Recommended parameters**

* `formsOfPayment` → Defines available payment methods
* `setupPreload` → Preloads transaction details for faster initialization

> `setupPreload` comes from the transaction creation response.\
> Passing it prevents the SDK from re-fetching transaction details, reducing initialization time by **several seconds**. It is a decoded JSON object to a `TransactionDetails` object.\
> See an example: [MainViewController.swift](https://github.com/ottuco/ottu-ios/blob/main/Example/OttuApp/MainViewController.swift#L259)

The simplest version of the initialization looks like this:

```swift
do {
    self.checkout =
        try Checkout(
            sessionId: sessionId,
            merchantId: merchantId,
            apiKey: apiKey,
            delegate: self
        )
} catch
let error as LocalizedError {
    // display an error here
    return
} catch {
    print("Unexpected error: \(error)")
    return
}

```

{% endstep %}

{% step %}
**Add the Payment View**

Still inside `viewDidLoad`, embed the payment view into your container:

{% code overflow="wrap" %}

```swift
if let paymentVC = self.checkout?.paymentViewController(),
    let paymentView = paymentVC.view {

        self.addChild(paymentVC)
        paymentVC.didMove(toParent: self)
        view.addSubview(paymentView)
    }
```

{% endcode %}
{% endstep %}
{% endstepper %}

> This is a **basic example** that adds the Checkout view without handling dimensions.\
> For proper layout with constraints, refer to the demo app: [OttuPaymentsViewController.swift](https://github.com/ottuco/ottu-ios/blob/main/Example/OttuApp/OttuPaymentsViewController.swift#L91)

## [Callbacks](#callbacks)

In order `ViewController` to be compliant to `OttuDelegate`, add the following functions to `ViewController` class:

{% code overflow="wrap" %}

```swift
func errorCallback(_ data: [String : Any]?) {
    navigationController?.popViewController(animated: true)
    let alert = UIAlertController(title: "Error", message: data?.debugDescription ?? "", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .cancel))
    self.present(alert, animated: true)
}
    
func cancelCallback(_ data: [String : Any]?) {
    var message = ""
       
    if let paymentGatewayInfo = data?["payment_gateway_info"] as? [String : Any],
       let pgName = paymentGatewayInfo["pg_name"] as? String,
       pgName == "kpay" {
           message = paymentGatewayInfo["pg_response"].debugDescription
    } else {
        message = data?.debugDescription ?? ""
    }
        
    navigationController?.popViewController(animated: true)
    let alert = UIAlertController(title: "Canсel", message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .cancel))
    self.present(alert, animated: true)
}
   
func successCallback(_ data: [String : Any]?) {
    navigationController?.popViewController(animated: true)
    let alert = UIAlertController(title: "Success", message: data?.debugDescription ?? "", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .cancel))
    present(alert, animated: true)
}
```

{% endcode %}

* **Error** → Displays an error alert and navigates back
* **Cancel** → Displays cancel reason (custom handling for `kpay`)
* **Success** → Displays success confirmation

> This code describes callbacks to be handled by the parent app.

## [Apple Pay](#apple-pay)&#x20;

If your app should support **Apple Pay**:

1. Enable **Apple Pay capability** in:\
   `Xcode > Targets > Signing & Capabilities`
2. Add **Apple Pay Merchant IDs (MID)** in project settings

## [Advanced Features](#advanced-features)

* [Forms of Payment](https://docs.ottu.com/developer/checkout-sdk/ios#formsofpayment-string-required)
* [Customization Theme](https://docs.ottu.com/developer/checkout-sdk/ios#customization-theme)
* [Setup Preload](https://docs.ottu.com/developer/checkout-sdk/ios#setuppreload-object-optional)
