一. 支付宝 aliPay
alipay://alipayclient/?{"requestType":"SafePay","fromAppUrlScheme":"alipays","dataString":"h5_route_token=\"RZ24aot4PEXC1dhcY8lXk7yXsF2Y63mobilecashierRZ24\"&is_h5_route=\"true\""}
支付宝支付很简单,拦截链接替换 fromAppUrlScheme 对应的值即可, 常规思路是先解码然后把链接转为数组,字典然后替换其中的值,再对相应的部分编码然后拼接,发现太麻烦了。 重新思考需求,核心是”替换”,把未解码的链接当字符串处理行不行?试了一下,success!这应该是最最最简单的方案了。
官方文档:
用户在支付宝APP或H5收银台完成支付后,会根据商户在手机网站支付API中传入的前台回跳地址return_url自动跳转回商户页面,同时在URL请求中以Query String的形式附带上支付结果参数,详细回跳参数见“手机网站支付接口alipay.trade.wap.pay”前台回跳参数。 注意:在ios系统中,唤起支付宝App支付完成后,不会自动回到浏览器或商户APP。用户可手工切回到浏览器或商户APP;支付宝H5收银台会自动跳转回商户return_url指定的页面。
let URLScheme = "*.*.com"
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url as URL?,
let absoluteString = url.absoluteString as String?
else { return }
DDLog(absoluteString)
if absoluteString.contains("alipay://alipayclient") {
let tmp = absoluteString.replacingOccurrences(of: "alipays", with: URLScheme)
UIApplication.openURLString(tmp)
decisionHandler(.allow)
return
}
}
二. 微信 wxPay
@objcMembers class PHHCouponOrderWebController: UIViewController {
///查询结果
var redirect_url: String = ""
// MARK: -lifecycle
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "payReturn"), object: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupExtendedLayout()
title = "*支付"
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(handleAction(_:)))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "查看记录", style: .plain, target: self, action: #selector(handleAction(_:)))
view.backgroundColor = UIColor.background
view.addSubview(webView)
NotificationCenter.default.addObserver(self, selector: #selector(refreshOrderResult), name: NSNotification.Name(rawValue: "payReturn"), object: nil)
}
// MARK: -funtins
func refreshOrderResult() {
guard let urlStr = redirect_url.removingPercentEncoding as String? else { return }
webView.urlString = urlStr
}
}
extension PHHCouponOrderWebController: WKUIDelegate{
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let url = navigationAction.request.url as URL?,
let absoluteString = url.absoluteString as String?
else { return }
if absoluteString.contains("weixin://wap/pay") {
isLoading = false
UIApplication.openURL(url)
decisionHandler(.cancel)
} else if absoluteString.contains("https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?") && !isLoading {
let referer = URLScheme + "://"
let request = NSMutableURLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 20)
request.allHTTPHeaderFields = navigationAction.request.allHTTPHeaderFields;
request.setValue(referer , forHTTPHeaderField: "Referer")
let tmp = URLScheme.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
if absoluteString.contains("redirect_url") {
let range: NSRange = (absoluteString as NSString).range(of: "redirect_url=")
var reqUrl: String = (absoluteString as NSString).substring(to: range.location + range.length)
reqUrl = reqUrl + referer
request.url = URL(string: reqUrl)
redirect_url = (absoluteString as NSString).substring(from: range.location + range.length)
DDLog(reqUrl, redirect_url)
} else {
let reqUrl = "\(absoluteString)&redirect_url=\(tmp)"
request.url = URL(string: reqUrl)
DDLog(reqUrl)
}
webView.load(request as URLRequest)
isLoading = true
decisionHandler(.cancel)
}
else if (absoluteString.contains("https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?")) {
isLoading = false;
decisionHandler(.allow);
}
else {
decisionHandler(.allow);
}
}
}
@implementation AppDelegate
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
//URLScheme
if ([url.absoluteString hasSuffix:@".*.com://"]) {
[NSNotificationCenter.defaultCenter postNotificationName:@"payReturn" object:nil];
}
return YES;
}
注:每次做支付需求,微信支付都比支付宝麻烦,建议大家先弄支付宝。