第一步:
下载支付宝Demo
这里是支付宝开放平台,大家点开网址找到支付能力->移动支付
下载之后解压会出现一下目录 :
打开 支付宝钱包支付接口开发包2.0标准版(iOS 15.0.8) 压缩包
会出现一下目录,其中红色圈住的是需要东西,大家可以拷贝出来,在桌面新建一个AliPaySDK文件夹,放到其中,然后拽到工程中:
第二步:
在需要集成支付宝支付的控制器中导入以下头文件
#import "Order.h"
#import <AlipaySDK/AlipaySDK.h>
#import "DataSigner.h"
privateKey的生成参照链接
//在需要支付的按钮点击事件里面加入以下代码:
/*
*商户的唯一的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的成员变量
//以下需要自定义
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO = @"lzh_1"; //订单ID(由商家自行制定)
order.productName = @"商品标题。。"; //商品标题
order.productDescription = @"哈哈第一次支付"; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f",.01]; //商品价格
order.notifyURL = @"http://www.hehe.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<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);
//支付宝支付完成后的回调方法,9000代表支付成功
}];
}
//导入头文件#import <AlipaySDK/AlipaySDK.h>
//在appdelegate.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);
}];
}
return YES;
}
这样代码已经生成好了,如果需要自定义则自己写一个Order类就行了
第三步
下面还需要解决一些头文件、库的错误和其他的配置
1. 在TARGETS的BuildSettings中找到HeaderSearchPaths,
添加上刚才拖拽到工程中的文件夹的路径:
2. 打开支付宝Demo,在支付宝Demo工程的AliSDKDemo-Info.plist文件中找到URL types,复制下面的Item0那一行添加到自己工程的URL types中
3. 需要添加的一些类库
4. 新建一个pch文件,添加以下代码,如果已经有pch文件则添加上UIKit和Foundation:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
5.如果还有其他问题,参照支付宝集成流程详解