iOS H5 调用微信、支付宝支付无法返回app 解决方案

    最新项目中遇到H5页面中调用微信、支付宝客户端支付的需求,虽然这并不是推荐的做法,但是需求确实存在。。。加载h5页面点击支付调起需要支付的客户端(微信或者支付宝)。

   微信支付解决方案:

   1.首先要设置好你自己项目中的scheme, LSApplicationQueriesSchemes 中添加weixin;

  2.你点击支付的时候,在shouldStartLoadWithRequest回调方法中会获取到https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?。。。。类似这种的连接,这就是你的支付链接,webView加载完成后就可以调起微信/支付宝进行支付了。

   3.支付完成后无论成功失败,都会调转到Safari浏览器中。

   4.要想支付完成后跳回app,我们就需要拦截回调的URL地址,回调的URL中一定不要拼接redirect_url, 不然还是会跳转到浏览器中。

   5.最后把Referer设置成:www.xxx.com://           scheme设置成:www.xxx.com就可以直接返回APP了。(www.xxx.com 这个必须是H5授权的域名)。

   支付宝支付解决方案:

   1.首先要设置好你自己项目中的scheme, LSApplicationQueriesSchemes 中添加alipay;

   2.跟微信支付一样,在shouldStartLoadWithRequest回调中拦截UrlScheme,并替换为项目中设置的UrlScheme。

   代码如下:

//UIwebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    //微信
    NSString *wxPre = @"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?";
    if ([request.URL.absoluteString hasPrefix:wxPre]) {
        //微信支付链接不要拼接redirect_url,如果拼接了还是会返回到浏览器的
        NSString *string1 = request.URL.absoluteString;
        //把redirect_url以及后面的参数全删掉
        NSString *string2 = [string1 stringByReplacingOccurrencesOfString:@"redirect_url=http://www.xxx.com/ad_list.php" withString:@""];
        NSURL *newURL = [NSURL URLWithString:string2];
        //新建webView
        UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectZero];
        [self.view addSubview:web]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            dispatch_async(dispatch_get_main_queue(), ^{

                NSMutableURLRequest* newResult = [NSMutableURLRequest requestWithURL:newURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
                [newResult setHTTPMethod:@"GET"];
                [newResult setValue:@“www.xxx.com://" forHTTPHeaderField: @"Referer"];
                [web loadRequest:newResult];
            });
            
        });
    }

    //支付宝
    NSString *reqUrl = request.URL.absoluteString;
    if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) {
        reqUrl = [self URLDecodeString:reqUrl];
        //替换fromAppUrlScheme 为 本APP的UrlSheme
        NSString *newStr = [self changeScheme:request.URL.absoluteString];
        NSString *encodeValue = [newStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:encodeValue];
        [[UIApplication sharedApplication]openURL:url options:nil completionHandler:nil];
        return NO;
    }

  return YES;
}
//替换UrlScheme
- (NSString *)changeScheme:(NSString *)str{
    NSArray *paramsArr = [str componentsSeparatedByString:@"?"];
    NSDictionary *dict = [self jsonToMessageDict:paramsArr.lastObject];
    NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    mDict[@"fromAppUrlScheme"] = @"your Url Scheme";
    NSString *jsonStr = mDict.mj_JSONString; //MJExtension
    return [NSString stringWithFormat:@"%@?%@",paramsArr.firstObject,jsonStr];
}

- (NSDictionary *)jsonToMessageDict:(id)messageJson{
    NSDictionary *messageDict = [NSJSONSerialization JSONObjectWithData:[messageJson dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
    return messageDict ? : @{};
}

- (NSString *)URLDecodeString:(NSString *)str{
    NSString *decodeString = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return decodeString;
}

 

你可能感兴趣的:(iOS,支付,H5支付)