微信支付,支付宝支付 iOS
来源:互联网 发布:java微信公众开发项目 编辑:程序博客网 时间:2024/06/02 16:33
整体感觉微信相对于支付宝稍微复杂一些,但是主要的都是后台那边做,我们只需请求相关的参数,调起支付就可以了。只要后台那边做好了,基本没我们啥事了~
首先做如下设置
然后在appdelegate.m 中
首先在didFinishLaunchingWithOptions
方法中注册微信(支付宝不需要注册):
[WXApi registerApp:@"wx13d6dbc9fb091222"];
然后其他方法中:
#pragma 支付宝支付- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { if ([url.host isEqualToString:@"safepay"]) { //跳转支付宝钱包进行支付,处理支付结果 [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { NSLog(@"result = %@",resultDic); }]; return YES; }else{ //微信 return [WXApi handleOpenURL:url delegate:self]; }}// NOTE: 9.0以后使用新API接口- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{ // safepay 是支付宝, pay 是微信 if ([url.host isEqualToString:@"safepay"]) { //跳转支付宝钱包进行支付,处理支付结果 [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { NSLog(@"result = %@",resultDic); }]; return YES; }else{ //跳回到APP return [WXApi handleOpenURL:url delegate:self]; }}//以下微信独有#pragma 微信- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [WXApi handleOpenURL:url delegate:self];}#pragma WXApiDelegate-(void) onReq:(BaseReq*)req{ NSLog(@"-========请求");}-(void) onResp:(BaseResp*)resp{ //支付结果回调 if ([resp isKindOfClass:[PayResp class]]){ PayResp*response=(PayResp*)resp; switch(response.errCode){ case WXSuccess: //服务器端查询支付通知或查询API返回的结果再提示成功 NSLog(@"支付成功"); [self toHomeVC]; break; default: NSLog(@"支付失败,retcode=%d",resp.errCode); break; } }}//跳转到首页(自己定义的支付后跳转到指定页面)- (void)toHomeVC{ UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; TabBarController *tabVC = [story instantiateViewControllerWithIdentifier:@"tabVC"]; self.window.rootViewController = tabVC;}
在支付的页面,点击支付按钮后,从后台那里请求相关参数,然后调起支付,主要代码如下:
if (_payMethod == 1) { //支付宝 [LSHTTPTool wx_POST:kUrl(@"add_Zfbpro") params:params success:^(id responseObject) { if (responseObject[@"meg"] == nil) { NSString *orderString = responseObject[@"OrderString"]; // NSString *payno = responseObject[@"payno"]; [[AlipaySDK defaultService] payOrder:orderString fromScheme:@"aliPayIQC" callback:^(NSDictionary *resultDic) { NSLog(@"reslut = %@",resultDic); }]; }else{ [ShowTool showHud:responseObject[@"meg"] view:self.view]; } } failure:^(NSError *error) { NSLog(@""); }]; }else{ //微信支付 [LSHTTPTool wx_POST:kUrl(@"add_Wxpro") params:params success:^(id responseObject) { if (responseObject[@"meg"] == nil) { NSDictionary *orderInfo = responseObject[@"params"]; //创建支付对象 PayReq *req = [[PayReq alloc] init]; //由用户微信号和AppID组成的唯一标识,用于校验微信用户 // req.openID = orderInfo[@"appid"]; // 商户号,在注册的时候给的 req.partnerId = orderInfo[@"partnerid"]; // 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你 req.prepayId = orderInfo[@"prepayid"]; // 根据财付通文档填写的数据和签名 //这个比较特殊,是固定的,只能是即req.package = Sign=WXPay req.package = orderInfo[@"package"]; // 随机编码,为了防止重复的,在后台生成 req.nonceStr = orderInfo[@"noncestr"]; // 这个是时间戳,也是在后台生成的,为了验证支付的 NSString * stamp = orderInfo[@"timestamp"]; req.timeStamp = stamp.intValue; // 这个签名也是后台做的 req.sign = orderInfo[@"sign"]; //发送请求到微信,等待微信返回onResp [WXApi sendReq:req]; // BOOL isok = [WXApi sendReq:req]; // if (isok) { // NSLog(@"调用微信支付成功"); // }else{ // NSLog(@"调用微信支付失败"); // } }else{ [ShowTool showHud:responseObject[@"meg"] view:self.view]; } } failure:^(NSError *error) { NSLog(@""); }]; }
阅读全文
0 0
- 微信支付,支付宝支付 iOS
- iOS支付[支付宝、银联、微信]
- iOS支付[支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝 银联 微信]
- iOS支付宝和微信支付
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS 支付 [支付宝、银联、微信]
- iOS-支付宝&微信支付
- leetcode 655. Print Binary Tree 深度优先遍历DFS
- IntelliJ Idea SpringBoot jpa mysql数据库增删改查实例
- maven详细介绍
- 极光推送集成与封装
- 148. Sort List--单链表自底向上归并排序
- 微信支付,支付宝支付 iOS
- gulp自动添加版本号过程中的一些要点记录
- 深入理解Android之AOP
- Linux下jdk的安装
- jetty配置的两种方式
- activiti 工作流 动态 设置 指定 节点任务人、责任人、组 的实现方式
- Netty 4.0在Android 上的使用,长连接断线自动重连
- 大数据程序员培训 哪些才是你重点要学的?
- java方法的可变参数用法