看到個不錯的微信集成帖子铣缠,分享給大家:嬷觥(因為微信支付也真是夠惡心的,TM要配置什么環(huán)境都沒說蝗蛙,肯定很多新手會被惡心死Sァ)
微信支付的開發(fā)!(文檔自己做的捡硅,僅供參考)
前言:之前聽說過微信支付有很多坑哮内,其實沒有想象的那么坑,整體感覺很容易上手壮韭,按照它的流程來不會有錯北发!PS:官方的流程看的TMD煩,好啦喷屋,廢話有點多琳拨,進入開發(fā)。(ps:每個微信的版本一直都在更新逼蒙,這是2015/6/1給你們做的標記下QQ:1242384226,歡迎隨便騷擾从绘,女士優(yōu)先!大神也優(yōu)先是牢!咦…)
一:怎么用官方DEMO
1.????申請得到AppID秘鑰什么的這里不寫了僵井。
2.????下載官方DEMO,真機測試,目的是你看看里面需要哪些參數(shù)驳棱,實現(xiàn)哪些方法等等批什。
3.
4.????問題來了,測試不了社搅,解決:因為你沒有更改商戶相關(guān)參數(shù)
(劃線位置添加AppID)
5.
6.????把相關(guān)商戶參數(shù)AppID,AppSECRET,MCH_ID(商戶號),填好就行
7.????運行以下微信官方demo真機 調(diào)試截圖
8.???? 《我的手機5S驻债,最近準備*****買6Plus乳规,親們能支援下我嘛,哈哈!》
9.???? 《有了加密的東西合呐,模擬后臺演示暮的,我是不是很好》
10.???? 啟動圖,說明調(diào)其支付淌实,強行一波帶走
11.???? 《啟動圖都給你截出來了冻辩,一開始我是拒絕的》
12.???? (能支付了,我擦類(河南話2鹌怼)恨闪,不錯哦!)
13.???? 《1分錢放坏,小意思啦咙咽!》
14.???? 《我想靜靜的等待》
15.???? 《1分錢就這樣沒了》
16.????點擊返回就可以回去啦!官方DEMO ,演示結(jié)束淤年,親钧敞!能來個逼格的App嘛?麸粮?
二:逼格APP,(掌拍藝術(shù)犁享!2個月做完的,原諒我的界面殺馬特…)
1.????導入微信支付庫
微信開放平臺新增了微信模塊用戶統(tǒng)計功能豹休,便于開發(fā)者統(tǒng)計微信功能模塊的用戶使用和活躍情況。開發(fā)者需要在工程中鏈接上:SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib桨吊。
最重要的時這個庫:libc++.dylib《ps:官方的文檔沒說威根,艸!》
2.在AppDelegate中導入:
(1)#import "WXApi.h"
#import "WXApiObject.h"
遵守WXApiDelegate
在這個方法里注冊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:WXAppId withDescription:@"yishuPayDes"];
}
(2)跳轉(zhuǎn)處理
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
NSLog(@"跳轉(zhuǎn)到URL schema中配置的地址-->%@",url);//跳轉(zhuǎn)到URL schema中配置的地址
if ([UMSocialSnsService handleOpenURL:url]) {
return??[UMSocialSnsService handleOpenURL:url];
}else{
return [WXApi handleOpenURL:url delegate:self];
}
}
(3)回調(diào)方法
-(void) onResp:(BaseResp*)resp
{
NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
NSString *strTitle;
if([resp isKindOfClass:[SendMessageToWXResp class]])
{
strTitle = [NSString stringWithFormat:@"發(fā)送媒體消息結(jié)果"];
}
if([resp isKindOfClass:[PayResp class]]){
//支付返回結(jié)果视乐,實際支付結(jié)果需要去微信服務(wù)器端查詢
strTitle = [NSString stringWithFormat:@"支付結(jié)果"];
switch (resp.errCode) {
case WXSuccess:{
strMsg = @"支付結(jié)果:成功洛搀!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
default:{
strMsg = [NSString stringWithFormat:@"支付結(jié)果:失斢拥怼留美!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"錯誤,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"fail"];
[[NSNotificationCenter defaultCenter] postNotification:notification];
break;
}
}
}
//????UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//????[alert show];
}
說明:這里掌拍藝術(shù)App的調(diào)試和真機伸刃,建議把通知的東西注銷谎砾,打開alert,方便測試,便于檢查回調(diào)錯誤信息捧颅,如果你夠牛景图,可以無視,代碼難看碉哑,見諒挚币,但是人很帥亮蒋,哈哈!
(4).接下來在需要支付的界面做這些事:
//監(jiān)聽通知
- (void)viewWillAppear:(BOOL)animated{
[self requestDownloadData];
if([WXApi isWXAppInstalled]) // 判斷 用戶是否安裝微信
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:ORDER_PAY_NOTIFICATION object:nil];//監(jiān)聽一個通知
}
[super viewWillAppear:animated];
}
/*ORDER_PAY_NOTIFICATION*/這個寫個宏妆毕,全局里寫慎玖,怎么寫?建.h文件笛粘!
//移除通知
- (void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
開始支付-----→終于等到你趁怔,還好沒放棄!
1.????預備工作闰蛔,(1)我這里封裝了下載類AF(自己感覺比較方便痕钢,親們自己寫下載就好了,因為我們公司的網(wǎng)絡(luò)數(shù)據(jù)就那么幾種)主要用于請求后臺服務(wù)器已經(jīng)做好的數(shù)據(jù)序六,請求下來的參數(shù)給微信任连,用于支付!(2)獲取每臺設(shè)備的IP地址,(3)HUD是啥例诀,大家都用過随抠,不說了(ps:HUD特效,自己定義看看那種效果好!)(4)后臺做了什么:http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php這個地址給后臺參考下繁涂,需要的參數(shù)都在上面移動端不需要寫拱她,如果你要寫,我不攔你…哈哈扔罪,當練手吧!
2.????代碼
#pragma mark - 微信支付
- (void)WeiXinPay{
if([WXApi isWXAppInstalled]) // 判斷 用戶是否安裝微信
{
HUD.delegate = self;
HUD.labelText = @"正在為您支付...";
[HUD show:YES];
NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
NSString *ipAdress = [MyHttpDownload GetIPAddress:YES];
NSLog(@"ipAdress%@",ipAdress);
NSLog(@"self.order_orderinfoid%@",self.order_orderinfoid);
NSLog(@"提交地址%@",[NSString stringWithFormat:TESTWXPayUrl,userID,self.order_orderinfoid,_WXPayStyleStr,ipAdress]);
NSDictionary *dict = @{@"uid":userID,@"orderinfo_id":self.order_orderinfoid,@"type":_WXPayStyleStr,@"ip":ipAdress};
[MyHttpDownload GetDownload:WXPayUrl param:dict finish:^(NSData *data, NSDictionary *obj, NSError *error) {
if ([obj[@"data"] isKindOfClass:[NSDictionary class]]) {
NSDictionary *dataDict = obj[@"data"];
NSLog(@"respose信息--》%@",dataDict);
if (obj != nil) {
[self WXPayRequest:dataDict[@"appid"] nonceStr:dataDict[@"noncestr"] package:dataDict[@"package"] partnerId:dataDict[@"partnerid"] prepayId:dataDict[@"prepayid"] timeStamp:dataDict[@"timestamp"] sign:dataDict[@"sign"]];
}else{
[HUD hide:YES];
FlyAlertView *alert = [[FlyAlertView alloc] initWithTitle:@"提示" contentText:@"網(wǎng)絡(luò)有誤" leftButtonTitle:nil rightButtonTitle:@"確定"];
[alert show];
}
}else{
[HUD hide:YES];
NSString *mess = [NSString stringWithFormat:@"%@,退出重試!",obj[@"data"]];
[self alert:@"提示" msg:mess];
}
}];
}else{
[HUD hide:YES];
[self alert:@"提示" msg:@"您未安裝微信!"];
}
}
#pragma mark - 發(fā)起支付請求
- (void)WXPayRequest:(NSString *)appId nonceStr:(NSString *)nonceStr package:(NSString *)package partnerId:(NSString *)partnerId prepayId:(NSString *)prepayId timeStamp:(NSString *)timeStamp sign:(NSString *)sign{
//調(diào)起微信支付
PayReq* wxreq???????????? = [[PayReq alloc] init];
wxreq.openID??????????????= WXAppId;
wxreq.partnerId?????????? = partnerId;
wxreq.prepayId????????????= prepayId;
wxreq.nonceStr????????????= nonceStr;
wxreq.timeStamp?????????? = [timeStamp intValue];
wxreq.package???????????? = package;
wxreq.sign????????????????= sign;
[WXApi sendReq:wxreq];
}
#pragma mark - 通知信息
- (void)getOrderPayResult:(NSNotification *)notification{
if ([notification.object isEqualToString:@"success"])
{
[HUD hide:YES];
[self alert:@"恭喜" msg:@"您已成功支付啦!"];
payStatusStr?????????? = @"YES";
_successPayView.hidden = NO;
_toPayView.hidden??????= YES;
[self creatPaySuccess];
}
else
{
[HUD hide:YES];
[self alert:@"提示" msg:@"支付失敗"];
}
}
//客戶端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
轉(zhuǎn)載 -- http://www.cocoachina.com/bbs/read.php?tid=303132