iOS H5微信支付

20190501.jpeg

最近,app嵌入了一個(gè)H5模塊,H5里面加入了微信支付的功能。在發(fā)起微信支付的時(shí)候是通過(guò)weixin://的協(xié)議方式的,在UIWebView中可以正常調(diào)起微信發(fā)起支付,但支付成功會(huì)跳轉(zhuǎn)Safari瀏覽器然后加載支付成功頁(yè)面,WKWebView不能識(shí)別這種協(xié)議,不會(huì)跳轉(zhuǎn)微信。

攔截H5微信支付請(qǐng)求

  1. 配置 URL Schemes,用于跳回App

    在info.plist中添加一個(gè)URL Schemes,用于在微信支付完成返回app。


    WeChateafef43a2d42634f63759992120f9f25.png
  1. 攔截支付請(qǐng)求,設(shè)置返回Url并保存支付成功頁(yè)面

    在WKWebView的 WKNavigationDelegate代理方法中攔截支付請(qǐng)求

    let request = navigationAction.request
    let scheme = request.url?.scheme
    let absoluteString = request.url?.absoluteString.removingPercentEncoding
    DPrint(absoluteString)
    // 設(shè)置返回Url:在Info.plist中設(shè)置的URL Schemes
    let appScheme = ""
    let redirect_url = "redirect_url=zxl.\(appScheme)://"
    // 判斷是否是微信支付請(qǐng)求
    let hasPrefix = absoluteString?.hasPrefix("https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb")
    let hasSuffix = absoluteString?.hasSuffix(redirect_url)
    if hasPrefix! && hasSuffix == false {
        decisionHandler(.cancel)
        var requestUrl = absoluteString
        let range = requestUrl?.range(of: "&redirect_url=")
        if (range != nil) {
            let subRange = (range?.lowerBound)!..<(requestUrl?.endIndex)!
            // 保存支付成功頁(yè)面路徑,支付成功后加載
            WeiXinPayRedirectURL = "https://hlht.echargenet.com/perhlhth5/#/views/rechargeSuccess"
            requestUrl?.removeSubrange(subRange)
        }
        requestUrl = requestUrl! + "&" + redirect_url
        
        // 重新發(fā)送請(qǐng)求
        let newRequest = NSMutableURLRequest(url: URL(string: requestUrl!)!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: TIMEOUT)
        newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields
        webView.load(newRequest as URLRequest)
        return;
    }
  1. 跳轉(zhuǎn)微信發(fā)起支付
if scheme == "weixin" {
     decisionHandler(.cancel)
     if UIApplication.shared.canOpenURL(request.url!) {
            UIApplication.shared.open(request.url!, options: [UIApplication.OpenExternalURLOptionsKey.init(rawValue: "hello") : "world"]) { (isT) in
            DPrint((isT ? "成功" : "失敗"))
        }
    }
}

監(jiān)聽支付完成并加載支付成功頁(yè)面

在AppDelegate中監(jiān)聽微信的支付成功回調(diào),發(fā)送通知(也可以通過(guò)其他方式)。

NotificationCenter.default.post(name: NSNotification.Name(rawValue: WebViewWXPaySuccess), object: "")

監(jiān)聽微信支付成功

/// 微信支付成功
///
/// - Parameter notification: 通知
@objc func webViewWXPaySuccess(_ notification:Notification) {
    DispatchQueue.global().async {
        DispatchQueue.main.async {
           let request = NSMutableURLRequest(url: URL(string: self.WeiXinPayRedirectURL)!)
            request.httpMethod = "GET"
            request.setValue("zxl.**://", forHTTPHeaderField: "Referer")
            self.webView.load(request as URLRequest)
         }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容