首选:
保证调起微信的地址:
https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb
? 中没有回调参数 redirect_url ,否则取消支付时永远调起redirect_url中对应的地址,返回到浏览器里;
其次:
找到商户申请H5时提交的授权域名,比如:
https://www.baidu.com
这个时候去配置APP 的schemes ,
注意
,一定要这么配:aaa可以随便写,有识别性就行,保证不跟其他APP的schemes相同就行。
再次:
代码里去实现给Referer赋值,在webview的shouldStartLoadWithRequest中实现,代码如下:
-(BOOL)setReferer:(NSURLRequest *)request
{
__block NSString *urlString = [NSString stringWithFormat:@”%@”,request.URL];
if ([urlString hasPrefix:@“
https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb
”]) {
NSDictionary *header = request.allHTTPHeaderFields;
if (header[@“Referer”] && [header[@“Referer”] isEqualToString:@“
nxh.pay.1an.com
?/”]) { // 如果已经设置过referer
// 允许跳转
return YES;
} else {
// 设置referer
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *urlStr = [NSString stringWithFormat:@"%@", urlString];
// 注意,此处须去掉redirectUrl参数,如果没去掉的话就自己 [写方法删除url中的redirectUrl字段](https://blog.csdn.net/qijian_shashi/article/details/88995399)
/*取掉redrect_url参数及其对应的值*/
NSString *newStr = [[NSString alloc]removeParValue:urlStr Key:@"redirect_url"];
NSURL *newUrl = [NSURL URLWithString:newStr];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
[request setValue:@"nxh.pay.1an.com://" forHTTPHeaderField: @"Referer"];
[self.webView loadRequest:request];
});
});
return NO;
}
}
NSURL *url = request.URL;
// 微信支付协议
if ([url.scheme isEqualToString:@"weixin"]) {
if ([url.host isEqualToString:@"wap"]) {
if ([url.relativePath isEqualToString:@"/pay"]) {
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}
return NO;
}
return YES;
}
最后:
尝试下,会不会成功。