目前項(xiàng)目里有微信支付的需求,調(diào)研過一段時(shí)間后编检,發(fā)現(xiàn)其實(shí)并沒有想象中的那么困難胎食。如果你只是想實(shí)現(xiàn)該功能,一個(gè)方法足以允懂,但是若你想深入了解實(shí)現(xiàn)原理厕怜。就需要花費(fèi)更多的功夫了。目前我只清楚微信支付需要做簽名,一種是在后臺(tái)簽名粥航,一種是自己在前端簽名舵揭。其實(shí)這對(duì)前端來說代碼量并沒有多大的改變,最大的區(qū)別是在后臺(tái)簽名邏輯更容易理解躁锡,而在前端簽名邏輯有些混亂。
現(xiàn)在置侍,小編也只是知道怎么使用映之,而不得其精髓,現(xiàn)在我附上我的學(xué)習(xí)鏈接蜡坊,為了避免以后鏈接出現(xiàn)不可用情況杠输,請(qǐng)各位理解小編的粘貼復(fù)制。
博客園(幻想無極) http://www.cnblogs.com/hxwj/p/4563351.html
微信支付開發(fā)文檔? ? http://mch.weixin.qq.com/wiki/doc/api/app.php?chapter=8_1
iOS第三方支付-微信支付
微信支付用到的文件
1.首先支持非arc
2.設(shè)置URL types
3.AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];
return YES;
}
4.微信回調(diào)
//處理微信通過URL啟動(dòng)App時(shí)傳遞的數(shù)據(jù)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [WXApi handleOpenURL:url delegate:self];
}
//微信回調(diào)
- (void)onResp:(BaseResp *)resp {
//errCode
switch (resp.errCode) {
case WXSuccess:
//成功回調(diào)
break;
default:
break;
}
}
5.調(diào)用微信支付(主要代碼秕衙,后臺(tái)簽名)
//調(diào)起微信支付
PayReq* req = [[PayReq alloc] init];
req.openID? ? ? = APPI_ID;
req.partnerId? = PARTNER_ID;
req.prepayId? ? = prePayid;
req.nonceStr? ? = nonce_str;
req.timeStamp? = now;
req.package? ? = package;
req.sign? ? ? ? = sign;
[WXApi safeSendReq:req];
6.如果要在程序簽名
?
//
//? ViewController.m
//? WxPay
//
//? Created by apple on 15/6/9.
//? Copyright (c) 2015年 tqh. All rights reserved.
//
#import "ViewController.h"
#import "payRequsestHandler.h"
#import "WXApi.h"
@interface ViewController () {
enum WXScene _scene;
NSString *Token;
long token_time;
}
@end
@implementation ViewController
- (instancetype)init
{
self = [super init];
if (self) {
_scene = WXSceneSession;
}
token_time = 0;
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
//向微信注冊(cè)
//微信支付
UIButton *but2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 100, 100, 40)];
[but2 setTitle:@"微信支付" forState:UIControlStateNormal];
but2.backgroundColor = [UIColor lightGrayColor];
[but2 addTarget:self action:@selector(wxpay) forControlEvents:UIControlEventTouchUpInside];
[but2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:but2];
}
//微信支付
- (void)wxpay
{
//商戶號(hào)
NSString *PARTNER_ID? ? = @"1900000109";
//商戶密鑰
NSString *PARTNER_KEY? = @"8934e7d15453e97507ef794cf7b0519d";
//APPID
NSString *APPI_ID? ? ? = @"wxd930ea5d5a258f4f";
//appsecret
NSString *APP_SECRET? ? = @"db426a9829e4b49a0dcac7b4162da6b6";
//支付密鑰
NSString *APP_KEY? ? ? = @"L8LrMqqeGRxST5reouB0K66CaYAWpqhAVsq7ggKkxHCOastWksvuX1uvmvQclxaHoYd3ElNBrNO2DHnnzgfVG9Qs473M3DTOZug5er46FhuGofumV8H2FVR9qkjSlC5K";
//支付結(jié)果回調(diào)頁面
NSString *NOTIFY_URL? ? = @"http://localhost/pay/wx/notify_url.asp";
//訂單標(biāo)題
NSString *ORDER_NAME? ? = @"Ios客戶端簽名支付 測試";
//訂單金額,單位(分)
NSString *ORDER_PRICE? = @"1";
//創(chuàng)建支付簽名對(duì)象
payRequsestHandler *req = [payRequsestHandler alloc];
//初始化支付簽名對(duì)象
[req init:APPI_ID app_secret:APP_SECRET partner_key:PARTNER_KEY app_key:APP_KEY];
//判斷Token過期時(shí)間蠢甲,10分鐘內(nèi)不重復(fù)獲取,測試帳號(hào)多個(gè)使用,可能造成其他地方獲取后不能用据忘,需要即時(shí)獲取
time_t? now;
time(&now);
//if ( (now - token_time) > 0 )//非測試帳號(hào)調(diào)試請(qǐng)啟用該條件判斷
{
//獲取Token
Token? ? ? ? ? ? ? ? ? = [req GetToken];
//設(shè)置Token有效期為10分鐘
token_time? ? ? ? ? ? ? = now + 600;
//日志輸出
NSLog(@"獲取Token: %@\n",[req getDebugifo]);
}
if ( Token != nil){
//================================
//預(yù)付單參數(shù)訂單設(shè)置
//================================
NSMutableDictionary *packageParams = [NSMutableDictionary dictionary];
[packageParams setObject: @"WX"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"bank_type"];
[packageParams setObject: ORDER_NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"body"];
[packageParams setObject: @"1"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"fee_type"];
[packageParams setObject: @"UTF-8"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"input_charset"];
[packageParams setObject: NOTIFY_URL? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"notify_url"];
[packageParams setObject: [NSString stringWithFormat:@"%ld",time(0)]? ? ? ? forKey:@"out_trade_no"];
[packageParams setObject: PARTNER_ID? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"partner"];
[packageParams setObject: @"196.168.1.1"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"spbill_create_ip"];
[packageParams setObject: ORDER_PRICE? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"total_fee"];
NSString? ? *package, *time_stamp, *nonce_str, *traceid;
//獲取package包
package? ? ? ? = [req genPackage:packageParams];
//輸出debug info
NSString *debug? ? = [req getDebugifo];
NSLog(@"gen package: %@\n",package);
NSLog(@"生成package: %@\n",debug);
//設(shè)置支付參數(shù)
time_stamp? = [NSString stringWithFormat:@"%ld", now];
nonce_str? ? = [TenpayUtil md5:time_stamp];
traceid? ? ? ? = @"mytestid_001";
NSMutableDictionary *prePayParams = [NSMutableDictionary dictionary];
[prePayParams setObject: APPI_ID? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"appid"];
[prePayParams setObject: APP_KEY? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"appkey"];
[prePayParams setObject: nonce_str? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"noncestr"];
[prePayParams setObject: package? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"package"];
[prePayParams setObject: time_stamp? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"timestamp"];
[prePayParams setObject: traceid? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"traceid"];
//生成支付簽名
NSString? ? *sign;
sign? ? ? ? = [req createSHA1Sign:prePayParams];
//增加非參與簽名的額外參數(shù)
[prePayParams setObject: @"sha1"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"sign_method"];
[prePayParams setObject: sign? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"app_signature"];
//獲取prepayId
NSString *prePayid;
prePayid? ? ? ? ? ? = [req sendPrepay:prePayParams];
//輸出debug info
debug? ? ? ? ? ? ? = [req getDebugifo];
NSLog(@"提交預(yù)付單: %@\n",debug);
if ( prePayid != nil) {
//重新按提交格式組包鹦牛,微信客戶端5.0.3以前版本只支持package=Sign=***格式,須考慮升級(jí)后支持?jǐn)y帶package具體參數(shù)的情況
//package? ? ? = [NSString stringWithFormat:@"Sign=%@",package];
package? ? ? ? = @"Sign=WXPay";
//簽名參數(shù)列表
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
[signParams setObject: APPI_ID? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"appid"];
[signParams setObject: APP_KEY? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"appkey"];
[signParams setObject: nonce_str? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"noncestr"];
[signParams setObject: package? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"package"];
[signParams setObject: PARTNER_ID? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"partnerid"];
[signParams setObject: time_stamp? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"timestamp"];
[signParams setObject: prePayid? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:@"prepayid"];
//生成簽名
sign? ? ? ? = [req createSHA1Sign:signParams];
//輸出debug info
debug? ? = [req getDebugifo];
NSLog(@"調(diào)起支付簽名: %@\n",debug);
//調(diào)起微信支付
PayReq* req = [[PayReq alloc] init];
req.openID? ? ? = APPI_ID;
req.partnerId? = PARTNER_ID;
req.prepayId? ? = prePayid;
req.nonceStr? ? = nonce_str;
req.timeStamp? = now;
req.package? ? = package;
req.sign? ? ? ? = sign;
[WXApi safeSendReq:req];
}else{
/*long errcode = [req getLasterrCode];
if ( errcode == 40001 )
{//Token實(shí)效勇吊,重新獲取
Token? ? ? ? ? ? ? ? ? = [req GetToken];
token_time? ? ? ? ? ? ? = now + 600;
NSLog(@"獲取Token: %@\n",[req getDebugifo]);
};*/
NSLog(@"獲取prepayid失敗\n");
[self alert:@"提示信息" msg:debug];
}
}else{
NSLog(@"獲取Token失敗\n");
[self alert:@"提示信息" msg:@"獲取Token失敗"];
}
}
//客戶端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
//- (void)onReq:(BaseReq *)req {
//? ? NSLog(@"qwe123");
//}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
DEMO下載地址:http://pan.baidu.com/s/1jGjCaJc
補(bǔ):有些童鞋出現(xiàn)跳到微信支付又跳回來的情況一般是你集成了第三方分享功能需要進(jìn)行一下判斷
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
上面方法里面進(jìn)行是不是微信支付的判斷
if ([url.host isEqualToString:@"pay"]) {
[WXApi handleOpenURL:url delegate:self];
}
return YES;