iOS H5微信支付总结
2019-12-30 本文已影响0人
劉光軍_MVP
功能描述
- 1、webView加载H5页面
- 2、点击微信支付,调起微信客户端支付
- 3、支付完成,返回APP
操作流程
-
1、调起微信的项目设置
选中‘TARGETS’一栏,在‘info’中的‘LSApplicationQueriesSchemes’添加‘weixin’,已添加过的可以忽略此步骤
-
2、H5拦截微信支付请求,跳转微信
-
2.1 WKWebView
// 实际使用时可以拦截weixin://wap/pay前缀的判断
#pragma mark - WKNavigationDelegate
//! WKWeView在每次加载请求前会调用此方法来确认是否进行请求跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// 先打印此方法拦截的所有请求
// NSLog(@"\n ==== %@" ,navigationAction.request.URL.absoluteString);
// decisionHandler(WKNavigationActionPolicyAllow);
// return ;
NSURLRequest *request = navigationAction.request;
NSString *scheme = [request.URL scheme];
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
if ([scheme isEqualToString:@"weixin"]) {
decisionHandler(WKNavigationActionPolicyCancel);
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:request.URL];
if (canOpen) {
[[UIApplication sharedApplication] openURL:request.URL];
}
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
decisionHandler(WKNavigationActionPolicyAllow);
}
-
2.2 UIWebView
NSString *reqUrl = request.URL.absoluteString;
if ([reqUrl hasPrefix:@"weixin://"]) {
if([[UIApplication sharedApplication] openURL:[NSURL URLWithString:reqUrl]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reqUrl]];
} else {
[DLLoading DLToolTipInWindow:@"请安装微信"];
return NO;
}
}
-
3、APP端更改redirect_url,实现回到APP
调用支付的时候一定会有一条URL::https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb&redirect_url =**********,通过测试得出支付返回的时候会加载redirect_url,当你的redirect_url是网址的时候,必定会跳转到浏览器,浏览器有个特性就是当你的url是****://的时候,会查找你的schemes(iOS系统应该在跳转浏览器之前自己有了判断是网址才进浏览器,****://直接会调用APP),从而可以跳转到APP,所以要修改这个redict_url的值为你的schemes的值加上://,就实现跳转回APP。
-
3.1 WKWebView
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest *request = navigationAction.request;
NSString *absoluteString = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
// 拦截WKWebView加载的微信支付统一下单链接, 将redirect_url参数修改为唤起自己App的URLScheme
if ([absoluteString hasPrefix:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"] && ![absoluteString hasSuffix:[NSString stringWithFormat:@"redirect_url=a1.company.com://wxpaycallback/"]]) {
decisionHandler(WKNavigationActionPolicyCancel);
NSString *redirectUrl = nil;
if ([absoluteString containsString:@"redirect_url="]) {
NSRange redirectRange = [absoluteString rangeOfString:@"redirect_url"];
redirectUrl = [[absoluteString substringToIndex:redirectRange.location] stringByAppendingString:[NSString stringWithFormat:@"redirect_url=a1.company.com://wxpaycallback/"]];
} else {
redirectUrl = [absoluteString stringByAppendingString:[NSString stringWithFormat:@"redirect_url=a1.company.com://wxpaycallback/"]];
}
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields;
newRequest.URL = [NSURL URLWithString:redirectUrl];
[webView loadRequest:newRequest];
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
-
3.2UIWebView
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
static const NSString*schemeString =@"a1.company.com";
static const NSString*redirectString =@"redirect_url";
//更改微信参数redirect_url
if ([reqUrl hasPrefix:jumpString] && ![reqUrl containsString:changeredirectString]) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSRange redirectRange = [reqUrl rangeOfString:@"redirect_url"];
//更改reidrect_url为scheme地址
NSString *redirectUrl = [[reqUrl substringToIndex:redirectRange.location] stringByAppendingString:changeredirectString];
//记录本来跳转的地址 用于APP回来之后的刷新
NSArray *reloadArr = [reqUrl componentsSeparatedByString:@"redirect_url="];
if (reloadArr.count > 1) {
self.reloadURL = [[reloadArr lastObject] stringByRemovingPercentEncoding];
} else {
self.reloadURL = [NSString stringWithFormat:@"https:%@",schemeString];
}
//发送请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
//referer为空会提示"出现商家参数格式有误,请联系商家解决"
//设置Referer 此地址必须注册到商户后台
[request setValue:@"jxjywechat.cdeledu.com://" forHTTPHeaderField:@"Referer"];
[self.webView loadRequest:request];
});
});
return NO;
}
注:a1.company.com 为微信后台注册二级域名(服务端人员提供)
一级域名也可以,其中一级域名格式www.xxx.com,二级域名格式xxx.xxx.com
[request setValue:a1.company.com :// forHTTPHeaderField: @"Referer"];
这个a1.company.com :// 后面的://一定要加,这个实际是你要跳转的地址,就是你支付后回跳到你的APP的地址
-
4、重新加载正确的url
上一步回来的时候是个白屏,因为你的url是错误的,H5也不能识别,所以我们要重新加载之前保存的正确的url
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *reqUrl = request.URL.absoluteString;
if ([reqUrl isEqualToString:[NSString stringWithFormat:@"%@://",schemeString]]) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.reloadURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[self.webView loadRequest:request];
});
});
return NO;
}
return YES;
}
-
5、URL Types配置
选中‘TARGETS’一栏,在‘info’中的‘URL Types’添加一项,URL Schemes 填写‘a1.company.com’
-
6、支付取消或完成后跳转到APP内
也可以在payResult中执行step4的操作
AppDelegate中实现
// iOS 9.0
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
if ([url.absoluteString containsString:@"account.test.com://"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:url];
}
return YES;
}
WebViewController中实现
在viewDidLoad中监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResult:) name:@"test" object:nil];
- (void)payResult:(NSNotification *)noti {
}
注:在shouldStartLoadWithRequest代理方法中执行的这几个操作 都需要在if中return NO,否则会出现连续跳转的问题