PayU Money Gateway iOS Swift

后端 未结 2 532
栀梦
栀梦 2021-01-25 21:38

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-integr

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 22:02

    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("")
      }
    }
    

提交回复
热议问题