iOS 集成支付宝支付遇到的问题

2018-12-25  本文已影响10人  路边的风景呢

首先 我得说一下 iOS 的不能使用 沙箱数据去做测试,一定要用正式的APPID 还有公钥 私钥去配置 我就是在这个地方卡了两天,所以写一下记录一下!

然后接下来就是 很平常的步骤了 

1 . 先去下载SDK,然后倒入到你的文件里面 

2 . 就是导入依赖 如下图所示  AlipaySDK.framework 这个是你下载好的SDK的包里面就有的

3 . 导入后就是在你的Appdeleagte.m 里面 和你调用支付的地方   写入头文件 #import<AlipaySDK/AlipaySDK.h>

( 1 ) Appdeleagte.m里面写入这两个方法  记住不需要做其他的操作里面的通知也写好了

//支付宝回调

- (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);

            [[NSNotificationCenter defaultCenter] postNotificationName:@"ALiPaySuccess" object: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 = [resultcomponentsSeparatedByString:@"&"];

                for (NSString*subResult in resultArr) {

                    if (subResult.length>10&& [subResulthasPrefix:@"auth_code="]) {

                        authCode = [subResultsubstringFromIndex:10];

                        break;

                    }

                }

            }

            NSLog(@"授权结果 authCode = %@", authCode?:@"");

        }];

    }

    returnYES;

}

// NOTE: 9.0以后使用新API接口

- (BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary *)options

{

    if ([url.host isEqualToString:@"safepay"]) {

        // 支付跳转支付宝钱包进行支付,处理支付结果

        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {

            NSLog(@"result = %@",resultDic);

            [[NSNotificationCenter defaultCenter] postNotificationName:@"ALiPaySuccess" object: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 = [resultcomponentsSeparatedByString:@"&"];

                for (NSString*subResult in resultArr) {

                    if (subResult.length>10&& [subResulthasPrefix:@"auth_code="]) {

                        authCode = [subResultsubstringFromIndex:10];

                        break;

                    }

                }

            }

            NSLog(@"授权结果 authCode = %@", authCode?:@"");

        }];

    }

    returnYES;

}

(2) 我写了一个工具类 在里面调用支付宝的支付接口

// NOTE: 调用支付结果开始支付

    [[AlipaySDK defaultService] payOrder:@"这里就是写后台返回的签完名的订单字符串" fromScheme:@"这个就是返回你的APP的标识符" callback:^(NSDictionary *resultDic) {

        intstatusCode = [resultDic[@"resultStatus"]  intValue];

        if (statusCode ==9000)

        {

        }else{

            //交易失败

            [SVProgressHUD showErrorWithStatus:@"支付异常"];

            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];

            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeCustom];

        }

    }];

这个就是调起支付的接口

( 3 ) 一定要记得在这里配置你们的这个就是返回你的APP的标识符 不然支付完成后是不会返回你的APP的这里要注意一下 微信的那个前面一定要填 我的没有填就不能回到自己的APP 

上一篇 下一篇

猜你喜欢

热点阅读