1.看看demo是怎样的
在网上搜索“iOS支付宝sdk”,下载sdk(如图1)

进入到图2点击左侧移动支付Demo&SDK再点击右侧的iOS&Android版资源,点击右侧SDK&DEMO下载SDK

下载完成后解压,就得到了这个(图3)

再解压“支付宝钱包支付接口开发包2.0标准版(iOS2.2.3)”就得到了这个(图4)

打开文件夹,运行一下看看。就得到了这个很炫的界面(图5)

我们再来看看源码:

图6中所示在APViewController中有三个字符串变量,和图5当中的提示是一样的,这个玩意儿就是商户和阿里签订协议后得到的一系列标示符。到时候和公司要。
2.自己集成支付宝
新建一个项目(注意:项目必须是英文,否则会出现迷之bug)。
回到刚才下载的demo中有两个文件 AlipaySDK.Bundle和AlipaySDK.framework。将这两个文件拷贝到项目中

在ViewController中导入头文件

在viewdidload方法中引入如下代码
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
order.tradeNO = [self generateTradeNO]; //订单ID(由商家□自□行制定)
order.productName = product.subject; //商品标题
order.productDescription = product.body; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商
品价格

你会发现报错了。
再回到下载的文件夹中

会发现有两个文件Order.h和Order.m,把这两个文件拷贝到项目中,再导入头文件。

进入到Order.h中会发现有很多的属性名字被换掉了,果断改了它。

接着复制如下的代码:
order.notifyURL = @"http://www.xxx.com"; //回调URLorder.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);
在复制完NSString *appScheme = @"alisdkdemo";这一句后需要在项目配置中点击info在下面的URLType中添加一个type,URLscheme的值为alisdkdemo;

集成完毕之后会发现还有地方报错

注释显示是签名错误,在开发文档中找到签名代码问题解决

到下载的demo里面找到这两个文件夹和两个文件,将文件拷贝到项目中,导入头文件“DataSigner.h”,重新编译发现啊会出现一堆缺少框架的错误

这时候需要在报错的文件的头文件上导入foundation框架。编译之后终于就剩一个错了。

打开工程配置文件BulidSetting,在搜索框中输入“search”找到HeaderSearchPath,添加一个路径,内容为“$(SRCROOT)/项目名”回车,重新编译。

会出现如图17所示的错误,此时需要导入一些静态库
libz.tbd
libc++.tbd
Security.framework
CoreMotion.Framework
CFNetwork.framework
CoreTelephony.framework
SystemConfiguration.framework

添加完成之后编译就不会报错啦。
最后将
//将签名成功字符串格式化为订单字符串,请严格按照该格式
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);
}];
}
这段代码复制到Viewdidload中。
再在AppDelegate.m中复制下面这段代码并导入
#import <AlipaySDK\AlipaySDK.h>
- (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);
}];
}
if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回authCode
[[AlipaySDK defaultService] processAuthResult:url
standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}
到此就大功告成,再结合项目具体做出修改就可以啦。
网友评论