PayU Money Gateway iOS Swift

我们两清 提交于 2019-12-02 22:18:58

问题


I want to integrate the payU Money sdk in my app using swift2.0

I am using this sdk: https://github.com/payu-intrepos/Documentations/wiki/8.1-NEW-iOS-Seamless-SDK-integration

Where to create the test account for testing.


回答1:


import UIKit

var merchantKey="your live merchant key"
var salt="your live merchant salt"
var PayUBaseUrl="https://secure.payu.in"

class PaymentScreen: UIViewController,UIWebViewDelegate {

  @IBOutlet weak var myWebView: UIWebView!

  override func viewDidLoad() {
    super.viewDidLoad()
    self.initPayment()
  }

  func initPayment() {

    var i = arc4random()

    let amount = "1"
    let productInfo = "Order"
    let firstName = "Sample name"
    let email = "abc@gmail.com"
    let phone = "9999119911"
    let sUrl = "https://www.google.com"
    let fUrl = "https://www.bing.com"
    let service_provider = "payu_paisa"

    let strHash:String = self.sha1(String.localizedStringWithFormat("%d%@", i, NSDate()))

    let rangeOfHello = Range(start: strHash.startIndex,
                             end: strHash.startIndex.advancedBy(20))
    let txnid1 = strHash.substringWithRange(rangeOfHello)

    let hashValue = String.localizedStringWithFormat("%@|%@|%@|%@|%@|%@|||||||||||%@",merchantKey,txnid1,amount,productInfo,firstName,email,salt)

    let hash=self.sha1(hashValue)

    let postStr = "txnid="+txnid1+"&key="+merchantKey+"&amount="+amount+"&productinfo="+productInfo+"&firstname="+firstName+"&email="+email+"&phone="+phone+"&surl="+sUrl+"&furl="+fUrl+"&hash="+hash+"&service_provider="+service_provider

    let url = NSURL(string: String.localizedStringWithFormat("%@/_payment", PayUBaseUrl))

    print("check my url", url, postStr)

    let request = NSMutableURLRequest(URL: url!)

    do {

      let postLength = String.localizedStringWithFormat("%lu",postStr.characters.count)
        request.HTTPMethod = "POST"
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Current-Type")
        request.setValue(postLength, forHTTPHeaderField: "Content-Length")
        request.HTTPBody = postStr.dataUsingEncoding(NSUTF8StringEncoding)
        myWebView.loadRequest(request)
    } catch {

    }

  }

  func webViewDidStartLoad(webView: UIWebView) {

  }

  func webViewDidFinishLoad(webView: UIWebView) {

    let requestURL = self.myWebView.request?.URL
    let requestString:String = (requestURL?.absoluteString)!

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

  func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
    print("double failure")
  }

  func sha1(toEncrypt:String) -> String {
    let data = toEncrypt.dataUsingEncoding(NSUTF8StringEncoding)!
    var digest = [UInt8](count:Int(CC_SHA512_DIGEST_LENGTH), repeatedValue: 0)
    CC_SHA512(data.bytes, CC_LONG(data.length), &digest)
    let hexBytes = digest.map { String(format: "%02x", $0) }
    return hexBytes.joinWithSeparator("")
  }
}



回答2:


// Swift 4
import UIKit
import SystemConfiguration
import Foundation

class PayUMoneyViewController: UIViewController, UIWebViewDelegate, UIAlertViewDelegate {

    @IBOutlet weak var webView: UIWebView!

