IOS 集成微信支付功能的实现方法
第一步:集成微信的SDK
https://pay.weixin.qq.com/wiki/doc/api/index.html
点击进入
下载对应SDK或示例,最后可以看看示例程序
第二步:在Xcode中填写微信开放平台申请的Appid
Xcode>info>URL Types 中新建加入Appid
第三步:在Appdelegate.m 中注册微信支付 和回调
#import "WXApi.h" 添加 代理 WXApiDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //self.window.backgroundColor = [UIColor clearColor]; // 微信支付注册 [WXApiregisterApp:PAY_WEIXIN_ID]; returnYES; } // ios 9.0以上系统版本回调 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options { // 微信 if ([url.schemeisEqualToString:PAY_WEIXIN_ID]) { [WXApihandleOpenURL:url delegate:(id<WXApiDelegate>)self]; } // 支付宝 if ([url.schemeisEqualToString:@"SearchPigeonWorld"]) { //跳转支付宝钱包进行支付,处理支付结果 [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) { if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) { [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO]; } }]; } returnYES; } //支付成功时调用,回到第三方应用中 ios 9.0以下系统版本回调 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // 微信 if ([url.schemeisEqualToString:PAY_WEIXIN_ID]) { [WXApihandleOpenURL:url delegate:(id<WXApiDelegate>)self]; } // 支付宝 if ([url.hostisEqualToString:PAY_ALIPAY_appID]) { //跳转支付宝钱包进行支付,处理支付结果 [[AlipaySDKdefaultService] processOrderWithPaymentResult:urlstandbyCallback:^(NSDictionary *resultDic) { if ([self.appMyDelegaterespondsToSelector:@selector(payCenterWeixinOnResultWith:)]) { [self.appMyDelegatepayCenterWeixinOnResultWith:[resultDic[@"resultStatus"]intValue] ==9000 ? YES :NO]; } }]; } returnYES; } /** 微信自己的结果返回方法 @param resp 返回结果状态 */ - (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]; } } }
第四步:在使用微信的地方调用支付方法
#pragma mark 2.2.14(10)使用微信进行付款,获取微信加密信息 - (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"]; // 微信支付 //需要创建这个支付对象 PayReq *req = [[PayReqalloc] init]; //由用户微信号和AppID组成的唯一标识,用于校验微信用户 req.openID = successObject[@"appid"]; // 商家id,在注册的时候给的 req.partnerId = [successObjectobjectForKey:@"partnerid"]; // 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你 req.prepayId = [successObjectobjectForKey:@"prepayid"]; // 根据财付通文档填写的数据和签名 //这个比较特殊,是固定的,只能是即req.package = Sign=WXPay req.package = [successObjectobjectForKey:@"package"]; // 随机编码,为了防止重复的,在后台生成 req.nonceStr = [successObjectobjectForKey:@"noncestr"]; // 这个是时间戳,也是在后台生成的,为了验证支付的 req.timeStamp = [[successObjectobjectForKey:@"timestamp"]doubleValue]; // 这个签名也是后台做的 req.sign = [successObjectobjectForKey:@"sign"]; //发送请求到微信,等待微信返回onResp [WXApisendReq:req]; } else { [MBProgressHUDshow:[successObject objectForKey:@"value"]icon:nilview:self.view]; } } andFailure:^(NSError *error) { [MBProgressHUDhideHUD]; MYLOG(@"error: %@", error); }]; }
如有疑问请留言或者到本站社区交流讨论,感谢阅读, 希望通过本文能帮助到大家,谢谢大家对本站的支持!