首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获得Hash错配在支付u金钱斯威夫特3

获得Hash错配在支付u金钱斯威夫特3
EN

Stack Overflow用户
提问于 2017-05-24 11:54:47
回答 2查看 1.9K关注 0票数 1

我是斯威夫特的新手,我正在快速集成PayUmoney IOS SDK,当我在现场运行时遇到麻烦--如果我在测试它的显示无效的商业密钥(无效键),我从这里敲了两个星期,做了那么多事情,没有得到任何解决方案,因为任何一个人都可以帮助它,这将是很棒的。下面是我的代码,谢谢。

代码语言:javascript
复制
var params : PUMRequestParams = PUMRequestParams.shared()
var utils : Utils = Utils()

    params.environment = PUMEnvironment.test;
    params.firstname = txtFldName.text;
    params.key = "bZf4AOjj";
    params.merchantid = "5745303";  
    params.logo_url = ""; 
    params.productinfo = "Product Info";
    params.email = txtFldEmail.text;  
    params.phone = ""; 
    params.surl = "https://www.payumoney.com/mobileapp/payumoney/success.php";
    params.furl = "https://www.payumoney.com/mobileapp/payumoney/failure.php";

    if(params.environment == PUMEnvironment.test){
        generateHashForProdAndNavigateToSDK()
    }
    else{
        calculateHashFromServer()
    }
    // assign delegate for payment callback.
    params.delegate = self;
}

func generateHashForProdAndNavigateToSDK() -> Void {
    let txnid = params.txnid!

    let hashSequence : NSString = "\(params.key)|\(txnid)|\(params.amount)|\(params.productinfo)|\(params.firstname)|\(params.email)|||||||||||2uIsGhXWVw" as NSString
    let data :NSString = utils.createSHA512(hashSequence as String!) as NSString
    params.hashValue = data as String!;
    startPaymentFlow();
}

// MARK:HASH CALCULATION

func prepareHashBody()->NSString{
    return "SHA-512key=\(params.key!)&amount=\(params.amount!)&txnid=\(params.txnid!)&productinfo=\(params.productinfo!)&email=\(params.email!)&firstname=\(params.firstname!)" as NSString;
}

func calculateHashFromServer(){
    let config = URLSessionConfiguration.default // Session Configuration
    let session = URLSession(configuration: config) // Load configuration into Session
    let url = URL(string: "https://test.payumoney.com/payment/op/v1/calculateHashForTest")!
    var request = URLRequest(url: url)
    request.httpBody = prepareHashBody().data(using: String.Encoding.utf8.rawValue)
    request.httpMethod = "POST"

    let task = session.dataTask(with: request, completionHandler: {
        (data, response, error) in
        if error != nil {
            print(error!.localizedDescription)
        } else {
            do {
                if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any]{

                    print(json)
                    let status : NSNumber = json["status"] as! NSNumber
                    if(status.intValue == 0)
                    {
                        self.params.hashValue = json["result"] as! String!
                        OperationQueue.main.addOperation {
                            self.startPaymentFlow()
                        }
                    }
                    else{
                        OperationQueue.main.addOperation {
                            self.showAlertViewWithTitle(title: "Message", message: json["message"] as! String)
                        }
                    }
                }
            } catch {
                print("error in JSONSerialization")
            }
        }
    })
    task.resume()
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-26 06:26:27

你好,文尼做的webview它为我工作。在我也使用这个PayUmoney IOS SDK之前,我遇到了很多问题,所以基于objective,我这样做了,所以我认为它对您很有用。创建弱var webview并创建类UIwebviewdelegate

代码语言:javascript
复制
class PayumoneyViewController: UIViewController, UIWebViewDelegate, UIAlertViewDelegate {

@IBOutlet weak var Webview: UIWebView!

和用于测试使用以下凭据

代码语言:javascript
复制
//test
var merchantKey = "40747T"
var salt = "ur salt"
var PayUBaseUrl = "https://test.payu.in"

活的

代码语言:javascript
复制
//Production

var merchantKey = “xxxxxx”
var salt = “xxxxx”
var PayUBaseUrl = "https://secure.payu.in"
let productInfo = “Myapp” //It can be Project name or anything else
let firstName = “Santoshi”  //Details of user whose is purchasing order
let email = “santoshi@app.com"  //Details of user whose is purchasing order
let phone = "xxxxxxxxx" //Details of user whose is purchasing order
let sUrl = "www.google.com"   //By this URL we match whether payment got success or failure
let fUrl = "www.google.com"   //By this URL we match whether payment got success or failure
let service_provider = "payu_paisa"
var txnid1: String! = ""    //Its an unique id which can give order a specific order number.
let totalPriceAmount = "1.0"

上面的视图是这样的

代码语言:javascript
复制
override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(true)

    initPayment()

}

override func viewWillDisappear(_ animated: Bool) {

    super.viewWillDisappear(true)


}

在视图中是这样做的

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()

Webview.delegate = self

    // Do any additional setup after loading the view.
}

创建支付和生成哈希键

代码语言:javascript
复制
func initPayment() {


    txnid1 = “Myapp\(String(Int(NSDate().timeIntervalSince1970)))"

    //Generating Hash Key
    let hashValue = String.localizedStringWithFormat("%@|%@|%@|%@|%@|%@|||||||||||%@",merchantKey,txnid1,totalPriceAmount,productInfo,firstName,email,salt)
    let hash = self.sha1(string: hashValue)

    let postStr = "txnid="+txnid1+"&key="+merchantKey+"&amount="+totalPriceAmount+"&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))
    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 let error as NSError
    {

        print(error)

    }
}

最后做这个

代码语言:javascript
复制
func sha1(string:String) -> String {
    let cstr = string.cString(using: String.Encoding.utf8)

    let data = NSData(bytes: cstr, length: string.characters.count)
    var digest = [UInt8](repeating: 0, count:Int(CC_SHA512_DIGEST_LENGTH))
    CC_SHA512(data.bytes, CC_LONG(data.length), &digest)
    let hexBytes = digest.map { String(format: "%02x", $0) }
    return hexBytes.joined(separator: "")
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    let requestURL = self.Webview.request?.url
    let requestString:String = (requestURL?.absoluteString)!
    if requestString.contains("https://www.payumoney.com/mobileapp/payumoney/success.php") {
        print("success payment done")
    }else if requestString.contains("https://www.payumoney.com/mobileapp/payumoney/failure.php") {
        print("payment failure")
    }
}

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
    let requestURL = self.Webview.request?.url
    print("WebView failed loading with requestURL: \(requestURL) with error: \(error.localizedDescription) & error code: \(error)")

    if error._code == -1009 || error._code == -1003 {
        showAlertView(userMessage: "Please check your internet connection!")
    }else if error._code == -1001 {
        showAlertView(userMessage: "The request timed out.")
    }

}

func showAlertView(userMessage:String){

}
票数 2
EN

Stack Overflow用户

发布于 2017-07-14 10:49:41

我也面临过同样的问题,我已经解决了这个问题。在我的代码中,这一行生成可选值--> let hashSequence : NSString =NSStringas NSString

从值中删除可选项。

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44157709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档