一、支付寶設置
<寫在前言:如有哪里不對還請多多留言指正>
1.登陸支付寶開放平臺,詳情見圖1
2.創建應用并提交等待審核,詳情見圖2
3.打開Mac上的終端,使用終端生成公鑰和私鑰(rsa_public_key.pem & rsa_private_key.pem)。操作命令如下。詳情見圖3
3.1.打開? ? ? ?OpenSSL
3.2.生成私鑰em ?genrsa -out rsa_private_key.pem 1024
3.3.生成公鑰 ? ?rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
3.4.將RSA私鑰轉換成PKCS8格式 ? pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
3.5.退出終端 ? ?exit##
4.打開公鑰,上傳公鑰,詳情見圖4
步驟:點擊“文檔中心”->點擊“移動支付”->點擊“PID和密鑰管理”->點擊“上傳RSA公鑰”->點擊“b.alipaty.com”->點擊“查詢PID、Key”->這時會讓你安裝證書,輸入支付密碼->安裝成功后點擊“上傳RSA密鑰”
二、配置工程
1.下載AlipaySDK,并拖入工程中
2.添加SystemConfiguration.framework
3.在Header Search Paths添加AlipaySDK路徑
4.在.m文件中
1 導入頭文件
#import"AlipaySDK/AlipaySDK.h"
#import"DataSigner.h"
#import"Order.h"
2 添加PID/用戶賬號/私鑰
//PID
#define PartnerID @"2088212441087012"
//用戶賬號
#define SellerID @"yourqqnumber@qq.com"
//私鑰
#define RSAPrivateKey @“私鑰"
3 生成訂單
//生成點單
Order*order = [[Orderalloc]init];
//合作者身份(PID)
order.partner=PartnerID;
//商家支付寶賬號
order.seller=SellerID;
//訂單ID
order.tradeNO=@"234";
order.productName=@"商品名稱";
order.productDescription=@"商品描述";
//商品價格
order.amount=@"124";
//回調URL
order.notifyURL=@"http://url";
//Bundle ID
order.service=@"com.chenglong.FCLZhiFuBaoDemo";
order.paymentType=@"1";
//編碼格式
order.inputCharset=@"utf-8";
//訂單超時時間
order.itBPay=@"45m";
//應用注冊scheme,在AlixPayDemo-Info.plist定義URL types
NSString*appSchem =@"FCLZhiFuBaoDemo";
//將商品信息拼接成字符串
NSString*orderSpec = [orderdescription];
//使用私鑰進行簽名
idsigner =CreateRSADataSigner(RSAPrivateKey);
//簽名我的訂單描述
NSString*sinedString = [signersignString:orderSpec];
NSString*orderString =nil;
if(sinedString !=nil) {
orderString = [NSStringstringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",orderSpec,sinedString,@"RSA"];
[[AlipaySDKdefaultService]payOrder:orderStringfromScheme:appSchemcallback:^(NSDictionary*resultDic) {
NSLog(@"result= %@",resultDic);
}];
}
5.在入口類添加回調方法
-(BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{
//支付后的回調
//當通過別的應用程序,將該應用程序打開時,會調用該方法
if([url.hostisEqualToString:@"safepay"]) {
//當用戶通過支付寶客戶端進行支付時,會回調該block:standbyCallbac
[[AlipaySDKdefaultService]processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary*resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
returnYES;
}
注意:使用支付寶前提是購買的物品必須是和應用無關的。比如衣服、電子產品。游戲中的道具是需要通過內購(蘋果)購買。