iOS学习笔记--支付宝支付集成

来源:互联网 发布:淘宝差评可以改好评吗 编辑:程序博客网 时间:2024/05/22 15:24

最近项目中需要集成支付宝支付功能,在此做一个简单的笔记。
集成支付宝支付需要以下几个步骤:

1、申请支付宝支付用到的AppKey;2、添加支付宝SDK3、调用方法发送订单信息4、设置URL Type5、添加回调方法

第一步在此略过。
第二步下载AlipaySDK,下载后文件夹中有AlipaySDK.bundle 和AlipaySDK.framework 两个文件,添加到工程中进行编译,如有缺少的文件,在进行添加。
第三步封装订单信息,调用如下方法。

导入头文件#import <AlipaySDK/AlipaySDK.h> NSString *appID = 申请的AppID; NSString *rsa2PrivateKey = @""; NSString *rsaPrivateKey = @"";

rsa2PrivateKey 与 rsaPrivateKey 为私钥,只需填入一个,简易用rsa2PrivateKey。获取 rsa2PrivateKey,建议使用支付宝提供的公私钥生成工具生成工具地址:

 https://doc.open.alipay.com/docs/doc.htm?treeId=291&articleId=106097&docType=1
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic){   NSLog(@"reslut ===>= %@",resultDic);}}];这里需要注意:   orderString 为订单信息,由后台返回。   appScheme 为跳转的URL 在Info-plist中定义

第四步设置URL type
这里写图片描述
第五步,添加回调方法。在AppDelegate.m中

- (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);        }];        // 授权跳转支付宝钱包进行支付,处理支付结果        [[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;}

本文仅供学习,如有错误请见谅!

原创粉丝点击