WKUIDelegate协议 - 手机网站支付转Native支付

1,368 阅读4分钟

一:介绍

为了节约开发成本,很多Native-H5混合App采用手机网站支付的方式去实现支付模块。但手机网站支付的网络依赖比较严重,也通常需要经过更多的验证,这种种原因导致手机网站支付的成功率比Native支付低,对商户的利益造成影响。

官方文档使用的是UIWebViewDelegate协议

二:新旧版本区别

1. 旧版本

获取到的url order info,从h5链接中获取订单串接口函数如下:

- (NSString*)fetchOrderInfoFromH5PayUrl:(NSString*)urlStr;
- (void)payUrlOrder:(NSString *)orderStr
         fromScheme:(NSString *)schemeStr
           callback:(CompletionBlock)completionBlock;

上面两个函数自版本15.4.0起已废弃。

2. 新版本

新版本推荐使用下面方法

/**
 *  从h5链接中获取订单串并支付接口(自版本15.4.0起,推荐使用该接口)
 *
 *  @param urlStr     拦截的 url string
 *
 *  @return YES为成功获取订单信息并发起支付流程;NO为无法获取订单信息,输入url是普通url
 */
- (BOOL)payInterceptorWithUrl:(NSString *)urlStr
                   fromScheme:(NSString *)schemeStr
                     callback:(CompletionBlock)completionBlock;

旧版本在官方推荐的UIWebViewDelegate协议中使用,暂时没有遇到问题。

如果在WKUIDelegate协议中使用fetchOrderInfoFromH5PayUrl函数,获得的获取到的url order info为空,无法调起支付。

下面我就给大家介绍一下使用WKUIDelegate协议怎么解决手机网站支付转Native支付。

三:SDK导入流程

1.下载sdk

把iOS包中的压缩文件中以下文件拷贝到项目文件夹下,并导入到项目工程中。

AlipaySDK.bundle
AlipaySDK.framework

2.添加依赖

在Build Phases选项卡的Link Binary With Libraries中,增加以下依赖:

h5alipay.png

注意:

如果是Xcode 7.0之后的版本,需要添加libc++.tbd、libz.tbd。

如果是Xcode 7.0之前的版本,需要添加libc++.dylib、libz.dylib。

四:使用说明:

1.头文件引用

在需要调用AlipaySDK的文件中,增加头文件引用。

#import <AlipaySDK/AlipaySDK.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<WKNavigationDelegate,WKUIDelegate>

@property (weak, nonatomic) WKWebView *webView;
//进度条
@property (weak, nonatomic) CALayer *progresslayer;

@end

//初始化

- (void)stopRunning{
    
    WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    
    [self.view addSubview:webView];
    self.webView = webView;
    
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
    
    //添加属性监听
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    //进度条
    UIView *progress = [[UIView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), 3)];
    progress.backgroundColor = [UIColor clearColor];
    [self.view addSubview:progress];
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, 0, 3);
    layer.backgroundColor = [UIColor blueColor].CGColor;
    [progress.layer addSublayer:layer];
    self.progresslayer = layer;
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_customLabel.text]]];
     
}

2.调用接口

[[AlipaySDK defaultService]fetchOrderInfoFromH5PayUrl:url]

3.实现WKUIDelegate协议,拦截H5的URL

如果返回的resultCode为9000,接入方可以提示用户支付成功;

返回结果不是9000的情况,无需做任何处理。如果returnUrl不为空,建议接入方跳转到该returnUrl。

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
    
    NSLog(@"%@",navigationAction.request.URL.absoluteString);
    
    if ([navigationAction.request.URL.absoluteString hasPrefix:@"alipay://alipayclient/"]) {
        decisionHandler(WKNavigationActionPolicyCancel);
        
        if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
            [[UIApplication sharedApplication] openURL:navigationAction.request.URL options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
                
                __weak APWebViewController* wself = self;
                BOOL isIntercepted = [[AlipaySDK defaultService] payInterceptorWithUrl:[navigationAction.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) {
                    
                    NSLog(@"URL非支付宝支付URL");
                }
                
                
            }];
        }
    } else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

4.支付结果回调处理

支付宝客户端返回url处理方法,在AppDelegate.m文件中,增加头文件引用

#import <AlipaySDK/AlipaySDK.h>

在@implementation AppDelegate中增加如下代码:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
 
    //如果极简开发包不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给开发包
    if ([url.host isEqualToString:@"safepay"]) {
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
    //【由于在跳转支付宝客户端支付的过程中,商户app在后台很可能被系统kill了,所以pay接口的callback就会失效,请商户对standbyCallback返回的回调结果进行处理,就是在这个方法里面处理跟callback一样的逻辑】
            NSLog(@"result = %@",resultDic);
        }];
    }
    if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回authCode
  
        [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {
            //【由于在跳转支付宝客户端支付的过程中,商户app在后台很可能被系统kill了,所以pay接口的callback就会失效,请商户对standbyCallback返回的回调结果进行处理,就是在这个方法里面处理跟callback一样的逻辑】
            NSLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}

关注 【网罗开发】微信公众号,网罗天下方法,方便你我开发,更多iOS技术干货等待领取,所有文档会持续更新,欢迎关注一起成长!

希望可以帮助大家,如有问题可加QQ群: 668562416 交流

如果哪里有什么不对或者不足的地方,还望读者多多提意见或建议

如需转载请联系我,经过授权方可转载,谢谢