IOS 集成微信支付功能的實(shí)現(xiàn)方法
創(chuàng)新互聯(lián)公司是專(zhuān)業(yè)的崇陽(yáng)網(wǎng)站建設(shè)公司,崇陽(yáng)接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專(zhuān)業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行崇陽(yáng)網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專(zhuān)業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專(zhuān)業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
第一步:集成微信的SDK
https://pay.weixin.qq.com/wiki/doc/api/index.html
點(diǎn)擊進(jìn)入
下載對(duì)應(yīng)SDK或示例,最后可以看看示例程序
第二步:在Xcode中填寫(xiě)微信開(kāi)放平臺(tái)申請(qǐng)的Appid
Xcode>info>URL Types 中新建加入Appid
第三步:在Appdelegate.m 中注冊(cè)微信支付 和回調(diào)
#import "WXApi.h" 添加 代理 WXApiDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //self.window.backgroundColor = [UIColor clearColor]; // 微信支付注冊(cè) [WXApiregisterApp:PAY_WEIXIN_ID]; returnYES; } // ios 9.0以上系統(tǒng)版本回調(diào) - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options { // 微信 if ([url.schemeisEqualToString:PAY_WEIXIN_ID]) { [WXApihandleOpenURL:url delegate:(id )self]; } // 支付寶 if ([url.schemeisEqualToString:@"SearchPigeonWorld"]) { //跳轉(zhuǎn)支付寶錢(qián)包進(jìn)行支付,處理支付結(jié)果 [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) { if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) { [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO]; } }]; } returnYES; } //支付成功時(shí)調(diào)用,回到第三方應(yīng)用中 ios 9.0以下系統(tǒng)版本回調(diào) - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // 微信 if ([url.schemeisEqualToString:PAY_WEIXIN_ID]) { [WXApihandleOpenURL:url delegate:(id )self]; } // 支付寶 if ([url.hostisEqualToString:PAY_ALIPAY_appID]) { //跳轉(zhuǎn)支付寶錢(qián)包進(jìn)行支付,處理支付結(jié)果 [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) { if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) { [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO]; } }]; } returnYES; } /** 微信自己的結(jié)果返回方法 @param resp 返回結(jié)果狀態(tài) */ - (void)onResp:(BaseResp*)resp { if([respisKindOfClass:[PayRespclass]]){ BOOL isPaySuccess =NO; switch (resp.errCode) { caseWXSuccess: isPaySuccess = YES; break; caseWXErrCodeUserCancel: isPaySuccess = NO; break; caseWXErrCodeSentFail: isPaySuccess = NO; break; caseWXErrCodeAuthDeny: isPaySuccess = NO; break; default: isPaySuccess = NO; break; } if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) { [self.appMyDelegatepayCenterWeixinOnResultWith:isPaySuccess]; } } }
第四步:在使用微信的地方調(diào)用支付方法
#pragma mark 2.2.14(10)使用微信進(jìn)行付款,獲取微信加密信息 - (void)getWebResponsePayWeixinInfo { NSDictionary *parameters =@{@"key" :appDelegate.userKeyString, @"foundRecordId" : [self.payInfoDictobjectForKey:@"foundRecordId"]}; [MBProgressHUDshowMessage:@""]; [WebDataResponseInterfaceSessionManagerPostWebDataWithApi:WEBInterFace_Good_CreateWeiXinPayOrderandParameters:parameters andSuccess:^(id successObject) { MYLOG(@"%@", successObject); [MBProgressHUDhideHUD]; if ([successObject[@"status"]isEqualToString:@"success"]) { successObject = [successObject objectForKey:@"value"]; // 微信支付 //需要?jiǎng)?chuàng)建這個(gè)支付對(duì)象 PayReq *req = [[PayReqalloc] init]; //由用戶(hù)微信號(hào)和AppID組成的唯一標(biāo)識(shí),用于校驗(yàn)微信用戶(hù) req.openID = successObject[@"appid"]; // 商家id,在注冊(cè)的時(shí)候給的 req.partnerId = [successObjectobjectForKey:@"partnerid"]; // 預(yù)支付訂單這個(gè)是后臺(tái)跟微信服務(wù)器交互后,微信服務(wù)器傳給你們服務(wù)器的,你們服務(wù)器再傳給你 req.prepayId = [successObjectobjectForKey:@"prepayid"]; // 根據(jù)財(cái)付通文檔填寫(xiě)的數(shù)據(jù)和簽名 //這個(gè)比較特殊,是固定的,只能是即req.package = Sign=WXPay req.package = [successObjectobjectForKey:@"package"]; // 隨機(jī)編碼,為了防止重復(fù)的,在后臺(tái)生成 req.nonceStr = [successObjectobjectForKey:@"noncestr"]; // 這個(gè)是時(shí)間戳,也是在后臺(tái)生成的,為了驗(yàn)證支付的 req.timeStamp = [[successObjectobjectForKey:@"timestamp"]doubleValue]; // 這個(gè)簽名也是后臺(tái)做的 req.sign = [successObjectobjectForKey:@"sign"]; //發(fā)送請(qǐng)求到微信,等待微信返回onResp [WXApisendReq:req]; } else { [MBProgressHUDshow:[successObject objectForKey:@"value"]icon:nilview:self.view]; } } andFailure:^(NSError *error) { [MBProgressHUDhideHUD]; MYLOG(@"error: %@", error); }]; }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀, 希望通過(guò)本文能幫助到大家,謝謝大家對(duì)本站的支持!