美文网首页iOS支付系统
iOS AliPay(支付宝)支付

iOS AliPay(支付宝)支付

作者: 马威明 | 来源:发表于2018-09-20 17:44 被阅读414次

    1、集成SDK:
    下载并解压SDK​:下载链接

    2、添加依赖库
    SystemConfiguration.framework
    libz.tbd
    libc++.tbd
    CoreTelephony.framework
    CFNetwork.framework
    Security.framework
    UIKit.framework
    Foundation.framework
    CoreGraphics.framework
    CoreMotion.framework
    CoreText.framework

    3、新增⼀一条URL Scheme:选中⼯工程Target -> Info -> URLTypes;
    identifier随便写,URL scheme此时也可以随便填写,但是要保证调起支付的方法里传的appScheme参数和这里填写的一致

    支付宝URL scheme
    调起支付的方法中的参数scheme要和URL scheme配置中填写的一样

    4、添加⽩白名单:LSApplicationQueriesSchemes新增⽩名单

    支付宝白名单

    5、在支付页面获取到支付所需要的参数后调用支付方法

       /**
         *  支付宝支付
         */
    -(void)handleOrderPayWithParams:(NSDictionary *)aParam
        {
            NSLog(@"aParm = %@",aParam);
            //这里的appScheme一定要与URL  Scheme中的一致
            NSString *appScheme = @"alisdkofshuyinHotel";
            NSString *orderString = aParam[@"orderInfo"];
            // NOTE: 调用支付结果开始支付
            [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                NSLog(@"reslut = %@",resultDic);
                int statusCode = [resultDic[@"resultStatus"]  intValue];
                if (statusCode == 9000) {
                    //订单支付
                    [SVProgressHUD showSuccessWithStatus:@"支付成功!"];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccess" object:nil];
                }else {
                    //交易失败
                    // [[NSNotificationCenter defaultCenter] postNotificationName:@"PAY_STATUS" object:@"0"];
                    [SVProgressHUD showErrorWithStatus:@"支付异常!"];
                }
            }];
        }
    

    6、appDelegate中处理回调结果

    //iOS9.0以前
    - (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) {
                NSLog(@"result = %@",resultDic);
            }];
            // 授权跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
                // 解析 auth code
                NSString *result = resultDic[@"result"];
                NSString *authCode = nil;
                if (result.length>0) {
                    NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                    for (NSString *subResult in resultArr) {
                        if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                            authCode = [subResult substringFromIndex:10];
                            break;
                        }
                    }
                }
                NSLog(@"授权结果 authCode = %@", authCode?:@"");
            }];
        }
        return YES;
    }
    
    // NOTE: 9.0以后使用新API接口
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
    {
        if ([url.host isEqualToString:@"safepay"]) {
            // 支付跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
                if ([resultDic[@"resultStatus"] integerValue] == 9000) {
                    [SVProgressHUD showSuccessWithStatus:@"支付成功!"];
                    PaySucceedViewController *vc = [[PaySucceedViewController alloc] init];
                    vc.backStr = @"1";
                    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
                    [self.tb presentViewController:navi animated:NO completion:nil];
                }else{
                    [SVProgressHUD showSuccessWithStatus:@"支付异常!"];
                    PayFailedViewController *vc = [[PayFailedViewController alloc] init];
                    vc.backStr = @"1";
                    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
                    [self.tb presentViewController:navi animated:NO completion:nil];
                }
            }];
            
            // 授权跳转支付宝钱包进行支付,处理支付结果
            [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
                NSLog(@"result = %@",resultDic);
                // 解析 auth code
                NSString *result = resultDic[@"result"];
                NSString *authCode = nil;
                if (result.length>0) {
                    NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                    for (NSString *subResult in resultArr) {
                        if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                            authCode = [subResult substringFromIndex:10];
                            break;
                        }
                    }
                }
                NSLog(@"授权结果 authCode = %@", authCode?:@"");
            }];
        }
        return YES;
    }
    

    相关文章

      网友评论

        本文标题:iOS AliPay(支付宝)支付

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