ios 支付宝签名验证加签是什么意思

世界那么大,好想去看看
世界在我手里旋转
支付宝的集成还是比较简单的,按照文档来一步步操作,基本上很顺利。不过,仍然有两个地方会是坑。这里我集成成功了,在此整理一下。说先说一下我遇到的坑如下:
第一个坑:下载的SDK文件AliPay.bundle和AliPay.framework一直报错,找不到库,没法使用。什么原因,我暂时还不知道。
解决办法:将官方提供的demo中SDK文件拖入项目中,替换掉自己下载的SDK即可
第二个坑:拖入的openssl目录下的所有#include&&openssl/xxx.h&找不到路径。原因是在demo中openssl目录直接被放到了项目根目录下,而我们拖入是被放到了工程目录下,路径不一致,需要更改路径。
解决办法:在Build setting下设置Header Search Path路径。
例如我的路经设置图为,是根据下面图显示的openssl设置的:
二、看看我的项目图
(1)在支付宝开发者平台注册登录账号创建应用获取appid,蚂蚁金服开放平台()
(2)配置密钥,开发者调用接口前需要先生成RSA密钥,RSA密钥包含应用私钥(APP_PRIVATE_KEY)、应用公钥(APP_PUBLIC_KEY)。生成密钥后在开放平台管理中心进行密钥配置,配置完成后可以获取支付宝公钥(ALIPAY_PUBLIC_KEY)。详细步骤请参考。
(3)集成并配置sdk,要将库添加完整
(4)设置URL Schemes,这个必须保证唯一性,因为支付完成后会根据这个调回到我们自己的app,我将这个在pch中做了定义,值必须保持一样
在pch设置宏定义并导入头文件
Prefix header
The contents of this file are implicitly included at the beginning of every source file.
#import &Availability.h&
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#ifdef __OBJC__
#import &UIKit/UIKit.h&
#import &Foundation/Foundation.h&
#import &AlipaySDK/AlipaySDK.h& //支付宝SDK
#import "PayManager.h"
//支付宝调起支付类
#import "DataSigner.h"
//支付宝签名类
#import "Order.h"
//订单模型
#import "APAuthV2Info.h"
//授权模型
-----------------------------------
支付宝支付需要配置的参数
-----------------------------------
//开放平台登录https:///platform/appManage.htm
//管理中心获取APPID
#define AlipayAPPID
@"xxxxxxxxx"
//管理中心获取PIDID
#define PIDID
@"xxxxxxxxx"
//支付宝私钥(用户自主生成,使用pkcs8格式的私钥,文档上提供了两种生成方式,自己去选择吧)
#define AlipayPrivateKey
@"xxxxxxxxx"
//应用注册scheme,在AliSDKDemo-Info.plist定义URL types
#define URLScheme
@"AliPayKit"
//支付宝支付接口
#define AlipayUrl
@"alipay.trade.app.pay"
在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);
// 授权跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
//解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode =
if (result.length&<span style="color: #) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length & <span style="color: # && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:<span style="color: #];
NSLog(@"授权结果 authCode = %@", authCode?:@"");
return YES;
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary&NSString*, id& *)options
if ([url.host isEqualToString:@"safepay"]) {
// 支付跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 授权跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode =
if (result.length&<span style="color: #) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length & <span style="color: # && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:<span style="color: #];
NSLog(@"授权结果 authCode = %@", authCode?:@"");
return YES;
在工具类PayManager.m中实现支付
PayManager.m
Created by 夏远全 on 16/12/20.
Copyright (C) 2016年 广州市东德网络科技有限公司. All rights reserved.
#import "PayManager.h"
@implementation PayManager
#pragma mark - 进行支付
+(void)openAliPayForPaying{
/*=========================================================*/
/*====客户端调用支付宝支付(实际操作请放到服务端)=================*/
/*=========================================================*/
//AppId和PrivateKey没有配置下的提示
if ([AlipayAPPID length] == <span style="color: # || [AlipayPrivateKey length] == <span style="color: #)
[self alertShow];
//将商品信息赋予AlixPayOrder的成员变量
Order* order
= [Order new];
order.app_id
= AlipayAPPID;
// NOTE: app_id设置
order.method
// NOTE: 支付接口名称
order.charset = @"utf-8";
// NOTE: 参数编码格式
NSDateFormatter* formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
order.timestamp = [formatter stringFromDate:[NSDate date]];// NOTE: 当前时间点
order.version
= @"<span style="color: #.0";
// NOTE: 支付版本
order.sign_type = @"RSA";
// NOTE: sign_type设置
// NOTE: 商品数据
order.biz_content
= [BizContent new];
order.biz_content.body
= @"我是测试数据";
order.biz_content.subject
= @"<span style="color: #";
order.biz_content.out_trade_no
= [self generateTradeNO];
//订单ID(由商家自行制定)
order.biz_content.timeout_express
= @"<span style="color: #m";
//超时时间设置
order.biz_content.total_amount
= [NSString stringWithFormat:@"%.2f", <span style="color: #.01];//商品价格
//将商品信息拼接成字符串
NSString *orderInfo
= [order orderInfoEncoded:NO];
NSString *orderInfoEncoded
= [order orderInfoEncoded:YES];
NSLog(@"orderSpec = %@",orderInfo);
// NOTE: 获取私钥并将商户信息签名,外部商户的加签过程请务必放在服务端,防止公私钥数据泄露;
需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
id&DataSigner& signer = CreateRSADataSigner(AlipayPrivateKey);
NSString *signedString = [signer signString:orderInfo];
// NOTE: 如果加签成功,则继续执行支付
if (signedString != nil) {
//应用注册scheme,在AliSDKDemo-Info.plist定义URL types
NSString *appScheme = URLS
// NOTE: 将签名成功字符串格式化为订单字符串,请严格按照该格式
NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@",
orderInfoEncoded, signedString];
// NOTE: 调用支付结果开始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
#pragma mark - 随机字符串
//==============产生随机订单号==============
+ (NSString *)generateTradeNO
static int kNumber = <span style="color: #;
NSString *sourceStr = @"<span style="color: #ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
srand((unsigned)time(<span style="color: #));
for (int i = <span style="color: #; i & kN i++)
unsigned index = rand() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, <span style="color: #)];
[resultStr appendString:oneStr];
return resultS
#pragma mark - 提示信息
+(void)alertShow{
//去除警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
//AppId和PrivateKey没有配置下的提示
if ([AlipayAPPID length] == <span style="color: # || [AlipayPrivateKey length] == <span style="color: #)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"缺少appId或者私钥。"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
#pragma clang diagnostic pop
五、模拟器和真机演示的截图如下:
首先我生成的私钥如下:
支付截图:
完成的demo地址,github:,希望有帮助
阅读(...) 评论()2008年12月 移动平台大版内专家分月排行榜第一
2009年3月 移动平台大版内专家分月排行榜第三2009年1月 移动平台大版内专家分月排行榜第三2008年11月 移动平台大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。支付宝 iOS SDK 的简单使用 - IOS - 伯乐在线
& 支付宝 iOS SDK 的简单使用
一、去申请开通支付宝使用权限
二、审核通过
1、seller ID:商家唯一标识符
2、partner ID:合作身份者 ID,以2088开头的16位纯数字
3、加密用到的文件(支付宝公钥/自助生成的商家私钥)
三、具体项目代码(Demo)
准备工作:解压从上下载到的SDK压缩包(藏的很深,具体网址是,下载到的文件名是 WS_MOBILE_PAY_SDK_BASE.zip,内含Android 和 iOS 的 SDK),找到WS_MOBILE_PAY_SDK_BASE &#8211;& 支付宝钱包支付接口开发包2.0标准版() &#8211;& DEMO &#8211;& 客户端demo &#8211;& 支付宝钱包支付接口开发包2.0标准版(iOS 2.2.3).zip,解压。
位置藏的很好
1、启动IDE(如Xcode),把iOS包中的以下文件拷贝到项目文件夹下,并导入到项目工程中。
?AlipaySDK.bundle AlipaySDK.framework AliSDKDemo文件夹下的Order.h AliSDKDemo文件夹下的Order.m AliSDKDemo文件夹下的Util文件夹 AliSDKDemo文件夹下的openssl文件夹 AliSDKDemo文件夹下的libcrypto.a AliSDKDemo文件夹下的libssl.a
2、新建一个header file(.h)文件,放入商家相关的宏
Objective-C
PartnerConfig.h
YuenAlipayDemo
Created by peikua on 15/8/17.
Copyright (c) 2015年 Ruan. All rights reserved.
#ifndef YuenAlipayDemo_PartnerConfig_h
#define YuenAlipayDemo_PartnerConfig_h
//合作身份者id,以2088开头的16位纯数字
#define PartnerID
//收款支付宝账号
#define SellerID
//商户私钥,自助生成
#define PartnerPrivKey @"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAL0M8jaJCm9bMb7PjgI0wR9+mpzWTcNTwTyYBEXmrJg3MjRVluUezDjQhQBSrgaMTeM40cz+1Nt/f1OlS/vB9PzGSF+MDty6zS0NQEEvVjUUge7PsOtbPDIEmuPppKIj4wETfavaZt7j4/kVuABDC2P1DpPRP686dJsNTkSO5qrNAgMBAAECgYApxEVy9P3gMkagQFzAcgVEvwTLp7EQeV2U1IUFKHxzOKaX11z6C77UwoTP2HRoL/E5RSFc5+QBBn8L7NYHrgdAu4L5Kl048saM53QyXJviQs7lgxDSBbo+EHDY9OJJsVRalpqKSirgBZmce/M4/tNhDxUfV5yXvxOC43JEr92UIQJBAPXbahDDMN+D0MqG1y0zPyU5bJwopXsSLIxpqp4vRmHokMxlber5HGMgSSnVQ9x9j974G1RSamqV34xwnqPzIlUCQQDE2ZPgtKd9Te19kGpmmCs64iqlkUVabAuKI8wMyx4hGZx6/EpeufFiTpF3F3YDN37JOenBefLL9UIkrOrjXI6ZAkBmpX75FKV5DG3FwNph0r2QaxM/d3DvmzziOtOzS4WVJyYdUFO+ANerQzWIs7OrgPjqXKf8YpRvf7dfyT1SshYpAkAhj0qDw6jOVwvHHWjWZtjv6AEHSxX8zXDGM0YlZDeVww0Hdp2jOqYpcWWhXRGUiNCHs+TjREwdc4m8QPKmom/5AkAYGRw6TLB/XWfEvlGLMHMmbZWMXDBdBmlIN+JK2oRjIoTryG35KlXzAHWcAq2xVhvCd6gJjz9arUmqewOLBMWn"
//支付宝公钥
#define AlipayPubKey
@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB"
//安全校验码(MD5)密钥,以数字和字母组成的32位字符
#define MD5_KEY
@"1g81z5tgu3o2yst4mqm6n838dxe5cw1a"
1234567891011121314151617181920212223
////&&PartnerConfig.h//&&YuenAlipayDemo////&&Created by peikua on 15/8/17.//&&Copyright (c) 2015年 Ruan. All rights reserved.//&#ifndef YuenAlipayDemo_PartnerConfig_h#define YuenAlipayDemo_PartnerConfig_h&//合作身份者id,以2088开头的16位纯数字#define PartnerID&&&&&&@"4308"//收款支付宝账号#define SellerID&&&&&& @""//商户私钥,自助生成#define PartnerPrivKey @"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAL0M8jaJCm9bMb7PjgI0wR9+mpzWTcNTwTyYBEXmrJg3MjRVluUezDjQhQBSrgaMTeM40cz+1Nt/f1OlS/vB9PzGSF+MDty6zS0NQEEvVjUUge7PsOtbPDIEmuPppKIj4wETfavaZt7j4/kVuABDC2P1DpPRP686dJsNTkSO5qrNAgMBAAECgYApxEVy9P3gMkagQFzAcgVEvwTLp7EQeV2U1IUFKHxzOKaX11z6C77UwoTP2HRoL/E5RSFc5+QBBn8L7NYHrgdAu4L5Kl048saM53QyXJviQs7lgxDSBbo+EHDY9OJJsVRalpqKSirgBZmce/M4/tNhDxUfV5yXvxOC43JEr92UIQJBAPXbahDDMN+D0MqG1y0zPyU5bJwopXsSLIxpqp4vRmHokMxlber5HGMgSSnVQ9x9j974G1RSamqV34xwnqPzIlUCQQDE2ZPgtKd9Te19kGpmmCs64iqlkUVabAuKI8wMyx4hGZx6/EpeufFiTpF3F3YDN37JOenBefLL9UIkrOrjXI6ZAkBmpX75FKV5DG3FwNph0r2QaxM/d3DvmzziOtOzS4WVJyYdUFO+ANerQzWIs7OrgPjqXKf8YpRvf7dfyT1SshYpAkAhj0qDw6jOVwvHHWjWZtjv6AEHSxX8zXDGM0YlZDeVww0Hdp2jOqYpcWWhXRGUiNCHs+TjREwdc4m8QPKmom/5AkAYGRw6TLB/XWfEvlGLMHMmbZWMXDBdBmlIN+JK2oRjIoTryG35KlXzAHWcAq2xVhvCd6gJjz9arUmqewOLBMWn"//支付宝公钥#define AlipayPubKey&& @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB"//安全校验码(MD5)密钥,以数字和字母组成的32位字符#define MD5_KEY&&&&&&&&@"1g81z5tgu3o2yst4mqm6n838dxe5cw1a"&#endif
3、在 ViewController.m 中导入必要的头文件,新建一个按钮,为按钮添加“购买Action”
Objective-C
ViewController.m
YuenAlipayDemo
Created by peikua on 15/8/17.
Copyright (c) 2015年 Ruan. All rights reserved.
#import "ViewController.h"
#import "MyUtility.h"
#import &AlipaySDK/AlipaySDK.h&
#import "Order.h"
#import "PartnerConfig.h"
@interface ViewController () &UITableViewDelegate&
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *payButton = [MyUtility createButtonWithFrame:CGRectMake(0, kHeightOfScreen/2, kWidthOfScreen, 100) title:@"支付" backgroundImageName: nil selectImageName:nil target:self action:@selector(payAction)];
payButton.backgroundColor = [UIColor colorWithRed:0.000 green:1.000 blue:0.870 alpha:1.000];
[self.view addSubview:payButton];
12345678910111213141516171819202122232425262728
////&&ViewController.m//&&YuenAlipayDemo////&&Created by peikua on 15/8/17.//&&Copyright (c) 2015年 Ruan. All rights reserved.//&#import "ViewController.h"#import "MyUtility.h"#import &AlipaySDK/AlipaySDK.h&#import "Order.h"#import "PartnerConfig.h"&@interface ViewController () &UITableViewDelegate&&@end&@implementation ViewController&- (void)viewDidLoad {&&&&[super viewDidLoad];&&&&// Do any additional setup after loading the view, typically from a nib.&&&&&UIButton *payButton = [MyUtility createButtonWithFrame:CGRectMake(0, kHeightOfScreen/2, kWidthOfScreen, 100) title:@"支付" backgroundImageName: nil selectImageName:nil target:self action:@selector(payAction)];&&&&payButton.backgroundColor = [UIColor colorWithRed:0.000 green:1.000 blue:0.870 alpha:1.000];&&&&[self.view addSubview:payButton];}
4、购买Action
Objective-C
- (void)payAction
1. 生成订单信息
Order *order = [[Order alloc] init];
order.partner = PartnerID; //支付宝分配给商户的ID
order.seller = SellerID; //收款支付宝账号(用于收款)
order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)
NSLog(@"%@", order.tradeNO);
order.productName = @"Mac"; //商品标题
order.productDescription = @"MacBook Air"; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f", 0.01]; //商品价格
//???: 回调 URL 没有进行调试
order.notifyURL =
@""; //回调URL(通知服务器端交易结果)(重要)
//???: 接口名称要如何修改?
order.service = @"mobile.securitypay.pay"; //接口名称, 固定值, 不可空
order.paymentType = @"1"; //支付类型 默认值为1(商品购买), 不可空
order.inputCharset = @"utf-8"; //参数编码字符集: 商户网站使用的编码格式, 固定为utf-8, 不可空
order.itBPay = @"30m"; //未付款交易的超时时间 取值范围:1m-15d, 可空
// 应用注册scheme,在当前项目的Info.plist定义URL types
NSString *appScheme = @"alisdkdemo";
// 将订单信息拼接成字符串
NSString *orderSpec = [order description];
NSLog(@"订单信息orderSpec = %@", orderSpec);
2. 签名加密
获取私钥并将商户信息签名, 外部商户可以根据情况存放私钥和签名, 只需要遵循 RSA 签名规范, 并将签名字符串 base64 编码和 UrlEncode
id &DataSigner& signer = CreateRSADataSigner(PartnerPrivKey);
NSString *signedString = [signer signString:orderSpec];
3. 将签名成功字符串格式化为订单字符串,请严格按照该格式
NSString *orderString =
if (signedString != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"", orderSpec, signedString, @"RSA"];
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
产生随机订单号
@return 订单号字符串
- (NSString *)generateTradeNO
static int kNumber = 15;
NSString *sourceStr = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
支付宝官方给出的 Demo 中加入了这句生成种子的代码, 但是 arc4random 似乎并不需要生成随机种子(引用网上: arc4random() 是一个真正的伪随机算法,不需要生成随机种子,因为第一次调用的时候就会自动生成)
srand((unsigned)time(0));
for (int i = 0; i & kN i++)
unsigned index =
arc4random() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
[resultStr appendString:oneStr];
NSLog(@"随机生成的订单号-&%@", resultStr);
return resultS
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
- (void)payAction{&&&&/**&&&& *&&1. 生成订单信息&&&& */&&&&Order *order = [[Order alloc] init];&&&&order.partner = PartnerID; //支付宝分配给商户的ID&&&&order.seller = SellerID; //收款支付宝账号(用于收款)&&&&order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)&&&&NSLog(@"%@", order.tradeNO);&&&&order.productName = @"Mac"; //商品标题&&&&order.productDescription = @"MacBook Air"; //商品描述&&&&order.amount = [NSString stringWithFormat:@"%.2f", 0.01]; //商品价格&&&&//???: 回调 URL 没有进行调试&&&&order.notifyURL =&&@""; //回调URL(通知服务器端交易结果)(重要)&&&&&//???: 接口名称要如何修改?&&&&order.service = @"mobile.securitypay.pay"; //接口名称, 固定值, 不可空&&&&order.paymentType = @"1"; //支付类型 默认值为1(商品购买), 不可空&&&&order.inputCharset = @"utf-8"; //参数编码字符集: 商户网站使用的编码格式, 固定为utf-8, 不可空&&&&order.itBPay = @"30m"; //未付款交易的超时时间 取值范围:1m-15d, 可空&&&&&// 应用注册scheme,在当前项目的Info.plist定义URL types&&&&NSString *appScheme = @"alisdkdemo";&&&&// 将订单信息拼接成字符串&&&&NSString *orderSpec = [order description];&&&&NSLog(@"订单信息orderSpec = %@", orderSpec);&&&&&/**&&&& *&&2. 签名加密&&&& *&&获取私钥并将商户信息签名, 外部商户可以根据情况存放私钥和签名, 只需要遵循 RSA 签名规范, 并将签名字符串 base64 编码和 UrlEncode&&&& */&&&&id &DataSigner& signer = CreateRSADataSigner(PartnerPrivKey);&&&&NSString *signedString = [signer signString:orderSpec];&&&&&/**&&&& *&&3. 将签名成功字符串格式化为订单字符串,请严格按照该格式&&&& */&&&&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);&&&&&&&&}];&&&&}}&/** *&&产生随机订单号 * *&&@return 订单号字符串 */- (NSString *)generateTradeNO{&&&&static int kNumber = 15;&&&&&NSString *sourceStr = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";&&&&NSMutableString *resultStr = [[NSMutableString alloc] init];&&&&&/*&&&& 支付宝官方给出的 Demo 中加入了这句生成种子的代码, 但是 arc4random 似乎并不需要生成随机种子(引用网上: arc4random() 是一个真正的伪随机算法,不需要生成随机种子,因为第一次调用的时候就会自动生成)&&&& srand((unsigned)time(0));&&&& */&&&&&for (int i = 0; i & kNumber; i++)&&&&{&&&&&&&&unsigned index =&&arc4random() % [sourceStr length];&&&&&&&&NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];&&&&&&&&[resultStr appendString:oneStr];&&&&}&&&&NSLog(@"随机生成的订单号-&%@", resultStr);&&&&return resultStr;}
5、配置支付宝客户端返回url处理方法:在AppDelegate.m中,重写- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation方法
Objective-C
- (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;
1234567891011121314
- (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;}
四、博主自己遇到的项目运行报错的2个地方,另需要注意添加URL Schemes
1、lexical or preprocessor issue 'penssl/asn1.h' file not found 支付宝提供的PDF中有解决办法(自己犯二了没有好好看文档)
点击项目名称,点击“Build Settings”选项卡,在搜索框中,以关键字“search” 搜索,对“Header Search Paths”增加头文件路径:$(SRCROOT)/项目名称。
2、报的一堆 Apple Mach-O Linker Error:点击项目名称,点击“Build Phases”选项卡,在“Link Binary with Librarles” 选项中,新增“SystemConfiguration.framework” 系统库文件
3、配置URL Schemes:点击项目名称,点击“Info”选项卡,在“URL Types”选项中,点击“+”, 在“URL Schemes”中输入“alisdkdemo”。“alisdkdemo”来自于文件 “APViewController.m”的 NSString *appScheme = @&#8221;alisdkdemo&#8221;
完整代码:
更多内容请查看支付宝官方文档,博主这就是一个简单的小 Demo
可能感兴趣的话题
关于iOS频道
iOS频道分享iOS和Swift开发,应用设计和推广,iOS相关的行业动态。
新浪微博:
推荐微信号
(加好友请注明来意)
&#8211; 好的话题、有启发的回复、值得信赖的圈子
&#8211; 分享和发现有价值的内容与观点
&#8211; 为IT单身男女服务的征婚传播平台
&#8211; 优秀的工具资源导航
&#8211; 翻译传播优秀的外文文章
&#8211; 国内外的精选文章
&#8211; UI,网页,交互和用户体验
&#8211; 专注iOS技术分享
&#8211; 专注Android技术分享
&#8211; JavaScript, HTML5, CSS
&#8211; 专注Java技术分享
&#8211; 专注Python技术分享
& 2017 伯乐在线

我要回帖

更多关于 ios集成支付宝签约 的文章

 

随机推荐