1、微信支付
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest * request = navigationAction.request;
NSString *urlString = [[navigationAction.request URL] absoluteString];
NSString* scheme = [navigationAction.request URL].scheme;
NSString *absoluteString = [urlString stringByRemovingPercentEncoding];
DLog(@"Current URL is %@",absoluteString);
if ([urlString containsString:@"midweixin"]) {//微信支付中间页Url。保留,用户放弃支付时显示
self.midPayUrlStr = urlString;
}
//xxx[.com](http://mipay.com/)为商户申请H5时提交的授权域名,一级域名
NSString *host = @"aaa[.xxx.com](http://imibaby.mipay.com/)";//aaa为自定义名字,可用来区分App,xxx.com为一级域名,必须保留
static NSString *endPayRedirectURL = nil;
//修改微信支付的redirect_url,如果已有redirect_url,则拼接,没有redirect_url,就新增
if ([absoluteString hasPrefix:@"[https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb](https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb)"] && ![absoluteString hasSuffix:[NSString stringWithFormat:@"redirect_url=%@://",host]]) {
decisionHandler(WKNavigationActionPolicyCancel);
NSString *redirectUrl = nil;
if ([absoluteString containsString:@"redirect_url="]) {
NSRange redirectRange = [absoluteString rangeOfString:@"redirect_url"];
endPayRedirectURL = [absoluteString substringFromIndex:redirectRange.location+redirectRange.length+1];
redirectUrl = [[absoluteString substringToIndex:redirectRange.location] stringByAppendingString:[NSString stringWithFormat:@"redirect_url=%@://",host]];
}else {
redirectUrl = [absoluteString stringByAppendingString:[NSString stringWithFormat:@"&redirect_url=%@://",host]];
}
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
newRequest.allHTTPHeaderFields = request.allHTTPHeaderFields;
newRequest.URL = [NSURL URLWithString:redirectUrl];
[webView loadRequest:newRequest];
return;
}
// 跳转到App
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
decisionHandler(WKNavigationActionPolicyCancel);
if ([scheme isEqualToString:@"weixin"]) {
if (endPayRedirectURL) {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:endPayRedirectURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]];
}
}else if ([scheme isEqualToString:[NSString stringWithFormat:@"%@",host]]) {
}
BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:request.URL];
if (canOpen) {
[[UIApplication sharedApplication] openURL:request.URL];
}
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
本地URL Types 要添加一下授权域名:aaa.xxx.com
2、支付宝支付
第一种方式:支付宝支持拦截+支付二合一的方式,通过拦截H5支付链接,调用SDK接口进行支付
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
__weak APWebViewController* wself = self;
BOOL isIntercepted = [[AlipaySDK defaultService] payInterceptorWithUrl:[request.URL absoluteString] fromScheme:@"alisdkdemo" callback:^(NSDictionary *result) {
// 处理支付结果 NSLog(@"%@", result);
// isProcessUrlPay 代表 支付宝已经处理该URL
if ([result[@"isProcessUrlPay"] boolValue]) {
// returnUrl 代表 第三方App需要跳转的成功页URL
NSString* urlStr = result[@"returnUrl"];
[wself loadWithUrlStr:urlStr];
}
}];
if (isIntercepted) {
return NO;
}
return YES;
}
第二种方式:
1.添加 URL Scheme
2.实现代理方法拦截链接并跳转支付宝
staticconstNSString*CompanyFirstDomainByWeChatRegister =@"www.xxxx.com";
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
NSURLRequest*request = navigationAction.request;
NSString *scheme = [request.URL scheme];
// decode for all URL to avoid url contains some special character so that it wasn't load.
NSString *absoluteString = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
NSLog(@"Current URL is %@",absoluteString);
staticNSString*endPayRedirectURL =nil;
// 解决跳转到本地支付宝App不返回的问题
if([absoluteStringhasPrefix:@"alipays://"] || [absoluteStringhasPrefix:@"alipay://"])
{
NSURL*openedURL = navigationAction.request.URL;
NSString*prefixString =@"alipay://alipayclient/?";
//替换里面的默认Scheme为自己的Scheme
NSString*urlString = [[self rf_URLDecodedString:absoluteString] stringByReplacingOccurrencesOfString:@"alipays"withString:[NSString stringWithFormat:@"%@",CompanyFirstDomainByWeChatRegister]];
if([urlString hasPrefix:prefixString]) {
NSRangerang = [urlString rangeOfString:prefixString];
NSString*subString = [urlString substringFromIndex:rang.length];
NSString*encodedString = [prefixString stringByAppendingString:[self rf_URLEncodedString:subString]];
openedURL = [NSURL URLWithString:encodedString];
}
BOOL isSucc = [[UIApplication sharedApplication] openURL:openedURL];
if(!isSuccss) {
NSLog(@"未安装客户端");
}
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
- (NSString*)rf_URLDecodedString:(NSString*)urlString {
NSString*string = urlString;
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)string, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}
- (NSString*)rf_URLEncodedString:(NSString*)urlString {
NSString*string = urlString;
NSString *encodedString = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}