Knowband Blog | Ecommerce Modules

PayU Integration in iOS using Swift

What is PayU?

PayU is an online payment method that is highly popular in the eCommerce industry. The rise in popularity of PayU money is the main reason why every eCommerce store merchant is looking to integrate this payment mode into their store. Thus, PayU is one of the fast & secure payment gateways.

The users can easily complete their transaction process with the PayU payment method. So, Let’s find out the advantages of the PayU payment mode.

Advantages of PayU Money

  1. Zero Setup Cost.
  2. No technical knowledge is required to manage it.
  3. No documentation is required to use PayU money as a payment gateway.

Recommended Read: How to download the Android/iOS app from the Diawi link?

Here is the complete process to integrate PayU in iOS using Swift:

1. SignUp with Payu money here.

2. You can find your PayU Test Key & Salt in Business Dashboard. Check the below screenshots reference.


3. You have to download Payu SDK by using the command “$ git clone –recursive https://github.com/payu-intrepos/PayUMoney-IOS-SDK.git” in the terminal. We need Payu Frameworks from this SDK. However, it may fail sometime but it will work. For instance, for me, it failed 8-9 times.

The general error that you can face :

MacBook-Air:desktop mac$ git clone –recursive https://github.com/payu-intrepos/PayUMoney-IOS-SDK.git

Cloning into ‘PayUMoney-IOS-SDK’…

remote: Enumerating objects: 4, done.

remote: Counting objects: 100% (4/4), done.

remote: Compressing objects: 100% (4/4), done.

remote: Total 2146 (delta 0), reused 0 (delta 0), pack-reused 2142

Receiving objects: 100% (2146/2146), 50.17 MiB | 958.00 KiB/s, done.

Resolving deltas: 100% (892/892), done.

