# Integration

## You can integrate with the CheckoutPro mobile SDK in 5 steps:&#x20;

1. [Include the SDK in your app project](#1-include-the-sdk-in-your-app-project-mandatory-step)
2. [Set up the payment hashes](#2-set-up-the-payment-hashes-mandatory-step)
3. [Build the payment parameters](#3-build-the-payment-parameters-mandatory-step)
4. [Initiate the payment](#4-initiate-the-payment-mandatory-step)
5. [Handle the payment completion](#5-handle-the-payment-completion-mandatory-step)
6. [(Optional) Customise the integration](advanced-integration)

## 1.Include the SDK in your app project (Mandatory Step)

The CheckoutPro SDK is offered via CocoaPods. To add the SDK to your app project, include the SDK framework in your podfile.

```
// make sure to add below-mentioned line to use dynamic frameworks
use_frameworks!

// Add this to include our SDK
pod 'PayUIndia-CheckoutPro' 
```

Install dependency using `pod install`command in terminal

Next, add the following imports in the class where you need to initiate a payment

{% tabs %}
{% tab title="Swift" %}
```swift
import PayUCheckoutProKit
import PayUCheckoutProBaseKit
import PayUParamsKit
```
{% endtab %}

{% tab title="Objective-C" %}
```objectivec
#import <PayUCheckoutProKit/PayUCheckoutProKit.h>
#import <PayUCheckoutProBaseKit/PayUCheckoutProBaseKit.h>
#import <PayUParamsKit/PayUParamsKit.h>
```
{% endtab %}
{% endtabs %}

{% hint style="info" %}
Test Merchant key/salt for production environment : 3TnMpV/g0nGFe03

Test Merchant key/salt for test environment : gtKFFx/eCwWELxi
{% endhint %}



## Swift Package Manager Integration

You can integrate PayUIndia-Checkoutpro with your app or SDK in two ways:

1. Via Xcode - Go to File-> Add Package-> [https://github.com/payu-intrepos/PayUCheckoutPro-iOS](https://github.com/payu-intrepos/PayUCheckoutPro-iOS)\

2. Via Package.Swift - Add below line in Package.swift dependencies

&#x20;  **.**<mark style="color:blue;">**package(name: "PayUCheckoutProKit", url: "**</mark>[<mark style="color:blue;">**https://github.com/payu-intrepos/PayUCheckoutPro-iOS**</mark>](https://github.com/payu-intrepos/PayUCheckoutPro-iOS)<mark style="color:blue;">**", from: "4.0.0")**</mark>

**CrashReporter**

In order to receive all the crashes related to our SDKs, add the below-mentioned line your AppDelegate's **didFinishLaunchingWithOptions**.

{% tabs %}
{% tab title="Swift" %}
```swift
PayUCheckoutPro.start()
```
{% endtab %}

{% tab title="Objective-C" %}
```objectivec
[PayUCheckoutPro start];
```
{% endtab %}
{% endtabs %}

## 2. Set up the payment hashes (Mandatory Step)

Detailed document is present [here](set-up-the-payment-hashes-ios).

## 3. Build the payment parameters (Mandatory Step)

To initiate a payment, your app will need to send transactional information to the Checkout Pro SDK. To pass this information, build a payment parameter object as

{% hint style="danger" %}
TransactionId can't have special character and not more than 25 characters.
{% endhint %}

{% tabs %}
{% tab title="Swift" %}
```swift
let paymentParam = PayUPaymentParam(key: <#T##String#>,
                                    transactionId: <#T##String#>,
                                    amount: <#T##String#>,
                                    productInfo: <#T##String#>,
                                    firstName: <#T##String#>,
                                    email: <#T##String#>,
                                    phone: <#T##String#>,
                                    surl: <#T##String#>,
                                    furl: <#T##String#>,
                                    environment: <#T##Environment#> /*.production or .test*/)
                                    
paymentParam.userCredential = <#T##String#> // For saving and fetching user’s saved card
```
{% endtab %}

{% tab title="Objective-C" %}
```objectivec
PayUPaymentParam *paymentParam = [[PayUPaymentParam alloc] initWithKey:<#(NSString * _Nonnull)#>
                                                         transactionId:<#(NSString * _Nonnull)#>
                                                                amount:<#(NSString * _Nonnull)#>
                                                           productInfo:<#(NSString * _Nonnull)#>
                                                             firstName:<#(NSString * _Nonnull)#>
                                                                 email:<#(NSString * _Nonnull)#>
                                                                 phone:<#(NSString * _Nonnull)#>
                                                                  surl:<#(NSString * _Nonnull)#>
                                                                  furl:<#(NSString * _Nonnull)#>
                                                           environment:<#(enum Environment)#> /*EnvironmentProduction or EnvironmentTest*/];

paymentParam.userCredential = <#(NSString)#>; // For saving and fetching use saved card
```
{% endtab %}
{% endtabs %}

### For Recurring Payments(SI)

If you are integrating SI, then create an object of SIParam as mentioned [here](../payuparams/payusiparams).

After creating an object, pass the object as below:

{% tabs %}
{% tab title="Swift" %}
```swift
paymentParam.siParams = siParam
```
{% endtab %}

{% tab title="Objective-C" %}
```objectivec
paymentParam.siParams = siParam;
```
{% endtab %}
{% endtabs %}



### **Payment parameter definition**

| Parameter        | Type                   | Description                                                                                                                                                                           | Data Type & Validation                                                                                                                                                    | Mandatory  |
| ---------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- |
| Key              | String                 | Merchant key received from PayU                                                                                                                                                       | Can not be null or empty                                                                                                                                                  | Yes        |
| Transaction ID   | String                 | It should be unique for each transaction                                                                                                                                              | Can not be null or empty and should be unique for each transaction. It can not contain special characters like :-\_/                                                      | Yes        |
| Amount           | String                 | Total transaction amount                                                                                                                                                              | Can not be null or empty and should be valid double stringified eg, “100.0”                                                                                               | Yes        |
| Product info     | String                 | Information about product                                                                                                                                                             | Can not be null or empty and should be less than 100 Characters.                                                                                                          | Yes        |
| First Name       | String                 | Customer’s first name                                                                                                                                                                 | Can not be null or empty                                                                                                                                                  | Yes        |
| Email            | String                 | Customer’s email id                                                                                                                                                                   | Can not be null or empty                                                                                                                                                  | Yes        |
| Phone            | String                 | Customer’s phone number                                                                                                                                                               | Should be a valid phone number                                                                                                                                            | Yes        |
| SURL             | String                 | When the transaction is successful, PayU will load this url and pass transaction response                                                                                             | Should be a valid URL                                                                                                                                                     | Yes        |
| FURL             | String                 | When the transaction is a failure, PayU will load this url and pass transaction response                                                                                              | Should be a valid URL                                                                                                                                                     | Yes        |
| Environment      | Environment            | Environment of SDK                                                                                                                                                                    | <p>Should be either <br>Swift: Environment.production or Environment.test </p><p>ObjectiveC: EnvironmentProduction or EnvironmentTest</p>                                 | Yes        |
| User Credential  | String                 | This is used for the store card feature. PayU will store cards corresponding to passed user credentials and similarly, user credentials will be used to access previously saved cards | <p>Should be a unique value </p><p>Format : &#x3C;merchantKey>:&#x3C;userId> </p><p>Here,  </p><p>UserId is any id/email/phone number to uniquely identify the user  </p> | No         |
| PayUSIParams     | Object of PayUSIParams | Details can be found [here](../../payuparams/payusiparams#siparam-definition)                                                                                                         | Object of PayUSIParams                                                                                                                                                    | No         |

**Additional Params:**

Additional params are optional params that can be passed to SDK like udf params, static hashes. More details on static hash generation and passing is mentioned in [hash generation section](set-up-the-payment-hashes-ios). Below is list of params that can be passed in additional params -

| Parameter                     | Type    | Description                                                     | Mandatory  |
| ----------------------------- | ------- | --------------------------------------------------------------- | ---------- |
| PaymentParamConstant.udf1     | String  | User defined field, Merchant can store their customer id, etc.  | No         |
| PaymentParamConstant.udf2     | String  | User defined field, Merchant can store their customer id, etc.  | No         |
| PaymentParamConstant.udf3     | String  | User defined field, Merchant can store their customer id, etc.  | No         |
| PaymentParamConstant.udf4     | String  | User defined field, Merchant can store their customer id, etc.  | No         |
| PaymentParamConstant.udf5     | String  | User defined field, Merchant can store their customer id, etc.  | No         |
| Static Hashes                 | String  | Covered in the Hash Generation section                          | No         |
| PaymentParamConstant.sourceId | String  | Source id of sodexo saved card                                  | No         |

{% tabs %}
{% tab title="Swift" %}
```swift
paymentParam.additionalParam[PaymentParamConstant.udf1] = <#T##String#>
paymentParam.additionalParam[PaymentParamConstant.udf2] = <#T##String#>
paymentParam.additionalParam[PaymentParamConstant.udf3] = <#T##String#>
paymentParam.additionalParam[PaymentParamConstant.udf4] = <#T##String#>
paymentParam.additionalParam[PaymentParamConstant.udf5] = <#T##String#>
paymentParam.additionalParam[PaymentParamConstant.sourceId] = <#T##String#>
 
```
{% endtab %}

{% tab title="Objective-C" %}
```objectivec
    paymentParam.additionalParam = [[NSDictionary alloc] initWithObjectsAndKeys:
                                    <#(NSString)#>, PaymentParamConstant.udf1,
                                    <#(NSString)#>, PaymentParamConstant.udf2,
                                    <#(NSString)#>, PaymentParamConstant.udf3,
                                    <#(NSString)#>, PaymentParamConstant.udf4,
                                    <#(NSString)#>, PaymentParamConstant.udf5,
                                    <#(NSString)#>, HashConstant.paymentRelatedDetailForMobileSDK,
                                    <#(NSString)#>, HashConstant.vasForMobileSDK,
                                    <#(NSString)#>, HashConstant.payment,
                                    <#(NSString)#>, PaymentParamConstant.sourceId,
                                    nil];
```
{% endtab %}
{% endtabs %}

## 4. Initiate the payment (Mandatory Step)

Initialise and launch the Checkout Pro SDK by calling the following method from your UIViewController’s subclass

{% tabs %}
{% tab title="Swift" %}
```swift
PayUCheckoutPro.open(on: self, paymentParam: paymentParam, config: <#T##PayUCheckoutProConfig?#>, delegate: self)
```
{% endtab %}

{% tab title="Objective-C" %}
```objectivec
[PayUCheckoutPro openOn:self paymentParam:paymentParam config:<#(PayUCheckoutProConfig * _Nullable)#> delegate:self];
```
{% endtab %}
{% endtabs %}

## &#x20;5.Handle the payment completion (Mandatory Step)

Confirm to PayUCheckoutProDelegate and use these functions to get appropriate callbacks from the SDK:

{% tabs %}
{% tab title="Swift" %}
```swift
/// This function is called when we successfully process the payment
/// - Parameter response: success response
func onPaymentSuccess(response: Any?) {
    
}

/// This function is called when we get failure while processing the payment
/// - Parameter response: failure response
func onPaymentFailure(response: Any?) {
    
}

/// This function is called when the user cancel’s the transaction
/// - Parameter isTxnInitiated: tells whether payment cancelled after reaching bankPage
func onPaymentCancel(isTxnInitiated: Bool) {
    
}

/// This function is called when we encounter some error while fetching payment options or there is some validation error
/// - Parameter error: This contains error information
func onError(_ error: Error?) {
    
}

/// Use this function to provide hashes
/// - Parameters:
///   - param: Dictionary that contains key as HashConstant.hashName & HashConstant.hashString
///   - onCompletion: Once you fetch the hash from server, pass that hash with key as param[HashConstant.hashName]
func generateHash(for param: DictOfString, onCompletion: @escaping PayUHashGenerationCompletion) {
    // Send this string to your backend and append the salt at the end and send the sha512 back to us, do not calculate the hash at your client side, for security is reasons, hash has to be calculated at the server side
    let hashStringWithoutSalt = param[HashConstant.hashString] ?? ""
    // Or you can send below string hashName to your backend and send the sha512 back to us, do not calculate the hash at your client side, for security is reasons, hash has to be calculated at the server side
    let hashName = param[HashConstant.hashName] ?? ""

    // Set the hash in below string which is fetched from your server
    let hashFetchedFromServer = <#T##String#>
    
    onCompletion([hashName : hashFetchedFromServer])
}
```
{% endtab %}

{% tab title="Objective-C" %}
```objectivec
/// This function is called when we successfully process the payment
/// @param response  success response
- (void)onPaymentSuccessWithResponse:(id _Nullable)response {
    
}

/// This function is called when we get failure while processing the payment
/// @param response failure response
- (void)onPaymentFailureWithResponse:(id _Nullable)response {
    
}

/// This function is called when the user cancel’s the transaction
/// @param isTxnInitiated tells whether payment cancelled after reaching bankPage
- (void)onPaymentCancelWithIsTxnInitiated:(BOOL)isTxnInitiated {
    
}

/// This function is called when we encounter some error while fetching payment options or there is some validation error
/// @param error This contains error information
- (void)onError:(NSError * _Nullable)error {
    
}

/// Use this function to provide hashes
/// @param param NSDictionary that contains key as HashConstant.hashName & HashConstant.hashString
/// @param onCompletion Once you fetch the hash from server, pass that hash with key as param[HashConstant.hashName]
- (void)generateHashFor:(NSDictionary<NSString *, NSString *> * _Nonnull)param onCompletion:(void (^ _Nonnull)(NSDictionary<NSString *, NSString *> * _Nonnull))onCompletion {
    // Send below string hashStringWithoutSalt to your backend and append the salt at the end and send the sha512 back to us, do not calculate the hash at your client side, for security is reasons, hash has to be calculated at the server side
    NSString *hashStringWithoutSalt = [param objectForKey:HashConstant.hashString];
    // Or you can send below string hashName to your backend and send the sha512 back to us, do not calculate the hash at your client side, for security is reasons, hash has to be calculated at the server side
    NSString * hashName = [param objectForKey:HashConstant.hashName];
    
    // Set the hash in below string which is fetched from your server
    NSString *hashFetchedFromServer = <#(NSString)#>;
    
    NSDictionary *hashResponseDict = [NSDictionary dictionaryWithObjectsAndKeys:hashFetchedFromServer, hashName, nil];
    onCompletion(hashResponseDict);
}
```
{% endtab %}
{% endtabs %}

###

### For UPI Intent (Optional Step)

Currently we support only PhonePe and GooglePay via Intent. Add the query schemes in info.plist&#x20;

```objectivec
<key>LSApplicationQueriesSchemes</key>
<array>
<string>phonepe</string>
<string>tez</string>
<string>paytm</string>
</array>
```

### Distributing your app (App Store / Ad-hoc) (Mandatory Step)

What you get by default is a fat framework that allows you to test your app seamlessly on device as well as simulator. But before archiving your app, you need to remove simulator slices from the framework. Please see the [doc](../../releasing-to-apple) which assists in archiving your app with PayUChekoutPro integrated.

