本文內(nèi)容如標題标锄,會涉及到H5調(diào)原生支付以及在微信强衡、支付寶支付后跳轉(zhuǎn)回本APP內(nèi)容
1集漾、H5調(diào)起微信竖幔、支付寶APP支付
2板乙、支付成功或失敗后的回調(diào)
首先需要再項目工程配置URL Types,路徑“info->URL Types”拳氢,如圖所示
添加后info.plist文件的URL Types所得如圖所示
微信的URL Schemes里面添加的必須為微信開放平臺H5支付的域名募逞,格式為xxx.###.com(大概如此具體問H5開發(fā)要),然后再添加下面代碼
H5調(diào)起微信馋评、支付寶支付原理都一樣放接,都是在WKNavigationDelegate代理方法進行操作
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{}
不同的是微信需要判斷“weixin://"而支付寶需要判斷"alipays://" 、"alipay://"留特,具體代碼如下:
微信
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
decisionHandler(WKNavigationActionPolicyCancel);
if ([scheme isEqualToString:@"weixin"]) {
if (endPayRedirectURL) {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[endPayRedirectURL stringByRemovingPercentEncoding]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30]];
}
}
if ([navigationAction.request.URL.absoluteString hasPrefix:@"weixin://"]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
}
return;
}
其中“ endPayRedirectURL”是微信支付完成或者失敗后回調(diào)需要用到的
微信處理回調(diào)其實就是將傳給微信redirect_url拼接上APP的URL Types所配置的URL Schemes纠脾,如果沒有傳的直接拼接,傳了的需要替換蜕青,具體方法如下
if ([reqUrl hasPrefix:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"] && ![reqUrl hasSuffix:[NSString stringWithFormat:@"redirect_url=%@://",CompanyFirstDomainByWeChatRegister]]) {
decisionHandler(WKNavigationActionPolicyCancel);
NSString *redirectUrl = nil;
if ([reqUrl containsString:@"redirect_url="]) {
NSRange redirectRange = [reqUrl rangeOfString:@"redirect_url"];
endPayRedirectURL = [reqUrl substringFromIndex:redirectRange.location+redirectRange.length+1];
redirectUrl = [[reqUrl substringToIndex:redirectRange.location] stringByAppendingString:[NSString stringWithFormat:@"redirect_url=%@://",CompanyFirstDomainByWeChatRegister]];
}else {
redirectUrl = [reqUrl stringByAppendingString:[NSString stringWithFormat:@"&redirect_url=%@://",CompanyFirstDomainByWeChatRegister]];
}
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
newRequest.allHTTPHeaderFields = navigationAction.request.allHTTPHeaderFields;
[newRequest setValue:[NSString stringWithFormat:@"%@",CompanyFirstDomainByWeChatRegister] forHTTPHeaderField:@"Referer"];
newRequest.URL = [NSURL URLWithString:redirectUrl];
[webView loadRequest:newRequest];
return;
}
支付寶
if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) {
NSURL* alipayURL = [self changeURLSchemeStr:reqUrl];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:alipayURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
}];
} else {
[[UIApplication sharedApplication] openURL:alipayURL];
}
}
支付處理支付成功回調(diào)需要用到兩個方法更改fromAppUrlScheme直接copy就可以
-(NSURL*)changeURLSchemeStr:(NSString*)urlStr{
NSString* tmpUrlStr = urlStr.copy;
if([urlStr containsString:@"fromAppUrlScheme"]) {
tmpUrlStr = [tmpUrlStr stringByRemovingPercentEncoding];
NSDictionary* tmpDic = [self dictionaryWithUrlString:tmpUrlStr];
NSString* tmpValue = [tmpDic valueForKey:@"fromAppUrlScheme"];
tmpUrlStr = [[tmpUrlStr stringByReplacingOccurrencesOfString:tmpValue withString:@"你自己的scheme"] mutableCopy];
tmpUrlStr = [[tmpUrlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] mutableCopy];
}
NSURL * newURl = [NSURL URLWithString:tmpUrlStr];
return newURl;
}
-(NSDictionary*)dictionaryWithUrlString:(NSString*)urlStr{
if(urlStr && urlStr.length&& [urlStr rangeOfString:@"?"].length==1) {
NSArray *array = [urlStr componentsSeparatedByString:@"?"];
if(array && array.count==2) {
NSString*paramsStr = array[1];
if(paramsStr.length) {
NSString* paramterStr = [paramsStr stringByRemovingPercentEncoding];
NSData *jsonData = [paramterStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
return responseDic;
}
}
}
return nil;
}
整個WKNavigationDelegate代碼如下
#pragma mark - WKNavigationDelegate
static const NSString * CompanyFirstDomainByWeChatRegister = @"xxx.###.com";
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString * reqUrl = navigationAction.request.URL.absoluteString;
NSString * scheme = [navigationAction.request.URL scheme];
///打電話
if ([scheme isEqualToString:@"tel"]) {
NSString *resourceSpecifier = [navigationAction.request.URL resourceSpecifier];
NSString *callPhone = [NSString stringWithFormat:@"telprompt:%@", resourceSpecifier];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
}
/// 支付寶
if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) {
NSURL* alipayURL = [self changeURLSchemeStr:reqUrl];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:alipayURL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
}];
} else {
[[UIApplication sharedApplication] openURL:alipayURL];
}
}
/// 微信
if ([reqUrl hasPrefix:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb"] && ![reqUrl hasSuffix:[NSString stringWithFormat:@"redirect_url=%@://",CompanyFirstDomainByWeChatRegister]]) {
decisionHandler(WKNavigationActionPolicyCancel);
NSString *redirectUrl = nil;
if ([reqUrl containsString:@"redirect_url="]) {
NSRange redirectRange = [reqUrl rangeOfString:@"redirect_url"];
endPayRedirectURL = [reqUrl substringFromIndex:redirectRange.location+redirectRange.length+1];
redirectUrl = [[reqUrl substringToIndex:redirectRange.location] stringByAppendingString:[NSString stringWithFormat:@"redirect_url=%@://",CompanyFirstDomainByWeChatRegister]];
}else {
redirectUrl = [reqUrl stringByAppendingString:[NSString stringWithFormat:@"&redirect_url=%@://",CompanyFirstDomainByWeChatRegister]];
}
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:redirectUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
newRequest.allHTTPHeaderFields = navigationAction.request.allHTTPHeaderFields;
[newRequest setValue:[NSString stringWithFormat:@"%@",CompanyFirstDomainByWeChatRegister] forHTTPHeaderField:@"Referer"];
newRequest.URL = [NSURL URLWithString:redirectUrl];
[webView loadRequest:newRequest];
return;
}
if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
decisionHandler(WKNavigationActionPolicyCancel);
if ([scheme isEqualToString:@"weixin"]) {
if (endPayRedirectURL) {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[endPayRedirectURL stringByRemovingPercentEncoding]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30]];
}
}
if ([navigationAction.request.URL.absoluteString hasPrefix:@"weixin://"]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
}
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
如需改正還望不吝賜教