1

I'm new on swift Can any one help me to Integrate PayU Money in swift.... I am using this sdk: https://github.com/payu-intrepos/Documentations/wiki/8.1-NEW-iOS-Seamless-SDK-integration

Anurag Mishra
  • 109
  • 1
  • 9
  • 1
    I totally understand that starting something new can be intimidating, but the best way to learn is by doing! You should read the instructions and try it on your own first. You may struggle, but that's part of the learning process. The community will be here to help you when you run into specific problems with your code. Good luck, you can do it! – user3353890 Nov 30 '16 at 10:37

2 Answers2

2

This answer is taken from PayU documentation itself, i am answering here just because it took me hours to implement with their documentation.

Hi i can guide you with NON seamless integration. https://github.com/payu-intrepos/Documentations/wiki/8.-iOS-SDK-integration#nonseamless

In non seamless integration PayU is already providing UI and will handle the card type and all payment process and at the end you will be notified for the status of your transaction with reason if failed and all details.

Download the SDK from Here:https://github.com/payu-intrepos/iOS-SDK-Sample-App/archive/3.8.1.zip

From Sample code copy file from "BusinessLayer" folder.

So i hope you have all required files now we can go further with integration.

You are integrating PayU with swift, as there is no swift SDK is not present from PayU team we have to proceed with Briding to Objective-C . You can find about this here:How to call Objective-C code from Swift

Once header file is created and configured in build setting, import the following Headers of SDK

#import "PayU_iOS_CoreSDK.h"
#import <CommonCrypto/CommonHMAC.h>
#import "PUUIPaymentOptionVC.h"
#import "PUSAWSManager.h"
#import "PUSAWSManager.h"
#import "PUSAHelperClass.h"

Now we are ready to use PayU SDK into our environment/project.

Create new instance of 3 main object used for payment 1)Payment parameters 2)Hash Values 2)Helperclass// to calculate hash value

paste this above your viewDidLoad()

let paymentParam: PayUModelPaymentParams  = PayUModelPaymentParams()
var hashes :PayUModelHashes  = PayUModelHashes()
let PUSAhelper:PUSAHelperClass = PUSAHelperClass()

Here is function i have created for further processing

func continueWithCardPayment()  {

        paymentParam.key = "gtKFFx"
        paymentParam.transactionID = "umangtxn123"
        paymentParam.amount = "100.0"
        paymentParam.productInfo = "Nokia"
        paymentParam.SURL = "https://google.com/"
        paymentParam.FURL = "https://facebook.com/"
        paymentParam.firstName = "Umang"
        paymentParam.email = "umangarya336@gmail.com"
        paymentParam.environment = ENVIRONMENT_MOBILETEST
        paymentParam.udf1 = "udf1"
        paymentParam.udf2 = "udf2"
        paymentParam.udf3 = "udf3"
        paymentParam.udf4 = "udf4"
        paymentParam.udf5 = "udf5"
        paymentParam.offerKey = ""              // Set this property if you want to give offer:
        paymentParam.userCredentials = ""

        PUSAhelper.generateHashFromServer(self.paymentParam) { (hashes, errorString) in
            self.hashes = hashes
            self.paymentParam.hashes = hashes
            self.callPaymentGateway()
        }
    }

    func callPaymentGateway()  {

        let webServiceResponse :PayUWebServiceResponse = PayUWebServiceResponse()

        webServiceResponse.getPayUPaymentRelatedDetailForMobileSDK(paymentParam) { (paymentDetail, errString, extraParam) in

            if errString == nil {

                let payOptionVC: PUUIPaymentOptionVC = loadVC("PUUIMainStoryBoard", strVCId: VC_IDENTIFIER_PAYMENT_OPTION) as! PUUIPaymentOptionVC

                payOptionVC.paymentParam = self.paymentParam
                payOptionVC.paymentRelatedDetail = paymentDetail

                runOnMainThread({
                    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.paymentResponseReceived(_:)), name: kPUUINotiPaymentResponse, object: nil)
                    self.navigationController?.pushViewController(payOptionVC, animated: true)
                })
            }
            else{
                print("Failed to proceed for payment : \(errString)")
            }
        }
    }

