最簡單明了的教程還是官方文檔,以下是我給應用添加支付功能,記錄下。
1、微信支付(官方文檔:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5)
假設你的應用在微信開放平臺已經創建、通過審核并獲取了支付功能。
拿到的相應參數、項目中成功導入微信支付SDK
APP_ID-----公眾賬號ID
partnerId---商戶號
API_KEY----商戶秘鑰
1-1、在AppDelegate設置相應的函數
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//向微信注冊
[WXApi registerApp:APP_ID ?withDescription:@"demo 2.0"];
returnYES;
}?
//授權后回調WXApiDelegate
-(void)onResp:(BaseReq*)resp{
if([respisKindOfClass:[PayRespclass]]){
//寫個通知、告訴之前微信支付所在的頁面響應相應的動作NSString*paySuccessWeixinMsg=nil;
paySuccessWeixinMsg=[[NSStringalloc]initWithFormat:@"%d",aresp.errCode];
[[NSNotificationCenterdefaultCenter]postNotificationName:@"PayStateWeixin"object:paySuccessWeixinMsg];
}
}
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url {
return[WXApihandleOpenURL:urldelegate:[WXApiManagersharedManager]];
}
-(void)onResp:(BaseReq*)resp{
//返回app的回調
- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation {
return[WXApihandleOpenURL:urldelegate:[WXApiManagersharedManager]];
}
微信支付跟支付寶支付還不太一樣,在調起手機微信客戶端支付的時得先獲取prepay_id,這一步也就是官方文檔說的統一下單API。統一下單API在我們的服務器端完成,通過接口返回prepay_id。接下來就是調用手機端微信支付了以下是VC中調用支付的關鍵代碼:發起成功后就會調AppDelegate中的-(void)onResp:(BaseReq*)resp;
-(void)weiXinPayAction:(NSString*)out_trade_noStr prepay_id:(NSString*)prepay_idStr{
payRequsestHandler*handler=[[payRequsestHandleralloc]init];
intrandomX =arc4random() %10000;
NSDate* date = [NSDatedateWithTimeIntervalSinceNow:0];
inttime_stamp=(int)[datetimeIntervalSince1970];
PayReq*request=[[PayReqalloc]init];
request.openID=APP_ID;//公眾賬號ID
request.partnerId=partnerId;//商戶號
request.prepayId=prepay_idStr;//(接口返回--預支付交易會話ID)
request.package=@"Sign=WXPay";//(固定寫法)
request.nonceStr=[WXUtilmd5:[[NSStringalloc]initWithFormat:@"%d",randomX]];//隨機字符串
request.timeStamp= time_stamp;//時間戳
NSDictionary*parameters =@{@"appid":APP_ID,@"partnerid":partnerId,@"prepayid":prepay_idStr,@"package":@"Sign=WXPay",@"noncestr":request.nonceStr,@"timestamp":[[NSStringalloc]initWithFormat:@"%d",time_stamp]};
NSMutableDictionary*mutableDic=[[NSMutableDictionaryalloc]initWithDictionary:parameters];
[handlersetKey:API_KEY];
request.sign=[handlercreateMd5Sign:mutableDic];
[WXApisendReq:request];
}
以上是實現微信支付需要在app端添加的代碼。
如果在微信支付的時候,跳轉到微信支付界面彈出了一個確定按鈕,頁面空白,這個很有可能是你的APP_SECRET(商戶密鑰)沒有設置對,這東西的作用是在對支付參數進行md5加密時所用到的key。
2、支付寶支付(官方文檔:http://open.alipay.com/platform/document.htm#down)
應用已在支付寶開放平臺審核通過,獲取相應的參數。項目中成功導入支付寶SDK結合官方Demo。
再說一點下:有些童鞋在導入支付寶SDK時rsa.h中會報這個錯誤:#include <openssl/asn1.h>,這是由于在編譯時Xcode找不到<openssl/asn1.h>文件,在TARGETS->Build Settings->Header Search Paths添加:$(SRCROOT)/PayDemoTest/ThirdPart/Pay/Alipay,在項目中的目錄結構。
PARTNER---合作身份者ID--商家賬戶中找到!以2088開頭
SELLER-----支付寶收款賬號
PRIVATEKEY-商戶方的私鑰,pkcs8格式
在AppDelegate中添加該方法
- (BOOL)application:(UIApplication*)application?openURL:(NSURL*)url?sourceApplication:(NSString*)sourceApplication?annotation:(id)annotation {
//跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDKdefaultService]processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary*resultDic) {
NSLog(@"result = %@",resultDic);
}];
returnYES;
}
在支付的VC頁面,調用我們自己的服務器后臺接口獲取tradeNO(訂單ID由商家自行制定),然后初始化訂單對象,并賦予相應的值
-(void)zhiFuBaoAction:(NSString*)out_trade_no{
//生成訂單信息及簽名,將商品信息賦予AlixPayOrder的成員變量
Order*order = [[Orderalloc]init];
order.partner=PARTNER;//合作身份者ID
order.seller=SELLER;//支付寶收款賬號
order.tradeNO= out_trade_no;//訂單ID(由商家自行制定)
order.productName=@"商品標題";//商品標題
order.productDescription=@"商品描述";//商品描述
order.amount= [NSStringstringWithFormat:@"%.2f",[moneyTF.textfloatValue]];//商品價格
order.notifyURL=[[NSStringalloc]initWithFormat:@"%@/pay/alipay/receive_notify",SERVERURL];//回調URL
order.service=@"mobile.securitypay.pay";
order.paymentType=@"1";
order.inputCharset=@"utf-8";
order.itBPay=@"30m";
order.showUrl=@"m.alipay.com";
//應用注冊scheme,在AlixPayDemo-Info.plist定義URL types
NSString*appScheme =@"Yunshu";
//將商品信息拼接成字符串
NSString*orderSpec = [orderdescription];
NSLog(@"orderSpec = %@",orderSpec);
//獲取私鑰并將商戶信息簽名,外部商戶可以根據情況存放私鑰和簽名,只需要遵循RSA簽名規范,并將簽名字符串base64編碼和UrlEncode
id signer =CreateRSADataSigner(PRIVATEKEY);
NSString*signedString = [signersignString:orderSpec];
//將簽名成功字符串格式化為訂單字符串,請嚴格按照該格式
NSString*orderString =nil;
if(signedString !=nil) {
orderString = [NSStringstringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString,@"RSA"];
[[AlipaySDKdefaultService]payOrder:orderStringfromScheme:appSchemecallback:^(NSDictionary*resultDic) {
NSLog(@"reslut = %@",resultDic);
NSString*memoStr=[resultDicobjectForKey:@"memo"];
NSString*resultStatus=[resultDicobjectForKey:@"resultStatus"];
if([resultStatusisEqualToString:@"9000"]){
PaySuccesViewController*paySuccessVC=[[PaySuccesViewControlleralloc]initWithMoney:moneyTF.textpayStyle:@"支付寶"out_trade_noStr:nil];
[self.navigationControllerpushViewController:paySuccessVCanimated:YES];
//發一個通知,提醒支付成功,刷新列表
[[NSNotificationCenterdefaultCenter]postNotificationName:@"RefreshCallsList"object:nil];
}elseif([resultStatusisEqualToString:@"4000"]) {
[HUDCommonViewHUDErrorWithHudSuperView:self.viewinfoStr:memoStr];
//self.navView.titleLab.text=@"充值失敗";
}elseif([resultStatusisEqualToString:@"6001"]) {
[HUDCommonViewHUDErrorWithHudSuperView:self.viewinfoStr:memoStr];
//self.navView.titleLab.text=@"充值失敗";
}elseif([resultStatusisEqualToString:@"6002"]) {
[HUDCommonViewHUDErrorWithHudSuperView:self.viewinfoStr:memoStr];
//self.navView.titleLab.text=@"充值失敗";
}}];}}}
以上是實現支付寶支付功能在app端需要添加的代碼.