# iOS SDK

You can easily integrate PayKun in your iOS application with the iOS SDK library and start receiving payment from your customers.

{% hint style="info" %}
**Note:** This framework only supports iOS version 9.0 and later
{% endhint %}

### **Requirements**

* [Merchant ID](https://docs.paykun.com/technical-guide/api-encryption-key-guide)
* [Access Token (Device API key)](https://docs.paykun.com/technical-guide/api-encryption-key-guide)
* Refer the [PayKun Payment Flow.](https://docs.paykun.com/paykun-basic/payment-life-cycle)
* Have a look at the Sample iOS App integrated with PayKun iOS SDK, that contains the default Checkout form. Follow the respective links below for your suitable SDK requirement :

### **Integration Steps**

1. Get Access Token (For Device)
2. Import the Library
3. Initialize the Paykun SDK
4. Pass Payment Parameter and Display Checkout Form
5. Handle Success and Fail Event
6. Get Transaction Details

**Step 1 : Get Access Token (For Device)**

Go to the [guide to generate Merchant ID and Encryption Key](https://docs.paykun.com/technical-guide/api-encryption-key-guide).

**Step 2: Importing the Library**

Follow the instructions given below to import the SDK library to your Swift or Objective-C project :

1. Download and unzip the[ SDK framework](https://github.com/paykun-code/paykun-ios-swift-checkout-sample) directly
2. Open your project in XCode and go to file under Menu. Select Add files to "yourproject"
3. Select PayKun.framework in the directory you just unzipped
4. Select the Copy items if needed check-box
5. Click Add
6. Navigate to Target settings > General and add the Paykun.framework in both Embeded Binaries and Linked Frameworks and Libraries.(below xcode 11)

{% hint style="info" %}
For Xcode 11 and later, ensure that you have the framework added in Frameworks, Libraries, and Embed Content under Target settings - General. Change Embed status from - 'Do not Embed' to 'Embed & Sign'.
{% endhint %}

#### **Step 3: Initialize the Paykun SDK**

To initialize the Paykun SDK, you will need the following:

* Device API key
* Merchant Id
* isLive (true for live environment and false for test environment)

{% tabs %}
{% tab title="Swift" %}

```swift
import Paykun

class ViewController: UIViewController, PaykunCheckoutDelegate {

    var objPaykun: PaykunCheckout!

    override func viewDidLoad() {
        super.viewDidLoad()
        objPaykun = PaykunCheckout.init(key: "YourApiKey", merchantId: "YourMerchantId", isLive: true, andDelegate: self)
    }
}

```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
#import <Paykun/Paykun.h>

@interface ViewController () <PaykunCheckoutDelegate> {
  PaykunCheckout *objPaykun;
}
@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  
  objPaykun = [[PaykunCheckout alloc] initWithKey: @"YourApiKey" merchantId: @"YourMerchantId" isLive: true andDelegate: self];
}

```

{% endtab %}
{% endtabs %}

#### **Step 4: Pass Payment Parameter and Display Checkout Form**

Add the following code to your ViewController or wherever you want to initialize payments:

{% tabs %}
{% tab title="Swift" %}

```swift
func showPaymentCheckout(){
    objPaykun.checkout(withCustomerName: “name”, customerEmail: “email”, customerMobile: “mobile”, productName: “product”, orderNo: “order”, amount: “amount”, viewController: self);
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
- (void)showPaymentCheckout { 
  [objPaykun checkoutWithCustomerName: @“name” customerEmail: @“email” customerMobile: @“mobile” productName: @“product” orderNo: @“order” amount: @“amount” viewController: self];
}

```

{% endtab %}
{% endtabs %}

**Step 5: Handle Success and Fail Event**

You can handle success or failure events when a payment is completed by implementing onPaymentSuccess and onPaymentError methods.

{% tabs %}
{% tab title="Swift" %}

```swift
//Success
func onPaymentSucceed(_ responce: [AnyHashable : Any]) {
    print("responce: \(responce)")
}

//Fail
func onPaymentFailed(_ responce: [AnyHashable : Any]) {
    print("responce: \(responce)")
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
//Success
- (void)onPaymentSucceed:(NSDictionary *)responce {
  NSLog(@"responce: %@",responce);
}

//Fail
- (void)onPaymentFailed:(NSDictionary *)responce {
  NSLog(@"responce: %@",responce);
}
```

{% endtab %}
{% endtabs %}

\
**Step 6: Get Transaction Details**

{% tabs %}
{% tab title="Swift" %}

```swift
objPaykun.getTransactionByPaymentId("PaymentId", block: { responce in
    print("responce: \(responce)")
})
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[objPaykun getTransactionByPaymentId: @"PaymentId" block:^(NSDictionary * _Nonnull responce) {
    NSLog(@"responce: %@",responce);
}];
```

{% endtab %}
{% endtabs %}

### **Test your Integration (sandbox)**

{% hint style="info" %}
If you need it for testing purposes you can use our sandbox mode by sending “isLive” parameter to false.
{% endhint %}

If you need it for testing purposes you should login in Paykun dashboard using sandbox mode and get your testing [merchant Id](https://docs.paykun.com/technical-guide/api-encryption-key-guide) and [Device API key(access token).](https://docs.paykun.com/technical-guide/api-encryption-key-guide)

To test your integration in sandbox environment before going like, you can use our test environment (See how to create one) and use your test credentials, You just have to use **isLive**=**false** in your integration, You can use test card details provided [here ](https://docs.paykun.com/technical-guide/test-card-information)for testing purpose.

* [Log In](https://app.paykun.com/login) to your PayKun Dashboard
* ‘Go to’ the Merchant Account under the ‘Payment Gateway’ section (If you are first-time-login then you will see the ‘Become a Merchant’ option.)
* After the merchant account login, click on your profile picture.
* Select ‘Test mode’.
* Select "Generate Sandbox Account" to activate your sandbox account
* After activating sandbox account,you can click on "Sandbox Login" button to go to sandbox dashboard and use generated username and password login.
* Now you have to generate your Device API key from sandbox account from Setting-Security tab.
* You can get you Merchant Id clicking on profile icon from top right corner.
* Now you have to use this API key and Merchant Id in your Demo project for testing purposes. Note: Remember that you can login in Sandbox account from your Live account.

### **For Live environment**

If you want to do real transaction than set "**isLive**" parameter to **true** and use your Live MerchantId and AccessToken from Paykun dashboard.

### **iOS SDK FAQ**

**Question:** I am getting an error "PayKun contains unsupported Architecture x86\_64" while submitting the archive to the app store. What should I do?

**Answer:** Add below script in Build Phase > Run Script

```bash
FRAMEWORK="PayKun"
FRAMEWORK_EXECUTABLE_PATH="${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/$FRAMEWORK.framework/$FRAMEWORK"
EXTRACTED_ARCHS=()
for ARCH in $ARCHS
do
lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"
EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")
done
lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"
rm "${EXTRACTED_ARCHS[@]}"
rm "$FRAMEWORK_EXECUTABLE_PATH"
mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"
```

### **For any Assistance**

In case of any query, please contact to <tech@paykun.com>

### <br>