There are some My custom function that will through error at your side you copy paste, i am mentioning them here. Do take care of them

1)loadVC("PUUIMainStoryBoard", strVCId: VC_IDENTIFIER_PAYMENT_OPTION) //Loadvc function i have created to load view controller, you have to change it as you call your view controller

2)runOnMainThread({ // This function is for running code on main thread.

I have used all test credentials provided by PayU team you can find more in their doc :https://www.payumoney.com/pdf/PayUMoney-Technical-Integration-Document.pdf

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.paymentResponseReceived(_:)), name: kPUUINotiPaymentResponse, object: nil)

//With this line we are adding notification sent by payment gateway to notify us regarding the status of the payment process, lets cash the notification.

func paymentResponseReceived(notify:NSNotification) {
print(notify)
}

You will get the response in notify.object. You can find more sophisticated language and way at their document:https://github.com/payu-intrepos/Documentations/wiki/8.-iOS-SDK-integration.

Hope this answer may help you.

Community
  • 1
  • 1
Devang Tandel
  • 2,988
  • 1
  • 21
  • 43
  • Hey @Dev_Tandel, should I copy whole PayU folder from sample app to my code? – Nikhil Manapure Dec 28 '17 at 06:42
  • @NikhilManapure - PayU now have published SDK for swift, check it out once. – Devang Tandel Dec 28 '17 at 06:50
  • I needed an non seemless api, I don't need to use my own api and I guess PayU has only seemless api in swift, please correct me if I am wrong. Check [here](https://github.com/payu-intrepos/Documentations/wiki/8.-iOS-SDK-integration) – Nikhil Manapure Dec 28 '17 at 06:53
  • @NikhilManapure - Yes, they have just provided Swift SDK for seamless API, for non seamless API you need to copy the following folders 1.BusinessLayer 2.CB 3.SDK 4.SDKUI – Devang Tandel Dec 28 '17 at 07:15
  • I have an api created by backend team for getting hash from server so I guess I won't need "BusinessLayer" contents and another thing I am confused about do I really need "CB" – Nikhil Manapure Dec 28 '17 at 07:21
  • Yes, if you have hash from server then you will not need "BusinessLayer". CB Stands for Custom Browser, they have used it in their UI. – Devang Tandel Dec 28 '17 at 08:38
  • thanks a lot bro, you are awesome. will ask you if I get any other doubt. – Nikhil Manapure Dec 28 '17 at 09:42
  • Hey is "paymentRelatedDetail" need compulsorily? – Nikhil Manapure Dec 28 '17 at 11:49
  • Is this answer working now or deprecated? I need to integrate non seamless payu money with saved cards feature. – Amber K Sep 03 '18 at 13:09
  • @AmberK - give it a try. I haven’t tested it since long – Devang Tandel Sep 03 '18 at 13:22
  • Hash should be generated from server. Hash keys are used for security. In the answer I have used from the example given by payu. One must not do it. Generate hash from server, payu have provided code for it – Devang Tandel Sep 06 '18 at 06:29
  • Hi @Dev_Tandel, Your answer is correct. However I did struggle. Please edit your answer with an option of generating from salt at frontend as provided in the sample sdk. It will help others :) – Amber K Sep 07 '18 at 13:07
  • CitrusGraphics framework is not found in the git. Any one pls help me. -- https://github.com/citruspay/citruspay-ios-sdk/tree/f69a3c0a6335b78bfade7a6d641b2459e0c40a4e – Rashid KC Sep 19 '18 at 04:18
2

Here is the solutions will working source code, with all steps

First of all download framework from this link and follow the installation steps

https://developer.payumoney.com/ios/

after completing installation steps of frame work here is the Code

import this framework

**

1)import PlugNPlay 2) import CommonCrypto

**

add this function and call it where you need ex like on button or particular events

func continueWithCardPayment()

    {
             var paymentParam = PUMTxnParam()
            paymentParam.key = "your merhcant key"
            paymentParam.merchantid = " merchant id"
            paymentParam.txnID = "xyz"
            paymentParam.phone = "982412345"
            paymentParam.amount = "500"
            paymentParam.productInfo = "Nokia"
            paymentParam.surl = "https://test.payumoney.com/mobileapp/payumoney/success.php"
            paymentParam.furl = "https://test.payumoney.com/mobileapp/payumoney/failure.php"
            paymentParam.firstname = "john"
            paymentParam.email = "john@john.com"
            paymentParam.environment = PUMEnvironment.test
            paymentParam.udf1 = "udf1"
            paymentParam.udf2 = "udf2"
            paymentParam.udf3 = "udf3"
            paymentParam.udf4 = "udf4"
            paymentParam.udf5 = "udf5"
            paymentParam.udf6 = ""
            paymentParam.udf7 = ""
            paymentParam.udf8 = ""
            paymentParam.udf9 = ""
            paymentParam.udf10 = ""
            paymentParam.hashValue = self.getHashForPaymentParams(paymentParam)
           // paymentParam.offerKey = ""              // Set this property if you want to give offer:
           // paymentParam.userCredentials = ""



                PlugNPlay.presentPaymentViewController(withTxnParams: paymentParam, on: self, withCompletionBlock: { paymentResponse, error, extraParam in
                    if error != nil {
                        UIUtility.toastMessage(onScreen: error?.localizedDescription)
                    } else {
                        var message = ""
                        if paymentResponse?["result"] != nil && (paymentResponse?["result"] is [AnyHashable : Any]) {
                            print(paymentResponse!)
                            message = "Hello Asad sucess"
                            //                    message = paymentResponse?["result"]?["error_Message"] as? String ?? ""
                            //                    if message.isEqual(NSNull()) || message.count == 0 || (message == "No Error") {
                            //                        message = paymentResponse?["result"]?["status"] as? String ?? ""
                            //                    }
                        } else {
                            message = paymentResponse?["status"] as? String ?? ""
                        }
                        UIUtility.toastMessage(onScreen: message)
                    }
                })

            //PlugNPlay.presentPaymentViewController(withTxnParams: paymentParam, on: self, withCompletionBlock: )
        }

than add this two function of genrating hash & SHA512

func sha512(_ str: String) -> String {

        let data = str.data(using:.utf8)!
        var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
        data.withUnsafeBytes({
            _ = CC_SHA512($0, CC_LONG(data.count), &digest)
        })
        return digest.map({ String(format: "%02hhx", $0) }).joined(separator: "")
    }
    func getHashForPaymentParams(_ txnParam: PUMTxnParam?) -> String? {
        let salt = "your salt key"
        var hashSequence: String? = nil
        if let key = txnParam?.key, let txnID = txnParam?.txnID, let amount = txnParam?.amount, let productInfo = txnParam?.productInfo, let firstname = txnParam?.firstname, let email = txnParam?.email, let udf1 = txnParam?.udf1, let udf2 = txnParam?.udf2, let udf3 = txnParam?.udf3, let udf4 = txnParam?.udf4, let udf5 = txnParam?.udf5, let udf6 = txnParam?.udf6, let udf7 = txnParam?.udf7, let udf8 = txnParam?.udf8, let udf9 = txnParam?.udf9, let udf10 = txnParam?.udf10 {
            hashSequence = "\(key)|\(txnID)|\(amount)|\(productInfo)|\(firstname)|\(email)|\(udf1)|\(udf2)|\(udf3)|\(udf4)|\(udf5)|\(udf6)|\(udf7)|\(udf8)|\(udf9)|\(udf10)|\(salt)"
        }



        let hash = self.sha512(hashSequence!).description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")

        return hash
    }

run the code now

Asad ali
  • 245
  • 1
  • 2
  • 13