PayU Integration in iOS using swift

1. SignUp with Payu money ici .

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

PayU
test-key

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

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
You can download the required frameworks from this lien.

4. Open your Xcode project. Move PayUMoney-IOS-SDK/Dependencies/CitrusGraphics/Framework/graphics-sdk/CitrusGraphics.framework into your project. And remember to select Add to Targets when moving into Xcode project.
Move ?PayUMoney-IOS-SDK/CoreSDK/PayUMoneyCoreSDK.framework into your project. And Add to Targets when moving into Xcode project.
Move ??PayUMoney-IOS-SDK/PlugNPlay/PlugNPlay.framework into your project. And Add to Targets when moving into 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 Apple o linker error for these files then add “Target Memberships” to these frameworks.

target-membership

6. Add below line in podfile –

# For payu integration
pod ‘PayUmoney_PnP’

# For hash creation
pod ‘CryptoSwift’

7. Run command – installation de pod

8. After pods 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. You can create a new Bridging header manually and add it to Build settings > Objective -C Bridging Header.
OR
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 below code in Bridging Header either at the bottom or at the to of the file –

#import <PlugNPlay/PlugNPlay.h>

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

12. Add below function in you 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. You can call setupPayu() function when you want to start PayU transaction.
Either on 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 tap or button that I created.

@IBAction func onTapBtnPay(_ sender: Any) {

setupPayu()

}

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

Create your own eCommerce mobile app with Knowband’s?constructeur d'applications mobiles and integrate PayU just by following these simple steps.

GTranslate Your license is inactive or expired, please subscribe again!

Akash Sharma

Akash Sharma

I'm Akash Sharma, an engineering graduate and software developer by profession. Challenges fascinate me and I like solving them.

Leave a Reply

Your email address will not be published. Required fields are marked *