iOS微信支付集成

看到個不錯的微信集成帖子,分享給大家?。ㄒ驗槲⑿胖Ц兑舱媸菈驉盒牡模琓M要配置什么環(huán)境都沒說,肯定很多新手會被惡心死!)


微信支付的開發(fā)?。ㄎ臋n自己做的,僅供參考)

前言:之前聽說過微信支付有很多坑,其實沒有想象的那么坑,整體感覺很容易上手,按照它的流程來不會有錯!PS:官方的流程看的TMD煩,好啦,廢話有點多,進入開發(fā)。(ps:每個微信的版本一直都在更新,這是2015/6/1給你們做的標記下QQ:1242384226,歡迎隨便騷擾,女士優(yōu)先!大神也優(yōu)先!咦…)

一:怎么用官方DEMO

1.????申請得到AppID秘鑰什么的這里不寫了。

2.????下載官方DEMO,真機測試,目的是你看看里面需要哪些參數(shù),實現(xiàn)哪些方法等等。

3.

4.????問題來了,測試不了,解決:因為你沒有更改商戶相關參數(shù)

(劃線位置添加AppID)

5.

6.????把相關商戶參數(shù)AppID,AppSECRET,MCH_ID(商戶號),填好就行

7.????運行以下微信官方demo真機 調試截圖

8.???? 《我的手機5S,最近準備*****買6Plus,親們能支援下我嘛,哈哈!》

9.???? 《有了加密的東西,模擬后臺演示,我是不是很好》

10.???? 啟動圖,說明調其支付,強行一波帶走

11.???? 《啟動圖都給你截出來了,一開始我是拒絕的》

12.???? (能支付了,我擦類(河南話!),不錯哦!)

13.???? 《1分錢,小意思啦!》

14.???? 《我想靜靜的等待》

15.???? 《1分錢就這樣沒了》

16.????點擊返回就可以回去啦!官方DEMO ,演示結束,親!能來個逼格的App嘛??

二:逼格APP,(掌拍藝術!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)跳轉處理

- (BOOL)application:(UIApplication *)application

openURL:(NSURL *)url

sourceApplication:(NSString *)sourceApplication

annotation:(id)annotation

{

NSLog(@"跳轉到URL schema中配置的地址-->%@",url);//跳轉到URL schema中配置的地址

if ([UMSocialSnsService handleOpenURL:url]) {

return??[UMSocialSnsService handleOpenURL:url];

}else{

return [WXApi handleOpenURL:url delegate:self];

}

}

(3)回調方法

-(void) onResp:(BaseResp*)resp

{

NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];

NSString *strTitle;

if([resp isKindOfClass:[SendMessageToWXResp class]])

{

strTitle = [NSString stringWithFormat:@"發(fā)送媒體消息結果"];

}

if([resp isKindOfClass:[PayResp class]]){

//支付返回結果,實際支付結果需要去微信服務器端查詢

strTitle = [NSString stringWithFormat:@"支付結果"];

switch (resp.errCode) {

case WXSuccess:{

strMsg = @"支付結果:成功!";

NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);

NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"];

[[NSNotificationCenter defaultCenter] postNotification:notification];

break;

}

default:{

strMsg = [NSString stringWithFormat:@"支付結果:失??!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];

}

說明:這里掌拍藝術App的調試和真機,建議把通知的東西注銷,打開alert,方便測試,便于檢查回調錯誤信息,如果你夠牛,可以無視,代碼難看,見諒,但是人很帥,哈哈!

(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)絡數(shù)據(jù)就那么幾種)主要用于請求后臺服務器已經(jīng)做好的數(shù)據(jù),請求下來的參數(shù)給微信,用于支付!(2)獲取每臺設備的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)絡有誤" 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{

//調起微信支付

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];

}

轉載 -- http://www.cocoachina.com/bbs/read.php?tid=303132

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容