1.微信開(kāi)發(fā)平臺(tái):http://kf.qq.com/faq/120911VrYVrA150906F3qqY3.html
2.SDK包:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
3.開(kāi)發(fā)文檔:https://pay.weixin.qq.com/wiki/doc/api/index.html
開(kāi)發(fā)步驟 :
第一步:
第二步:(記得看SDK里的readme和demo,因?yàn)橛邪酌麊魏鸵蕾噹?kù)要添加)
下載SDK開(kāi)發(fā)包:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
第三步:
1、項(xiàng)目設(shè)置APPID
商戶在微信開(kāi)放平臺(tái)申請(qǐng)開(kāi)發(fā)APP應(yīng)用后巷挥,微信開(kāi)放平臺(tái)會(huì)生成APP的唯一標(biāo)識(shí)APPID垢村。在Xcode中打開(kāi)項(xiàng)目钓账,設(shè)置微信的URL Schemes為申請(qǐng)成功的APPID垒手。
2胡嘿、注冊(cè)APPID
(1)在工程中導(dǎo)入微信lib庫(kù)和頭文件疏魏,
(2)向微信注冊(cè)您的APPID停做,代碼如下:
[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];
3、調(diào)起支付
-(void)WeixinPay:(NSString*)price{
[UDFsetObject:priceforKey:@"amount"];
//1.創(chuàng)建支付簽名對(duì)象
payRequsestHandler*req = [[payRequsestHandleralloc]init] ;
[reqinit:APP_IDmch_id:MCH_ID];//設(shè)置appid和商戶ID
[reqsetKey:PARTNER_ID];//設(shè)置密鑰
//2.獲取到實(shí)際調(diào)起微信支付的參數(shù)后大莫,在app端調(diào)起支付(此方法里要修改某些自己商品參數(shù))
NSMutableDictionary*dict = [reqsendPay_demo];
//3.發(fā)送微信支付請(qǐng)求
if(dict ==nil){//錯(cuò)誤提示
NSString*debug = [reqgetDebugifo];
// [self alert:@"提示信息" msg:debug];
}else{
//[self alert:@"確認(rèn)" msg:@"下單成功蛉腌,點(diǎn)擊OK后調(diào)起支付!"];
NSMutableString*stamp? = [dictobjectForKey:@"timestamp"];
//調(diào)起微信支付
PayReq* req ??????????? = [[PayReqalloc]init];
req.openID= [dictobjectForKey:@"appid"];
req.partnerId= [dictobjectForKey:@"partnerid"];
req.prepayId= [dictobjectForKey:@"prepayid"];
req.nonceStr= [dictobjectForKey:@"noncestr"];
req.timeStamp= stamp.intValue;
req.package= [dictobjectForKey:@"package"];
req.sign= [dictobjectForKey:@"sign"];
[WXApisendReq:req];//發(fā)送微信的支付請(qǐng)求
}
}
//這個(gè)方法是WXApi類里的方法
- (NSMutableDictionary*)sendPay_demo{
NSUserDefaults*udf = [NSUserDefaultsstandardUserDefaults];
//訂單標(biāo)題只厘,展示給用戶
NSString*order_name??? = [udfobjectForKey:@"courseTitle"];
//預(yù)付單參數(shù)訂單設(shè)置
srand( (unsigned)time(0) );
NSString*noncestr? = [NSStringstringWithFormat:@"%d",rand()];
NSString*orderno ? = [UDFvalueForKey:@"Orderid"];
NSMutableDictionary*packageParams = [NSMutableDictionarydictionary];
//開(kāi)放平臺(tái)appid
[packageParamssetObject:appidforKey:@"appid"];
//商戶號(hào)
[packageParamssetObject:mchidforKey:@"mch_id"];
//支付設(shè)備號(hào)或門(mén)店號(hào)
[packageParamssetObject:@"APP-001"forKey:@"device_info"];
//固定:隨機(jī)串
[packageParamssetObject: noncestrforKey:@"nonce_str"];
//固定:支付類型烙丛,固定為APP
[packageParamssetObject:@"APP"forKey:@"trade_type"];
//變動(dòng):自己商品名稱
[packageParamssetObject: order_nameforKey:@"body"];
//變動(dòng):支付結(jié)果異步通知
[packageParamssetObject:NOTIFY_URLforKey:@"notify_url"];
//商戶訂單號(hào)
[packageParamssetObject: ordernoforKey:@"out_trade_no"];
//發(fā)起支付的機(jī)器ip
[packageParamssetObject:@"196.168.1.1"forKey:@"spbill_create_ip"];
//訂單金額,單位為分(變動(dòng):自己商品價(jià)格)
[packageParamssetObject:[NSStringstringWithFormat:@"%.f",[ [udfvalueForKey:@"amount"]floatValue]*100]forKey:@"total_fee"];
//獲取prepayId(預(yù)支付交易會(huì)話標(biāo)識(shí))
NSString*prePayid;
prePayid ? = [selfsendPrepay:packageParams];
if( prePayid !=nil) {
//獲取到prepayid后進(jìn)行第二次簽名
NSString*package, *time_stamp, *nonce_str;
//設(shè)置支付參數(shù)
time_tnow;
time(&now);
time_stamp? = [NSStringstringWithFormat:@"%ld", now];
nonce_str= [WXUtilmd5:time_stamp];
//重新按提交格式組包羔味,微信客戶端暫只支持package=Sign=WXPay格式河咽,須考慮升級(jí)后支持?jǐn)y帶package具體參數(shù)的情況
//package ????? = [NSString stringWithFormat:@"Sign=%@",package];
package ??????? =@"Sign=WXPay";
//第二次簽名參數(shù)列表
NSMutableDictionary*signParams = [NSMutableDictionarydictionary];
[signParamssetObject:appidforKey:@"appid"];
[signParamssetObject: nonce_strforKey:@"noncestr"];
[signParamssetObject: packageforKey:@"package"];
[signParamssetObject:mchidforKey:@"partnerid"];
[signParamssetObject: time_stampforKey:@"timestamp"];
[signParamssetObject: prePayidforKey:@"prepayid"];
//生成簽名
NSString*sign? = [selfcreateMd5Sign:signParams];
//添加簽名
[signParamssetObject: signforKey:@"sign"];
[debugInfoappendFormat:@"第二步簽名成功,sign=%@\n",sign];
//返回參數(shù)列表
returnsignParams;
}else{
[debugInfoappendFormat:@"獲取prepayid失敻吃忘蟹!\n"];
}
returnnil;
}
4、支付結(jié)果回調(diào) (在appdelegate.m里寫(xiě)上這個(gè)回調(diào)方法搁凸!)
照微信SDK Sample媚值,在類實(shí)現(xiàn)onResp函數(shù),支付完成后护糖,微信APP會(huì)返回到商戶APP并回調(diào)onResp函數(shù)褥芒,開(kāi)發(fā)者需要在該函數(shù)中接收通知,判斷返回錯(cuò)誤碼嫡良,如果支付成功則去后臺(tái)查詢支付結(jié)果再展示用戶實(shí)際支付結(jié)果锰扶。注意 一定不能以客戶端返回作為用戶支付的結(jié)果献酗,應(yīng)以服務(wù)器端的接收的支付通知或查詢API返回的結(jié)果為準(zhǔn)。代碼示例如下:
-(void)onResp:(BaseResp*)resp{
if ([respisKindOfClass:[PayRespclass]]){
PayResp*response=(PayResp*)rest;
switch(response.errCode){
caseWXSuccess:
//服務(wù)器端查詢支付通知或查詢API返回的結(jié)果再提示成功
NSlog(@"支付成功");
break;
default:
NSlog(@"支付失敗少辣,retcode=%d",resp.errCode);
break;
}
}
}
如果上面的文章對(duì)您在以后的開(kāi)發(fā)中能派上用場(chǎng)凌摄,請(qǐng)關(guān)注或點(diǎn)個(gè)喜歡吧羡蛾,我會(huì)不定期的給您分享或推薦一些好的文章哦^_^