原创地址:http://www.jianshu.com/p/1ca250b92ecb
![](http://upload-images.jianshu.io/upload_images/4729102-eafec6f4fded2910.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
首先,支付宝支付的流程大致如下:
1、下载和查看支付宝官方Demo
直接进入支付宝的支付平台下载:doc.open.alipay.com/doc2/detail.htm
![](http://upload-images.jianshu.io/upload_images/4729102-253826af8ea1aa45.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
集成过程基本就是按照上面的四个流程就可以完成iOS的支付宝SDK。
那我们一起按着步骤来集成:
1、下载SDK和Demo。SDK&DEMO
![](http://upload-images.jianshu.io/upload_images/4729102-4ec408306a16ff52.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/4729102-66ad7e9ad30e1a81.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
下载后解压:
![](http://upload-images.jianshu.io/upload_images/4729102-65ac7f20cff5c27c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
运行Demo:
![](http://upload-images.jianshu.io/upload_images/4729102-80f0d977782ff02f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
查找方法:
![](http://upload-images.jianshu.io/upload_images/4729102-04435a821432cc8c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
由上图可以看出需要三个参数的值:partner、seller、privateKey。这三个参数就需要商户app申请的。
![](http://upload-images.jianshu.io/upload_images/4729102-edd3657fddff121c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2、实现支付宝支付功能:
2-1、添加SDK:
![](http://upload-images.jianshu.io/upload_images/4729102-1c54ae8300a6ce39.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2-2、添加SDK依赖库:
![](http://upload-images.jianshu.io/upload_images/4729102-673fbaea647ca049.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2-3、创建一个订单对象:AlipayOrder
![](http://upload-images.jianshu.io/upload_images/4729102-9d62a6873436c252.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2-4、把支付宝Demo中的订单对象的参数都拷贝进去:
Order.h的参数 都拷贝到 AlipayOrder.h中。
![](http://upload-images.jianshu.io/upload_images/4729102-7bde88c3ce1bdb19.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2-5、处理支付的代码:
![](http://upload-images.jianshu.io/upload_images/4729102-a88280bf8e72c3a5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2-6、对拷贝部分的代码适当的抽取和修改,放在一个alipay方法中:
- (void)aliPay {
// 支付宝支付
/* 在调用支付宝支付之前,需要我们将相关订单参数发送至我们的后台服务器,由后台服务器进行签名处理, 并返回客户端所有相关参数,客户端直接使用参数调起支付宝支付。
*/
/*
*商户的唯一的parnter和seller。
*签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
*/ /*======================================================================*/ /*=======================需要填写商户app申请的==============================*/ /*======================================================================*/ NSString *partner = @"";
NSString *seller = @"";
NSString *privateKey = @""; /*======================================================================*/ /*======================================================================*/ /*======================================================================*/ //partner和seller获取失败,提示
if ([partner length] == 0 ||
[seller length] == 0 ||
[privateKey length] == 0)
{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缺少partner或者seller或者私钥。"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
return;
}
/*
*生成订单信息及签名
*/
//将商品信息赋予AlixPayOrder的成员变量
AlipayOrder *order = [[AlipayOrder alloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO = @"1234567890"; //订单ID(由商家自行制定)
order.productName = @"测试商品标题"; //商品标题
order.productDescription = @"测试商品描述"; //商品描述
order.amount = @"0.01"; //商品价格
order.notifyURL = @"http://www.xxx.com"; //回调URL
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1";
order.inputCharset = @"utf-8";
order.itBPay = @"30m";
order.showUrl = @"m.alipay.com";
//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
NSString *appScheme = @"alisdkdemo";
//将商品信息拼接成字符串
NSString *orderSpec = [order description];
NSLog(@"orderSpec = %@",orderSpec);
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
// id<DataSinger> signer = CreateRSADataSigner(privateKey);
// 签名值由服务器处理并返回客户端
NSString *signedString = @"xxxxxxx_sign";
//将签名成功字符串格式化为订单字符串,请严格按照该格式
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);
}];
}
}
2-7、另外需要应用中注册appScheme,可以在上面代码中获取:
//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
NSString *appScheme = @"alisdkdemo";
2-8、添加appScheme
![](http://upload-images.jianshu.io/upload_images/4729102-5dec14597cac4dd4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2-9、支付代码处理之后,我们开始处理回调结果。
需要在Appdelegate中添加支付宝头文件#import<AlipaySDK/AlipaySDK.h>并添加处理回到结果的代理方法:
![](http://upload-images.jianshu.io/upload_images/4729102-880215c9f9287843.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2-10、添加按钮监听支付事件和设置商户ID的参数:
3、获取商户支付ID。
【说明:使用支付宝支付的时,如果手机安装了支付宝App,就会直接调用支付宝进行支付;
如果手机没有安装支付宝App,就会调用支付宝H5界面进行支付。】