0

I am trying to integrate PayU payment gateway in our application. But after successful payment i am not able to get response or status of payment. I am posting my code also. Please help me out from this.

import UIKit

class PayUWebViewController: UIViewController, UIWebViewDelegate {

    static let KEY = "W0weql"
    static let SALT = "Q00w5qtc"
    var params : PUMRequestParams = PUMRequestParams.shared()

    var utils : Utils = Utils()
    @IBOutlet weak var webView: UIWebView!


    override func viewDidLoad() {
        super.viewDidLoad()


        params.amount = "0.50"

        params.environment = PUMEnvironment.production;
        params.firstname = "Akshat";
        params.key = PayUWebViewController.KEY;
        params.merchantid = "1234567";  //Merchant merchantid
        params.logo_url = ""; //Merchant logo_url
        params.productinfo = "Product Info";
        params.email = "akshatrathore@live.com";  //user email
        params.phone = "1234567890"; //user phone
        params.txnid = utils.getRandomString(2)  //set your correct transaction id here
        params.surl = "https://www.google.com";
        params.furl = "https://www.facebook.com";

        params.udf1 = "";
        params.udf2 = "";
        params.udf3 = "";
        params.udf4 = "";
        params.udf5 = "";
        params.udf6 = "";
        params.udf7 = "";
        params.udf8 = "";
        params.udf9 = "";
        params.udf10 = "";

        let hashSequence : NSString = "\(params.key!)|\(params.txnid!)|\(params.amount!)|\(params.productinfo!)|\(params.firstname!)|\(params.email!)|||||||||||\(PayUWebViewController.SALT)" as NSString
        let data :NSString = utils.createSHA512(hashSequence as String!) as NSString

        params.hashValue = data as String!
        params.delegate = self
        makePayURequest()
    }


    func makePayURequest()
    {

        let url = URL(string: "https://secure.payu.in/_payment")!
        var request = URLRequest(url: url)
        let params = "key=\(self.params.key!)&txnid=\(self.params.txnid!)&amount=\(self.params.amount!)&productinfo=\(self.params.productinfo!)&firstname=\(self.params.firstname!)&email=\(self.params.email!)&phone=\(self.params.phone!)&surl=\(self.params.surl!)&furl=\(self.params.furl!)&hash=\(self.params.hashValue!)"
        request.httpBody = params.data(using: String.Encoding.utf8)
        request.httpMethod = "POST"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

        webView.loadRequest(request)


    }



    func webViewDidStartLoad(_ webView: UIWebView) {
    }

    func webViewDidFinishLoad(_ webView: UIWebView) {
        let requestURL = self.webView.request?.url
        let requestString:String = (requestURL?.absoluteString)!


        if requestString == "https://www.google.com" {
            print("success payment done")
        }
        else if requestString == "https://www.facebook.com" {
            print("payment failure")
        }
    }

    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        print(error.localizedDescription)
    }

}
Losiowaty
  • 7,911
  • 2
  • 32
  • 47
  • 1
    I hope that key and salt are random and not actually real values. Be very careful about posting confidential information. – Scriptable Mar 14 '17 at 12:22
  • You should not use a webview here, you should use the API. see this other answer: http://stackoverflow.com/questions/33908484/payu-money-gateway-ios-swift – Scriptable Mar 14 '17 at 12:24
  • @Scriptable Yes these key and salt values are random here and not real one. I have checked this link. Here also web view has been used and no response we are getting. – Akshat Rathore Mar 14 '17 at 12:29
  • oh yes, I would still look at using their API instead. I'm not familiar with PayU myself. The documentation here doesn't mention using a webview: https://github.com/payu-intrepos/PayUMoney-IOS-SDK/wiki or here: https://github.com/payu-intrepos/Documentations/wiki/8.1-NEW-iOS-Seamless-SDK-integration – Scriptable Mar 14 '17 at 12:36

1 Answers1

-1

In my case i have used https://github.com/payu-intrepos/PayUMoney-IOS-SDK example in which you just need to set

 var params : PUMRequestParams = PUMRequestParams.shared()
 var utils : Utils = Utils()

 override func viewDidAppear(_ animated: Bool) {
      params.amount = "0.50"

    params.environment = PUMEnvironment.production;
    params.firstname = "Akshat";
    params.key = PayUWebViewController.KEY;
    params.merchantid = "1234567";  //Merchant merchantid
    params.logo_url = ""; //Merchant logo_url
    params.productinfo = "Product Info";
    params.email = "akshatrathore@live.com";  //user email
    params.phone = "1234567890"; //user phone
    params.txnid = utils.getRandomString(2)  //set your correct transaction id here
    params.surl = "https://www.google.com";
    params.furl = "https://www.facebook.com";

    params.udf1 = "";
    params.udf2 = "";
    params.udf3 = "";
    params.udf4 = "";
    params.udf5 = "";
    params.udf6 = "";
    params.udf7 = "";
    params.udf8 = "";
    params.udf9 = "";
    params.udf10 = "";

    let hashSequence : NSString = "\(params.key!)|\(params.txnid!)|\(params.amount!)|\(params.productinfo!)|\(params.firstname!)|\(params.email!)|||||||||||\(PayUWebViewController.SALT)" as NSString
    let data :NSString = utils.createSHA512(hashSequence as String!) as NSString

    params.hashValue = data as String!
     params.delegate = self;
 }

 func transactionCompleted(withResponse response : NSDictionary,errorDescription error:NSError) -> Void {
     self.dismiss(animated: true){
        self.showAlertViewWithTitle(title: "Message", message: "congrats! Payment is Successful")
       }
  }

func transactinFailed(withResponse response : NSDictionary,errorDescription error:NSError) -> Void {
      self.dismiss(animated: true){
          self.showAlertViewWithTitle(title: "Message", message: "Oops!!! Payment Failed"                    }
  }
Krishna Kumar Thakur
  • 1,456
  • 12
  • 27