美文网首页
2021-05-10 iOS H5支付 (微信、支付宝)

2021-05-10 iOS H5支付 (微信、支付宝)

作者: colhy | 来源:发表于2021-05-10 16:47 被阅读0次

1、拦截支付URL

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    MLog(@"decisionHandler ---- %@", navigationAction.request.URL.absoluteString)
    NSString*urlString = navigationAction.request.URL.absoluteString;
    urlString = [urlString stringByRemovingPercentEncoding];
    NSURLRequest *request        = navigationAction.request;
    NSString     *scheme         = [request.URL scheme];
  
    if (![scheme isEqualToString:@"https"] && ![scheme isEqualToString:@"http"]) {
        decisionHandler(WKNavigationActionPolicyCancel);
        NSURL *requestURL = request.URL;
        if ([@"weixin" isEqualToString:scheme]) {
            if (endPayRedirectURL) {
                [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:endPayRedirectURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]];
                endPayRedirectURL = nil;
            }
        } else if ([@"alipay" isEqualToString:scheme]) {
            /*
             替换里面的默认Scheme为自己的Scheme LGKJAliPay
             在Info.plist 增加对应的URLTypes LGKJAliPay
             */
            NSArray *urlBaseArr = [navigationAction.request.URL.absoluteString componentsSeparatedByString:@"?"];
            NSString *urlBaseStr = urlBaseArr.firstObject;
            NSString *urlNeedDecode = urlBaseArr.lastObject;
            NSMutableString *afterDecodeStr = [NSMutableString stringWithString:[urlNeedDecode stringDecoderUrlEncode]];
            NSString *afterHandleStr = [afterDecodeStr stringByReplacingOccurrencesOfString:@"alipays" withString:@"LGKJAliPay"];
            NSString *finalStr = [NSString stringWithFormat:@"%@?%@",urlBaseStr, [afterHandleStr stringUrlEncode]];
            requestURL = [NSURL URLWithString:finalStr];
        }
        BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:requestURL];
        MLog(@"canOpen Url %@ = %@", @(canOpen), requestURL.description)
        if (canOpen) {
            [[UIApplication sharedApplication] openURL:requestURL
                                               options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO}
                                     completionHandler:^(BOOL success) {
            }];
        }
        return;
    }

    decisionHandler(WKNavigationActionPolicyAllow);
}

2、配置Info.plist

a、跳转到微信,支付宝

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>wechat</string>
        <string>weixin</string>
        <string>mqqopensdkapiV4</string>
        <string>alipay</string>
    </array>

b、微信,支付宝跳转回来 设置URLTypes

微信: 拿到支付时的URL的一级域名(看支付时候的URL)

eg:(https://baidu.com/pay/... -> com.baidu.com)

支付宝

替换支付URL里面的默认Scheme为自己的Scheme xxxxAliPay

if ([@"alipay" isEqualToString:scheme]) {
  NSArray *urlBaseArr = [navigationAction.request.URL.absoluteString   componentsSeparatedByString:@"?"];
  NSString *urlBaseStr = urlBaseArr.firstObject;
  NSString *urlNeedDecode = urlBaseArr.lastObject;
  NSMutableString *afterDecodeStr = [NSMutableString stringWithString:[urlNeedDecode stringDecoderUrlEncode]];
  NSString *afterHandleStr = [afterDecodeStr stringByReplacingOccurrencesOfString:@"alipays" withString:@"xxxxAliPay"];
  NSString *finalStr = [NSString stringWithFormat:@"%@?%@",urlBaseStr, [afterHandleStr stringUrlEncode]];
  requestURL = [NSURL URLWithString:finalStr];
}

再把xxxxAliPay配置到URL Types中去。

相关文章

网友评论

      本文标题:2021-05-10 iOS H5支付 (微信、支付宝)

      本文链接:https://www.haomeiwen.com/subject/ciuidltx.html