一、准备工作
1.与支付宝签约获取如下三个参数
2.下载SDK
二、将支付宝SDK接入项目
1.添加对应的依赖库
libcrypto.a 和 libssl.a 文件在 AllSDKDemo 文件中。
2.添加对应的文件
这些文件在 AllSDKDemo 文件中。
3.添加URL Schemes
4.添加Header Search Paths的相对路径
点击项目名称,点击“Build Settings”选项卡,在搜索框中,以关键字“search”搜索,对“Header Search
Paths”增加头文件路径:$(SRCROOT)/文件相对路径。如果头文件信息已增加,可不必再增加。
温馨提示:“文件相对路径”指libcrypto.a 和 libssl.a所在的项目中的相对文件路径。比如我项目工程中libcrypto.a和 libssl.a放的位置为“TestAlipay/Alipay”,那我的头文件路径就是:$(SRCROOT)/TestAlipay/Alipay。这个地方要是填错了,就会报isa.h文件不存在的错误。
三、核心代码
1.生成订单,发起支付
#define kAlipayPartner @"" //请自行填写
#define kAlipaySeller @"" //请自行填写
#define kAlipayPrivateKey @"" //请自行填写
#import "ViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "Order.h"
#import "DataSigner.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width-20, 35)];
btn.backgroundColor = [UIColor orangeColor];
[btn setTitle:@"测试支付宝" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(createOrder) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)createOrder
{
NSString *partner = kAlipayPartner;
NSString *seller = kAlipaySeller;
NSString *privateKey = kAlipayPrivateKey;
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的成员变量
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO = @"123456789"; //商品订单号
order.productName = @"测试支付标题"; //商品标题
order.productDescription = @"测试支付描述"; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f",200.0]; //商品价格
NSLog(@"price:%@",order.amount);
order.notifyURL = @"http://krdong.weixinmob.com/alipay"; //回调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 = @"testAlipay";
//将商品信息拼接成字符串
NSString *orderSpec = [order description];
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循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);
}];
}
}
@end
2.支付成功之后的回调
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
return YES;
}
出现的错误:
一、编译报错
1、Cannot find interface declaration for ‘NSObject’, superclass of ‘Base64’
那么需要打开报错了的文件,增加头文件。
#import <Foundation/Foundation.h>
2、”openssl/asn1.h” file not found
点击项目名称,点击“Build Settings”选项卡,在搜索框中,以关键字“search”搜索,对“Header Search
Paths”增加头文件路径:$(SRCROOT)/文件相对路径。如果头文件信息已增加,可不必再增加。
温馨提示:“文件相对路径”指libcrypto.a 和 libssl.a所在的项目中的相对文件路径。比如我项目工程中libcrypto.a和 libssl.a放的位置为“TestAlipay/Alipay”,那我的头文件路径就是:$(SRCROOT)/TestAlipay/Alipay。
二、点击支付,不跳转
1、错误返回值:Error Domain=系统繁忙,请稍后再试 Code=1000 “The operation couldn’t be completed. (系统繁忙,请稍后再试 error 1000.)”
//将商品信息赋予AlixPayOrder的成员变量
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO = @"123456789";
order.productName = @"测试支付标题"; //商品标题
order.productDescription = @"测试支付描述"; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f",200.0]; //商品价格
order.notifyURL = @"http://krdong.weixinmob.com/alipay"; //回调URL
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1";
order.inputCharset = @"utf-8";
order.itBPay = @"30m";
order.showUrl = @"m.alipay.com";
可能原因:
(1)order.seller为空;
(2)order.amount金额格式不对,最多可以精确到分,保留两位小数。(%.2f)