原文出處: Vinc ios
位置藏的很好git
AlipaySDK.bundle AlipaySDK.framework AliSDKDemo文件夾下的Order.h AliSDKDemo文件夾下的Order.m AliSDKDemo文件夾下的Util文件夾 AliSDKDemo文件夾下的openssl文件夾 AliSDKDemo文件夾下的libcrypto.a AliSDKDemo文件夾下的libssl.agithub
Objective-C算法
1編程 2api 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 @"yingtehua8@sina.com" //商戶私鑰,自助生成 #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行業人員進入!