筆者最近的項目中使用到了支付的功能,也遇到了一些坑。在這里跟大家分享一下。
相關配置
1.向支付寶申請, 與支付寶簽約,獲得商戶ID(partner)和賬號ID(seller)和私鑰(privateKey)
2.下載支付寶SDK,導入需要的一些頭文件。

- 集成中可能遇到的問題
-
1.集成SDK編譯時找不到 openssl/asn1.h 文件
解決方法:Build Settings --> Search Paths --> Header Search paths : $(SRCROOT)/支付寶集成/Classes/Alipay
找不到openssl/asn1.h文件 - 2.鏈接時:找不到 SystemConfiguration.framework 這個庫
解決方法:重新導入這個庫。
3.在Appdelegate里面進行回調的相關配置
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
BOOL result = [UMSocialSnsService handleOpenURL:url];
if ([url.host isEqualToString:@"safepay"]) {
//跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
}];
}
if (result == FALSE) {
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
return result;
}```
``#pragma mark ios9的回調``
-
(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
BOOL result = [UMSocialSnsService handleOpenURL:url];if ([url.host isEqualToString:@"safepay"]) {
//跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {}];
}
if (result == FALSE) {
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
return result;
}```
到了這里,一些基本的配置就已經完成了。
4.建立支付的model,也可以直接使用官方的。主要在于.m文件中desprication方法的實現。
- (NSString *)description {
NSMutableString * discription = [NSMutableString string];
if (self.partner) {
[discription appendFormat:@"partner=\"%@\"", self.partner];
}
if (self.seller) {
[discription appendFormat:@"&seller_id=\"%@\"", self.seller];
}
if (self.tradeNO) {
[discription appendFormat:@"&out_trade_no=\"%@\"", self.tradeNO];
}
if (self.productName) {
[discription appendFormat:@"&subject=\"%@\"", self.productName];
}
if (self.productDescription) {
[discription appendFormat:@"&body=\"%@\"", self.productDescription];
}
if (self.amount) {
[discription appendFormat:@"&total_fee=\"%@\"", self.amount];
}
if (self.notifyURL) {
[discription appendFormat:@"¬ify_url=\"%@\"", self.notifyURL];
}
if (self.service) {
[discription appendFormat:@"&service=\"%@\"",self.service];//mobile.securitypay.pay
}
if (self.paymentType) {
[discription appendFormat:@"&payment_type=\"%@\"",self.paymentType];//1
}
if (self.inputCharset) {
[discription appendFormat:@"&_input_charset=\"%@\"",self.inputCharset];//utf-8
}
if (self.itBPay) {
[discription appendFormat:@"&it_b_pay=\"%@\"",self.itBPay];//30m
}
if (self.showUrl) {
[discription appendFormat:@"&return_url=\"%@\"",self.showUrl];//m.alipay.com
}
if (self.out_context) {
[discription appendFormat:@"&out_context=\"%@\"",self.out_context];//
}
// if (self.sign) {
// [discription appendFormat:@"&sign=\"%@\"",self.sign];
//
// }
// if (self.rsaDate) {
// [discription appendFormat:@"&sign_date=\"%@\"",self.rsaDate];
// }
// if (self.appID) {
// [discription appendFormat:@"&app_id=\"%@\"",self.appID];
// }
// for (NSString * key in [self.extraParams allKeys]) {
// [discription appendFormat:@"&%@=\"%@\"", key, [self.extraParams objectForKey:key]];
// }
return discription;
}
調起支付
在點擊支付的按鈕中,添加事件,可以參考官方的demo,用客戶端進行簽名,不過一般都是服務器返回數據的。這邊給大家參考一下。

把這個model發送給支付寶就可以了 ,但是在這之前需要拼接這些數據,可以參考上面的介紹。我這邊是將這個模型帶回到了控制器里面,在調起支付后,發送給支付寶。

這里面主要的方法:
NSString *orderSpec = [model description];
// NSLog(@"00000000%@",orderSpec) ;
//url轉義
//獲取私鑰并將商戶信息簽名,外部商戶可以根據情況存放私鑰和簽名,只需要遵循RSA簽名規范,并將簽名字符串base64編碼和UrlEncode
// id<DataSigner> signer = CreateRSADataSigner(privateKey);
// NSString *signedString = [signer signString:orderSpec];
NSString *signedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)model.sign, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8));
// NSString * signedString = [NSString stringWithFormat:@"%@",model.sign];
//將簽名成功字符串格式化為訂單字符串,請嚴格按照該格式
NSString *orderString = nil;
if (signedString != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
// NSLog(@"%@",orderString);
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"%@",resultDic);
NSString *resultStatus = [resultDic objectForKey:@"resultStatus"] ;
if ([resultStatus isEqualToString:@"9000"]){
[self successView];
}
else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"支付失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil] ;
[alertView show] ;
}
}];
}
} failure:^{
}];