    var merchantKey = "YOUR_MARCHANT_KEY"
    var salt = "YOUR_SALT_KEY"
    var PayUBaseUrl = "https://secure.payu.in"
    var SUCCESS_URL = "https://www.payumoney.com/payments/guestcheckout/#/success"
    var FAILED_URL = "https://www.PayUmoney.com/mobileapp/PayUmoney/failure.php"

    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    var request = NSMutableURLRequest()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.webView.delegate = self
        self.payPayment()

        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PayUMoneyViewController.back(sender:)))
        self.navigationItem.leftBarButtonItem = newBackButton

    }

     @objc func back(sender: UIBarButtonItem) {


        let alert = UIAlertController(title: "Cancel !", message: "Do you really want to cancel the transaction ?", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: cancelTransaction))
        alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))

        self.present(alert, animated: true, completion: nil)
    }

    func cancelTransaction( action : UIAlertAction)
    {
        _ = navigationController?.popToRootViewController(animated: true)

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func payPayment() {

        var i = arc4random()

        let amount = "1"
        let productInfo = "Transport"
        let firstName = USER_FIRST_NAME // Geet
        let email = USER_EMAIL // geetbasakare@gmail.com
        let phone = USER_MOBILE_NO // 1234567890
        let sUrl = "https://www.google.com"
        let fUrl = "https://www.bing.com"
        let service_provider = "payu_paisa"
        let strHash:String = self.sha1(toEncrypt: String.localizedStringWithFormat("%d%@", i, NSDate()));
        let r1 = strHash.range(of: strHash)!

        // String range to NSRange:
        let n1 = NSRange(r1, in: strHash)
        print((strHash as NSString).substring(with: n1)) //

        // NSRange back to String range:
        let r2 = Range(n1, in: strHash)!
        print(strHash.substring(with: r2))
        let rangeOfHello = Range(n1, in: strHash)!
        let txnid1 = strHash.substring(with: rangeOfHello)
        let hashValue = String.localizedStringWithFormat("%@|%@|%@|%@|%@|%@|||||||||||%@",merchantKey,txnid1,amount,productInfo,firstName,email,salt)

        let hash = self.sha1(toEncrypt: hashValue)

        let postStr = "txnid="+txnid1+"&key="+merchantKey+"&amount="+amount+"&productinfo="+productInfo+"&firstname="+firstName+"&email="+email+"&phone="+phone+"&surl="+sUrl+"&furl="+fUrl+"&hash="+hash+"&service_provider="+service_provider

        let url = NSURL(string: String.localizedStringWithFormat("%@/_payment", PayUBaseUrl))

        print("check my url", url as Any, postStr)

        let request = NSMutableURLRequest(url: url! as URL)

        do {

            let postLength = String.localizedStringWithFormat("%lu",postStr.characters.count)
            request.httpMethod = "POST"
            request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Current-Type")
            request.setValue(postLength, forHTTPHeaderField: "Content-Length")
            request.httpBody = postStr.data(using: String.Encoding.utf8)
            webView.loadRequest(request as URLRequest)
        } catch {
            print(error)

        }

    }
    func webViewDidFinishLoad(_ webView: UIWebView) {

        let requestURL = self.webView.request?.url
        let requestString:String = (requestURL?.absoluteString)!
        if (requestString == SUCCESS_URL) {
            print("success payment done")
        }
        else if (requestString == FAILED_URL) {
            print("payment failure")
        }
    }

    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        print("double failure")
    }

    func sha1(toEncrypt: String) -> String {
        var digest = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
        if let data = toEncrypt.data(using: String.Encoding.utf8) {
            let value =  data as NSData
            CC_SHA512(value.bytes, CC_LONG(data.count), &digest)

        }
        var digestHex = ""
        for index in 0..<Int(CC_SHA512_DIGEST_LENGTH) {
            digestHex += String(format: "%02x", digest[index])
        }

        return digestHex
    }

    /*
    func sha1(toEncrypt:String) -> String {
        var data = toEncrypt.data(using: String.Encoding.utf8)!
        var digest = [UInt8](repeating: 0, count:Int(CC_SHA512_DIGEST_LENGTH))

        _ = data.withUnsafeBytes {messageBytes in
                CC_SHA512(messageBytes, CC_LONG(data.count), &digest)
            }
        let hexBytes = digest.map { String(format: "%02x", $0) }
        return hexBytes.joined(separator: "")
    }
 */
}


来源:https://stackoverflow.com/questions/33908484/payu-money-gateway-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!