iOS 手机网站支付转Native支付(WKUIDelegate协议)

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

官方使用方法,UIWebViewDelegate协议
下面我就给大家介绍一下使用WKUIDelegate协议怎么解决手机网站支付转Native支付:

一:sdk导入流程请查看

二:使用说明:
1.在需要调用AlipaySDK的文件中,增加头文件引用。

#import 

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@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) {
                
                NSString *orderInfo = [[AlipaySDK defaultService]fetchOrderInfoFromH5PayUrl:navigationAction.request.URL.absoluteString];
                NSLog(@"orderInfo----%@",orderInfo);
                
                if (orderInfo.length > 0) {
                    // 调用支付接口进行支付
                    [[AlipaySDK defaultService]payUrlOrder:orderInfo fromScheme:@"alisdkdemo" callback:^(NSDictionary* result) {
                        // 处理返回结果
                        NSString* resultCode = result[@"resultCode"];
                        //建议操作: 根据resultCode做处理
                        
                        NSLog(@"%@",resultCode);
                        
                        // returnUrl 代表 第三方App需要跳转的成功页URL
                        NSString* returnUrl = result[@"returnUrl"];
                        
                        NSLog(@"第三方url----%@",returnUrl);
                        //建议操作: 打开returnUrl
                    }];
                    
                }
                
            }];
        }
    } else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

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

#import 

在@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技术交流群:668562416

源码Demo获取方法

关注「网罗开发」公众号 ,有iOS demo、RN 视频以及demo、Android demo等你领取。


iOS 手机网站支付转Native支付(WKUIDelegate协议)_第1张图片

小专栏:https://xiaozhuanlan.com/u/fanbaoying

你可能感兴趣的:(iOS 手机网站支付转Native支付(WKUIDelegate协议))