微信H5支付并返回APP

 

如何实现:

1、针对iOS9增加白名单

 

2、设置自己项目的URL Scheme,这个必须是H5授权的域名

 

3、实现代码

#import "TuningupPaymentController.h"

@interface TuningupPaymentController () <UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation TuningupPaymentController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setLeftBackButton];
    
    self.webView = [UIWebView new];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
    
    NSURL *url = [NSURL URLWithString:self.urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest setValue:@"http://tp.htaonet.com" forHTTPHeaderField:@"Referer"];//设置商户申请H5时提交的授权域名
    [self.webView loadRequest:mutableRequest];
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //拦截微信下单请求
    NSString *urlStr = request.URL.absoluteString;
    if ([urlStr rangeOfString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?"].location != NSNotFound && ![urlStr containsString:@"t.htaonet.com://"]) {
        //在这个请求的基础上生成新的请求
        urlStr = [urlStr stringByReplacingOccurrencesOfString:@"redirect_url=http://t.htaonet.com/" withString:@"redirect_url=t.htaonet.com://"];
        NSURL *newUrl = [NSURL URLWithString:urlStr];
        NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        [newRequest setValue:@"t.htaonet.com://" forHTTPHeaderField:@"Referer"];//设置授权域名
        [webView loadRequest:newRequest];
        return NO;
    }
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [HELPER showProgressHUDWithText:@"加载中" inView:self.view];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [HELPER hideHUDForView:self.view];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [HELPER hideHUDForView:self.view];
    
    [self setTitleViewTitle:[webView stringByEvaluatingJavaScriptFromString:@"document.title"]];
}


@end

 

另外,如果APP中还有H5支付宝支付的话,需要在支付完成后返回APP只需要再加一层判断即可:

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *urlStr = request.URL.absoluteString;
    if ([urlStr rangeOfString:@"https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?"].location != NSNotFound && ![urlStr containsString:@"t.htaonet.com://"]) {
        //在这个请求的基础上生成新的请求
        urlStr = [urlStr stringByReplacingOccurrencesOfString:@"redirect_url=http://t.htaonet.com/" withString:@"redirect_url=t.htaonet.com://"];
        NSURL *newUrl = [NSURL URLWithString:urlStr];
        NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:newUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        [newRequest setValue:@"t.htaonet.com://" forHTTPHeaderField:@"Referer"];//设置授权域名
        [webView loadRequest:newRequest];
        
        [self foundFinishPayingUI];
        return NO;
    }else if ([urlStr rangeOfString:@"alipay://alipayclient"].location != NSNotFound) {//拦截支付宝URL
        urlStr = [self URLDecodeString:urlStr];
        NSRange range = [urlStr rangeOfString:@"{"];
        NSString *jsonStr = [urlStr substringFromIndex:range.location];
        //JSON字符串转字典
        NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableDictionary *mutableDict = dict.mutableCopy;
        [mutableDict setObject:@"duu" forKey:@"fromAppUrlScheme"];
        //字典转JSON字符串
        NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:mutableDict.copy options:NSJSONWritingPrettyPrinted error:nil];
        NSString *tempStr = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
        //对转成的JSON字符串进行URL编码
        NSString *encodedString = (NSString*) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)tempStr, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
        
        urlStr = [@"alipay://alipayclient/?" stringByAppendingString:encodedString];
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil];
        } else {
            // Fallback on earlier versions
        }
        return NO;
    }
    return YES;
}

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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值