支付宝 iOS SDK 的简单使用
原文出处: Vinc
位置藏的很好
AlipaySDK.bundle AlipaySDK.framework AliSDKDemo文件夹下的Order.h AliSDKDemo文件夹下的Order.m AliSDKDemo文件夹下的Util文件夹 AliSDKDemo文件夹下的openssl文件夹 AliSDKDemo文件夹下的libcrypto.a AliSDKDemo文件夹下的libssl.a
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// // 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 @"2088511933544308" //收款支付宝账号 #define SellerID @"[email protected]" //商户私钥,自助生成 #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 |
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// // 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]; } |
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
- (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 = @"http://www.xxx.com"; //回调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 = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 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; } |
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
方法
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (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; } |
lexical or preprocessor issue 'penssl/asn1.h' file not found
支付宝提供的PDF中有解决办法(自己犯二了没有好好看文档)
点击项目名称,点击“Build Settings”选项卡,在搜索框中,以关键字“search” 搜索,对“Header Search Paths”增加头文件路径:$(SRCROOT)/项目名称。
问啊-定制化IT教育平台,牛人一对一服务,有问必答,开发编程社交头条 官方网站:www.wenaaa.com 下载问啊APP,参与官方悬赏,赚百元现金。
QQ群290551701 聚集很多互联网精英,技术总监,架构师,项目经理!开源技术研究,欢迎业内人士,大牛及新手有志于从事IT行业人员进入!