RN里接入微信的H5支付ios和android的一些配置(主要是在webview中手动设置referer)
anroid 的配置
修改RNCWebViewManager.java 文件 路径/node_modules/react-native-webview/android/src/main/java/com/reactnativecommunity/RNCWebViewManager.java
1. 定义refererUrl
添加 public static String refererUrl = null;
2.添加Referer
删除
root.loadUrl(args.getString(0));
添加
HashMapheaderMap=newHashMap<>();
if(refererUrl!=null) {
headerMap.put("Referer", refererUrl);
}
root.loadUrl(args.getString(0), headerMap);
3设置我们的referer
refererUrl = activeUrl;
ios的配置
1.info.plist配置scheme
需要将微信H5支付的安全域名配置成scheme,不设置会报商家参数格式不对
2.设置我们的referer
修改RNCWebView.m 文件 路径/node_modules/react-native-webview/ios/RNCWebView.m
在webView:(WKWebView *)webViewdecidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler的方法中添加
//修改跳转链接header(Referer,以及重定向跳转微信的 URL)
if ([absoluteString hasPrefix:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"]&& ![absoluteString hasSuffix:[NSString stringWithFormat:@"redirect_url=www.baidu.com.cn://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=www.baidu.com.cn://wxpaycallback/"]];
} else {
redirectUrl = [absoluteString stringByAppendingString:[NSString stringWithFormat:@"redirect_url=www.baidu.com.cn://wxpaycallback/"]];
}
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields;
newRequest.URL = [NSURL URLWithString:redirectUrl];
[newRequest setValue:@"www.baidu.com.cn://wxpaycallback/" forHTTPHeaderField:@"Referer"];
[_webView loadRequest:newRequest];
return;
}
//拦截重定向的跳转微信的 URL Scheme, 打开微信
if ([absoluteString hasPrefix:@"weixin://"]) {
decisionHandler(WKNavigationActionPolicyAllow);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
} else {
//未安装微信的处理
}
});
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
return;