您的位置:首页 > 移动开发 > IOS开发

ios 支付宝支付流程

2015-11-20 21:07 459 查看
步骤1:
启动IDE(如Xcode),把iOS包中的压缩文件中以下文件拷贝到项目文件夹下,

并导入到项目工程中。

AlipaySDK.bundle
AlipaySDK.framework


步骤2:
在需要调用AlipaySDK的文件中,增加头文件引用。#import
<AlipaySDK/AlipaySDK.h>

步骤3:
配置请求信息。

Order *order = [[Order alloc] init];

order.partner = partner;//合作身份者ID,以 2088 开头由 16 位纯数字组成的字符串。请参考“7.1 如何获得PID与
密钥”。

order.seller = seller;//
支付宝收款账号,手机号码或邮箱格式。


private_key//商户方的私钥,pkcs8 格式

order.tradeNO = [self generateTradeNO]; //订单ID(由商家□自□行制定)order.productName
= product.subject; //商品标题 order.productDescription = product.body; //商品描述

order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商 品价格

order.notifyURL = @"http://www.xxx.com"; //回调URL
order.service = @"mobile.securitypay.pay"; order.paymentType = @"1";

order.inputCharset = @"utf-8";

order.itBPay = @"30m";


//应用注册scheme,在AlixPayDemo-Info.plist定义URL
types NSString *appScheme = @"alisdkdemo";

//将商品信息拼接成字符串

NSString *orderSpec = [order description]; NSLog(@"orderSpec = %@",orderSpec);

//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循 RSA 签名规范,并将签名字符串 base64 编码和 UrlEncode

id<DataSigner> signer = CreateRSADataSigner(privateKey);

NSString *signedString = [signer signString:orderSpec];

//将签名成功字符串格式化为订单字符串,请严格按照该格式NSString
*orderString = nil;

if (signedString != nil) {

orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];


[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {

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


}];

步骤4:
配置支付宝客户端返回url处理方法。

如示例 AliSDKDemo\APAppDelegate.m 文件中,

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

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

//如果极简 SDK 不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给 SDK
if ([url.host isEqualToString:@"safepay"]) {

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

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


}

if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回authCode

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

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


}

return YES; }

注意:

出于安全考虑,请商户尽量把私钥保 存在服务端,在服务端进行签名验签。

点击项目名称,点击“Info”选项卡,在“URL
Types”选项中,点击“+”,
在“URL Schemes”中输入“alisdkdemo”。“alisdkdemo”来自于文件
“APViewController.m”的 NSString
*appScheme = @"alisdkdemo";。

步骤1:
启动IDE(如Xcode),把iOS包中的压缩文件中以下文件拷贝到项目文件夹下,

并导入到项目工程中。

AlipaySDK.bundle
AlipaySDK.framework


步骤2:
在需要调用AlipaySDK的文件中,增加头文件引用。#import
<AlipaySDK/AlipaySDK.h>

步骤3:
配置请求信息。

Order *order = [[Order alloc] init];

order.partner = partner;//合作身份者ID,以 2088 开头由 16 位纯数字组成的字符串。请参考“7.1 如何获得PID与
密钥”。

order.seller = seller;//
支付宝收款账号,手机号码或邮箱格式。


private_key//商户方的私钥,pkcs8 格式

order.tradeNO = [self generateTradeNO]; //订单ID(由商家□自□行制定)order.productName
= product.subject; //商品标题 order.productDescription = product.body; //商品描述

order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商 品价格

order.notifyURL = @"http://www.xxx.com"; //回调URL
order.service = @"mobile.securitypay.pay"; order.paymentType = @"1";

order.inputCharset = @"utf-8";

order.itBPay = @"30m";


//应用注册scheme,在AlixPayDemo-Info.plist定义URL
types NSString *appScheme = @"alisdkdemo";

//将商品信息拼接成字符串

NSString *orderSpec = [order description]; NSLog(@"orderSpec = %@",orderSpec);

//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循 RSA 签名规范,并将签名字符串 base64 编码和 UrlEncode

id<DataSigner> signer = CreateRSADataSigner(privateKey);

NSString *signedString = [signer signString:orderSpec];

//将签名成功字符串格式化为订单字符串,请严格按照该格式NSString
*orderString = nil;

if (signedString != nil) {

orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];


[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {

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


}];

步骤4:
配置支付宝客户端返回url处理方法。

如示例 AliSDKDemo\APAppDelegate.m 文件中,

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

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

//如果极简 SDK 不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给 SDK
if ([url.host isEqualToString:@"safepay"]) {

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

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


}

if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回authCode

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

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


}

return YES; }

注意:

出于安全考虑,请商户尽量把私钥保 存在服务端,在服务端进行签名验签。

点击项目名称,点击“Info”选项卡,在“URL
Types”选项中,点击“+”,
在“URL Schemes”中输入“alisdkdemo”。“alisdkdemo”来自于文件
“APViewController.m”的 NSString
*appScheme = @"alisdkdemo";。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: