官方已是建議使用服務器簽名來接入微信支付,實際上從安全上考慮,確實是每一個客戶端不該該知道RAS密鑰,也不須要每一個客戶端都寫一遍簽名的算法。php
服務端接入流程文檔:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_3算法
商戶系統和微信支付系統主要交互說明:sql
步驟1:用戶在商戶APP中選擇商品,提交訂單,選擇微信支付。json
步驟2:商戶後臺收到用戶支付單,調用微信支付統一下單接口。參見【統一下單API】。api
步驟3:統一下單接口返回正常的prepay_id,再按簽名規範從新生成簽名後,將數據傳輸給APP。參與簽名的字段名爲appId,partnerId,prepayId,nonceStr,timeStamp,package。注意:package的值格式爲Sign=WXPay安全
步驟4:商戶APP調起微信支付。api參見本章節【app端開發步驟說明】服務器
步驟5:商戶後臺接收支付通知。api參見【支付結果通知API】微信
步驟6:商戶後臺查詢支付結果。,api參見【查詢訂單API】app
1.導入SDK文件ide
2.導入相關的系統庫及文件。不導入會報錯。
3.配置info.plist
a.schemes ,注意,item0 這裏要修改爲商戶本身的APPID
或者這樣修改:
b.白名單
c.安全配置支持Http
固然這部分的配置,也能夠經過修改XML來實現。
4.修改bitcode。
修改:
5.在AppDelegate 註冊微信
導入#import "WXApiManager.h"
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 //向微信註冊wxd930ea5d5a258f4f 3 [WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:@"demo 2.0"]; 4 5 return YES; 6 } 7 8 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 9 return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; 10 } 11 12 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 13 return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; 14 }
6.接下來,就是發起請求支付了,實現上核心代碼只有幾行。
這部分代碼在demo的WXApiRequestHandler--》jumpToBizPay 裏。
1 // 調起微信支付 2 PayReq* req = [[[PayReq alloc] init]autorelease]; 3 req.partnerId = [dict objectForKey:@"partnerid"]; 4 req.prepayId = [dict objectForKey:@"prepayid"]; 5 req.nonceStr = [dict objectForKey:@"noncestr"]; 6 req.timeStamp = stamp.intValue; 7 req.package = [dict objectForKey:@"package"]; 8 req.sign = [dict objectForKey:@"sign"]; 9 [WXApi sendReq:req];
7.無論支付成功仍是失敗,結果會返回到WXApiManager--》onResp 方法下
1 switch (resp.errCode) { 2 case WXSuccess: 3 strMsg = @"支付結果:成功!"; 4 NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode); 5 break; 6 7 default: 8 strMsg = [NSString stringWithFormat:@"支付結果:失敗!retcode = %d, retstr = %@", resp.errCode,resp.errStr]; 9 NSLog(@"錯誤,retcode = %d, retstr = %@", resp.errCode,resp.errStr); 10 break; 11 }
咱們直接處理回調結果便可。
1.導入文件。官方已經不提供這個SDK的下載了,我已經打包到源代碼了,2015年3月11號最新修改的版本
2.裏面有兩個文件是非arc的,咱們須要設置一下 -fno-objc-arc
3.導入系統庫及info.list配置,請參數上面服務端簽名。
4.AppDelegate配置
1)導入頭文件
1 #import "WXApi.h" 2 #import "payRequsestHandler.h"
2)實現微信代理
1 @interface AppDelegate ()<WXApiDelegate> 2 3 @end
3)註冊微信,及微信支付回調
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization after application launch. 3 //向微信註冊 4 [WXApi registerApp:APP_ID withDescription:@"demo 2.0"]; 5 6 return YES; 7 } 8 9 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 10 { 11 return [WXApi handleOpenURL:url delegate:self]; 12 } 13 14 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 15 { 16 return [WXApi handleOpenURL:url delegate:self]; 17 } 18 19 // 微信支付成功或者失敗回調 20 -(void) onResp:(BaseResp*)resp 21 { 22 NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode]; 23 NSString *strTitle; 24 25 if([resp isKindOfClass:[SendMessageToWXResp class]]) 26 { 27 strTitle = [NSString stringWithFormat:@"發送媒體消息結果"]; 28 } 29 if([resp isKindOfClass:[PayResp class]]){ 30 // 支付返回結果,實際支付結果須要去微信服務器端查詢 31 strTitle = [NSString stringWithFormat:@"支付結果"]; 32 33 switch (resp.errCode) { 34 case WXSuccess: 35 strMsg = @"支付結果:成功!"; 36 NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode); 37 break; 38 39 default: 40 strMsg = [NSString stringWithFormat:@"支付結果:失敗!retcode = %d, retstr = %@", resp.errCode,resp.errStr]; 41 NSLog(@"錯誤,retcode = %d, retstr = %@", resp.errCode,resp.errStr); 42 break; 43 } 44 } 45 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 46 [alert show]; 47 }
5.寫方法直接在支付時調用
1 - (void)payForWechat 2 { 3 // 建立支付簽名對象 4 payRequsestHandler *req = [[payRequsestHandler alloc] init]; 5 // 初始化支付簽名對象 6 [req init:APP_ID mch_id:MCH_ID]; 7 // 設置密鑰 8 [req setKey:PARTNER_ID]; 9 10 NSMutableDictionary *dict = [req sendPay_demo]; 11 12 if(dict != nil){ 13 NSMutableString *retcode = [dict objectForKey:@"retcode"]; 14 if (retcode.intValue == 0){ 15 NSMutableString *stamp = [dict objectForKey:@"timestamp"]; 16 17 // 調起微信支付 18 PayReq* req = [[PayReq alloc] init]; 19 req.openID = [dict objectForKey:@"appid"]; 20 req.partnerId = [dict objectForKey:@"partnerid"]; 21 req.prepayId = [dict objectForKey:@"prepayid"]; 22 req.nonceStr = [dict objectForKey:@"noncestr"]; 23 req.timeStamp = stamp.intValue; 24 req.package = [dict objectForKey:@"package"]; 25 req.sign = [dict objectForKey:@"sign"]; 26 [WXApi sendReq:req]; 27 //日誌輸出 28 NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",req.openID,req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign ); 29 }else{ 30 [self alert:@"提示信息" msg:[dict objectForKey:@"retmsg"]]; 31 } 32 }else{ 33 [self alert:@"提示信息" msg:@"服務器返回錯誤,未獲取到json對象"]; 34 } 35 } 36 37 // 客戶端提示信息 38 - (void)alert:(NSString *)title msg:(NSString *)msg 39 { 40 UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 41 42 [alter show]; 43 }