Submodule ‘Dependencies/CitrusGraphics’ (https://github.com/citruspay/citruspay-ios-sdk.git) registered for path ‘Dependencies/CitrusGraphics’

Cloning into ‘/Users/mac/Desktop/PayUMoney-IOS-SDK/Dependencies/CitrusGraphics’…

remote: Enumerating objects: 6013, done.

remote: Counting objects: 100% (6013/6013), done.

remote: Compressing objects: 100% (2573/2573), done.

error: RPC failed; curl 18 transfer closed with outstanding read data remaining

fatal: The remote end hung up unexpectedly

fatal: early EOF

fatal: index-pack failed

fatal: clone of ‘https://github.com/citruspay/citruspay-ios-sdk.git’ into submodule path ‘/Users/mac/Desktop/PayUMoney-IOS-SDK/Dependencies/CitrusGraphics’ failed

Failed to clone ‘Dependencies/CitrusGraphics’. Retry scheduled

Cloning into ‘/Users/mac/Desktop/PayUMoney-IOS-SDK/Dependencies/CitrusGraphics’…

remote: Enumerating objects: 6013, done.

remote: Counting objects: 100% (6013/6013), done.

remote: Compressing objects: 100% (2573/2573), done.

error: RPC failed; curl 18 transfer closed with outstanding read data remaining

fatal: The remote end hung up unexpectedly

fatal: early EOF

fatal: index-pack failed

fatal: clone of ‘https://github.com/citruspay/citruspay-ios-sdk.git’ into submodule path ‘/Users/mac/Desktop/PayUMoney-IOS-SDK/Dependencies/CitrusGraphics’ failed

Failed to clone ‘Dependencies/CitrusGraphics’ a second time, aborting

OR
Also, You can download the required frameworks from this link.

4. Open your Xcode project. Move PayUMoney-IOS-SDK/Dependencies/CitrusGraphics/Framework/graphics-sdk/CitrusGraphics.framework into your project. Then, remember to select Add to Targets when moving into the Xcode project.
Next, move ⁨PayUMoney-IOS-SDK/CoreSDK/PayUMoneyCoreSDK.framework into your project. After that, add to Targets when moving into the Xcode project.
Then, move ⁨⁨PayUMoney-IOS-SDK/PlugNPlay/PlugNPlay.framework into your project. Next, add to Targets when moving into the Xcode project.


5. Note:

If you missed to add targets or you are getting an error that these files are missing or in case you get an Apple o linker error for these files then adds “Target Memberships” to these frameworks.

6. Add the below line in the podfile –

# For payu integration
pod ‘PayUmoney_PnP’

# For hash creation
pod ‘CryptoSwift’

7. Run command – pod install

8. After pod installation is complete make sure that you are using “project.xcworkspace” Xcode project.

Note: Please don’t use “project.xcodeproj” Xcode project.

9. To use Objective C code in Swift we need to create a Bridging header. First, you can create a new Bridging header manually and add it to Build settings > Objective -C Bridging Header.
OR
Also, you can create a new C file ( .m file or .h file). It will automatically create a new bridging header.

10. Generally Bridging Header file will be empty. You can add the below code in Bridging Header either at the bottom or at the top of the file –

#import <PlugNPlay/PlugNPlay.h>

11. Import the CryptoSwift library into your controller.
import CryptoSwift

Note: There are chances that you don’t get “CryptoSwift” in the import suggestion. Don’t worry, you can manually type the same code shown above.

12. The function below in your ViewController class where from which you will initiate the PayU transaction.

func setupPayu()

{

var kMerchantSalt = “your salt”

var txnParam = PUMTxnParam()

PlugNPlay.setDisableCompletionScreen(true)

//Set the parameters

txnParam.phone = “8802444107”

txnParam.email = “asharma@velsof.com”

txnParam.amount = “500”

txnParam.environment = PUMEnvironment.test

txnParam.firstname = “Akash”

txnParam.key = “Your Key”

txnParam.merchantid = “Your Merchant id”

txnParam.txnID = “1234567890”

txnParam.surl = “https://www.payumoney.com/mobileapp/payumoney/success.php”

txnParam.furl = “https://www.payumoney.com/mobileapp/payumoney/failure.php”

txnParam.productInfo = “iPhone7”

txnParam.udf1 = “ud1”

txnParam.udf2 = “ud2”

txnParam.udf3 = “ud3”

txnParam.udf4 = “ud4”

txnParam.udf5 = “ud5”

txnParam.udf6 = “”

txnParam.udf7 = “”

txnParam.udf8 = “”

txnParam.udf9 = “”

txnParam.udf10 = “”

/*

* Below code is used to create a formatted hashString from the Payu Transaction parameters

* **Author:** Asharma

* **Date:** 09-Oct-2018

*/

let hashString = “\(txnParam.key!)|\(txnParam.txnID!)|\(txnParam.amount!)|\(txnParam.productInfo!)|\(txnParam.firstname!)|\(txnParam.email!)|\(txnParam.udf1!)|\(txnParam.udf2!)|\(txnParam.udf3!)|\(txnParam.udf4!)|\(txnParam.udf5!)||||||\(kMerchantSalt)”

/*

* Below code is used to create dynamic hash from hashString

* **Author:** Asharma

* **Date:** 09-Oct-2018

*/

let data = hashString.data(using: .utf8)

txnParam.hashValue = data?.sha512().toHexString()
PlugNPlay.presentPaymentViewController(withTxnParams: txnParam, on: self) { (response, error, extraParam) in

print(“response”)

print(error)

}

}

13. When you want to start a PayU transaction, you can call the setupPayu() function. Then, either on the tap of a button or inside another function.
Example: I have created a button on my project. Created an IBAction of the button. This IBAction is a function that will be called on the tap or button that I created.

@IBAction func onTapBtnPay(_ sender: Any) {

setupPayu()

}

14. You have completed the integration part of PayU integration.

Recommended Read: How to save data to Firebase in swift language?

Create your eCommerce mobile app with Knowband’s mobile app builder and integrate PayU just by following these below simple steps. With KnowBand’s eCommerce Mobile App Builder, you can create and release a pair of fully native Android and iOS apps for your eCommerce website.

You may start selling on mobile devices and draw a global audience to your store with the help of our feature-rich eCommerce Mobile App Builder solution. As a producer of eCommerce mobile apps, Knowband is a well-known brand. Our major goal is to build strong, yet simple to use, eCommerce mobile apps that may provide any eCommerce enterprise with a competitive edge. The eCommerce Mobile App Builder from Knowband enables you to design a user-friendly environment and makes it simpler for e-merchants to reach their consumer base. Every handheld device is totally compatible with the lightweight, straightforward applications offered by the eCommerce mobile app provider.

Suggested Articles: