?一、軟硬件環(huán)境
類型:移動(dòng)支付
目標(biāo):給公司的App集成支付寶付款功能
開發(fā)環(huán)境:OSX 10.11, Xcode7.1,
SDK:支付寶錢包支付接口開發(fā)包2.0標(biāo)準(zhǔn)版
查看移動(dòng)支付的相關(guān)說明文檔牺荠、下載SDK和Demo返咱,點(diǎn)這里。
二赋铝、集成SDK
step:1: 在工程目錄下新建一個(gè)文件夾,存放支付寶相關(guān)SDK和工具類文件。
step:2: 將SDK中的文件拷貝至新建的文件夾:
Util疫诽、openssl、libssl.a、libcrypto.a奇徒、AlipaySDK.bundle雏亚、AlipaySDK.framework、APAuthV2Info.h摩钙、APAuthV2Info.m罢低、Order.h、Order.m
step:3: 將該文件夾拖入工程胖笛;
step:4: 添加框架SystemConfiguration.framework网持;
step:5: 此時(shí)會(huì)遇到編譯報(bào)錯(cuò)的情況,根據(jù)報(bào)錯(cuò)情況添加相應(yīng)的頭文件即可长踊;
??.在Util文件夾下base64.h中添加 #import<Foundation/Foundation.h>功舀;
??.在Util文件夾下openssl_wrapper.h中添加#import<Foundation/Foundation.h>;
??.如果遇到Error : Lexical or Preprocessor Issue 'openssl/asn1.h' file not found身弊,在工程的Building Setting中查找Framework Search Paths辟汰,添加$(PROJECT_DIR)/AliSDK2_2_3或者$(SRCROOT)/AliSDK2_3_3,其中AliSDK2_3_3為剛才新建的文件夾名阱佛。
??.如遇到其它報(bào)錯(cuò)信息帖汞,請(qǐng)百度或Google自行解決。
step:6: 設(shè)置?跳轉(zhuǎn)參數(shù)
如果手機(jī)裝有支付寶客戶端凑术,則支付時(shí)自動(dòng)調(diào)用該客戶端翩蘸,如果沒有安裝,則調(diào)用網(wǎng)頁(yè)版的支付頁(yè)面淮逊。
在支付完成后根據(jù)跳轉(zhuǎn)參數(shù)回到之前的應(yīng)用鹿鳖。
設(shè)置方法:在工程的Target里找到Info - URL Type,點(diǎn)+號(hào)新建壮莹,在URL Schemes里將App的identifier添加進(jìn)來翅帜,然后在AppDelegate中添加如下代碼:
#pragma mark ?跳轉(zhuǎn)至支付寶App或網(wǎng)頁(yè)版支付寶
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication
annotation:(id)annotation {
//跳轉(zhuǎn)至支付寶App進(jìn)行支付,處理支付結(jié)果
[[AlipaySDKdefaultService]processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary*resultDic) {
#warning 是一坑命满,請(qǐng)注意
//這里不用寫任何代碼涝滴,支付結(jié)果會(huì)在下面的代碼中進(jìn)行處理
}];
returnYES;
}
step:7: 在調(diào)用支付寶App或網(wǎng)頁(yè)版支付寶進(jìn)行支付的viewController里做如下操作:
#pragma mark 支付核心代碼
- (void)alipay {
//獲得公鑰私鑰的方法在支付寶相關(guān)文檔里有說明
//合作者身份(PID)2088開頭的16位數(shù)字。
NSString *partner = @"";
//支付寶賬號(hào)
NSString *seller = @"";
//RSA私鑰轉(zhuǎn)化的pkcs8格式私鑰
NSString *privateKey = @"";
//支付寶提供的公鑰
NSString *publicKey = @"";
//服務(wù)器異步通知頁(yè)面
#warning TODO:需更改
NSString *AlipayNotifyURL = @"";
/*
*生成訂單信息及簽名
*/
//將商品信息賦予AlixPayOrder的成員變量
Order *order = [[Order alloc] init];
order.partner = partner;
order.seller = seller;
#warning TODO:服務(wù)器提供相關(guān)交易信息
order.tradeNO = [self generateTradeNO]; //訂單ID(由商家自行制定)
order.productName = self.product.subject; //商品標(biāo)題
order.productDescription = self.product.body; //商品描述
order.amount = [NSString stringWithFormat:@"%.2f",self.product.price]; //商品價(jià)格
order.notifyURL = @"http://www.xxx.com"; //回調(diào)URL
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1"; //默認(rèn)是1
order.inputCharset = @"utf-8";
order.itBPay = @"30m"; //訂單過期時(shí)間:30分鐘
order.showUrl = @"m.alipay.com";
//需要告訴支付寶剛才給該App定義的URL Types胶台,以便跳轉(zhuǎn)回來的時(shí)候使用
NSString *appScheme = @"test";
//將商品信息拼接成支付寶要求的字符串(自動(dòng)調(diào)用支付寶提供的類來實(shí)現(xiàn))
NSString *orderSpec = [order description];
//獲取私鑰并將商戶信息簽名,外部商戶可以根據(jù)情況存放私鑰和簽名,只需要遵循RSA簽名規(guī)范,并將簽名字符串base64編碼和UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString *signedString = [signer signString:orderSpec];
//將簽名成功字符串格式化為訂單字符串,請(qǐng)嚴(yán)格按照該格式
NSString *orderString = nil;
if (signedString != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
#warning TODO: 以下為自定義內(nèi)容歼疮,用?于處理支付結(jié)果
//支付寶返回結(jié)果
NSDictionary *result = [self.paymentResult handleResultWithDictionary:resultDic andPublicKey:publicKey];
//如果返回結(jié)果為真,則隱藏支付按鈕诈唬,并顯示返回首頁(yè)按鈕
if ([result[@"result"] boolValue] == YES) {
//更新界面label文字
[self.paymentResultLabel setText:result[@"info"] textColor:[UIColor myGreenColor]];
[self hidePaymentResultControll:NO];
} else {
[self.paymentResultLabel setText:result[@"result"] textColor:[UIColor myRedColor] fontSize:18.0];
}
}];
}
}
#pragma mark 隨機(jī)生成訂單號(hào)
#warning TODO:在與服務(wù)器正式調(diào)試時(shí)記得刪除
- (NSString *)generateTradeNO
{
static int kNumber = 15;
NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
for (int i = 0; i < kNumber; i++) {
unsigned index = arc4random() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
[resultStr appendString:oneStr];
}
return resultStr;
}
參考文章: