Swift2 PayU Money Gateway iOS Swift

Swift2 PayU Money Gateway iOS Swift,swift2,ios9.1,payu,payumoney,Swift2,Ios9.1,Payu,Payumoney,我想使用swift2.0将payU Money sdk集成到我的应用程序中 我正在使用此sdk: 在何处创建用于测试的测试帐户。//Swift 4 import UIKit var merchantKey="your live merchant key" var salt="your live merchant salt" var PayUBaseUrl="https://secure.payu.in" class PaymentScreen: UIViewController,UIWebV

我想使用swift2.0将payU Money sdk集成到我的应用程序中

我正在使用此sdk:

在何处创建用于测试的测试帐户。

//Swift 4
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("")
  }
}
导入UIKit 导入系统配置 进口基金会 类PayUMoneyViewController:UIViewController、UIWebViewDelegate、UIAlertViewDelegate{ @ibvar-webView:UIWebView! var merchantKey=“您的\u MARCHANT\u密钥” var salt=“你的盐钥匙” var PayUBaseUrl=”https://secure.payu.in" var SUCCESS_URL=”https://www.payumoney.com/payments/guestcheckout/#/success" var失败\u URL=”https://www.PayUmoney.com/mobileapp/PayUmoney/failure.php" @IBOutlet弱var活动指示器:UIActivityIndicator视图! var request=NSMutableURLRequest() 重写func viewDidLoad(){ super.viewDidLoad() self.webView.delegate=self 自我支付 self.navigationItem.hidesBackButton=true let newBackButton=uiBarButtonim(标题:“Back”,样式:uiBarButtonimStyle.plain,目标:self,操作:#选择器(PayUMoneyViewController.Back(发件人:)) self.navigationItem.LeftBarButtonim=newBackButton } @objc func back(发送方:UIBarButtonItem){ let alert=UIAlertController(标题:“取消!”,消息:“是否确实要取消事务?”,首选样式:UIAlertControllerStyle.alert) addAction(UIAlertAction(标题:“是”,样式:UIAlertActionStyle.default,处理程序:cancelTransaction)) addAction(UIAlertAction(标题:“否”,样式:UIAlertActionStyle.default,处理程序:nil)) self.present(警报、动画:true、完成:nil) } func cancelTransaction(操作:UIAlertAction) { _=导航控制器?.popToRootViewController(动画:true) } 重写函数didReceiveMemoryWarning(){ 超级。我收到了记忆警告() //处置所有可以重新创建的资源。 } func payPayment(){ var i=arc4random() 让amount=“1” 让productInfo=“运输” 让firstName=USER\u FIRST\u NAME//Geet 让email=USER\u email//geetbasakare@gmail.com let phone=用户\手机\号码//1234567890 让sUrl=”https://www.google.com" 让我们卷起“https://www.bing.com" 让服务提供商=“付费” 设strHash:String=self.sha1(toEncrypt:String.localizedStringWithFormat(“%d%@”,i,NSDate()); 设r1=strHash.range(of:strHash)! //到NSRange的字符串范围: 设n1=NSRange(r1,in:strHash) 打印((strHash作为NSString)。子字符串(带:n1))// //NSRange返回字符串范围: 让r2=范围(n1,in:strHash)! 打印(strHash.子字符串(带:r2)) 让rangeOfHello=Range(n1,in:strHash)! 设txnid1=strHash.substring(带:rangeOfHello) 让hashValue=String.localizedStringWithFormat(“%@%@%@%@%@%@%@%@%@%@%@%@| | | | | | | | | |%@”、商品密钥、txnid1、金额、产品信息、名字、电子邮件、盐) 让hash=self.sha1(toEncrypt:hashValue) 让postStr=“txnid=“+txnid1+”&key=“+merchantKey+”&amount=“+amount+”&productinfo=“+productinfo+”&firstname=“+firstname+”&email=“+email+”&phone=“+phone+”&surl=“+surl+”&furl=“+furl+”&hash=“+hash+”&service\u provider=“+service\u provider=”服务提供商 让url=NSURL(字符串:string.localizedStringWithFormat(“%@/_payment”,PayUBaseUrl)) 打印(“检查我的url”,如有url,postStr) let request=NSMutableURLRequest(url:url!作为url) 做{ 让postLength=String.localizedStringWithFormat(“%lu”,postStr.characters.count) request.httpMethod=“POST” request.setValue(“application/x-www-form-urlencoded”,forHTTPHeaderField:“当前类型”) request.setValue(postLength,forHTTPHeaderField:“内容长度”) request.httpBody=postStr.data(使用:String.Encoding.utf8) loadRequest(请求为URL请求) }抓住{ 打印(错误) } } func webViewDidFinishLoad(webView:UIWebView){ 让requestURL=self.webView.request?.url 让requestString:String=(requestURL?.absoluteString)! if(requestString==SUCCESS\u URL){ 打印(“成功付款完成”) } else if(requestString==失败的\u URL){ 打印(“付款失败”) } } func网络视图(webView:UIWebView,DidFailLoadWither错误:错误){ 打印(“双重失败”) } func sha1(toEncrypt:String)->String{ 变量摘要=[UInt8](重复:0,计数:Int(抄送摘要长度)) 如果let data=toEncrypt.data(使用:String.Encoding.utf8){ 设值=数据为NSData CC_SHA512(value.bytes、CC_LONG(data.count)和摘要) } var digestHex=“” 对于0..String中的索引{ var data=toEncrypt.data(使用:String.Encoding.utf8)! 变量摘要=[UInt8](重复:0,计数:Int(抄送摘要长度)) _=data.withUnsafeBytes{messageBytes in CC_SHA512(messageBytes、CC_LONG(data.count)和摘要) } 让hexBytes=digest.map{String(格式:'%02x',$0)} 返回十六字节。已加入(分隔符:“”) } */ }
@Anirudh将卡的详细信息放在哪里?当我尝试您的解决方案时,它会显示使用未解析标识符“CC_SHA256_DIGEST_LENGTH”@Anirudh:如何确定支付是否成功/失败?我对您检查containssString()方法表示怀疑。
// 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: "")
    }
 */
}