前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS微信支付简单的使用

iOS微信支付简单的使用

作者头像
LeeCen
发布2018-10-11 17:00:34
9350
发布2018-10-11 17:00:34
举报
文章被收录于专栏:LeeCenLeeCen
APP微信商户申请APPID步骤地址

微信支付 SDK与 Demo地址下载

微信SDK与 Demo

  • 把微信支付 SDK 拖到工程上

SDK

  • 添加微信支付依赖库

添加依赖库

  • 微信支付 SDK 文件的read_me.txt 有版本更新说明与注意问题,能避免不必要Xcode的报错

版本更新说明与注意问题

  • ** 苹果在iOS9 系统把 Http 协议升级为 Https协议,Https比 Http更为安全性,对 Http协议访问做限制,所以需要在 Info.plust 文件添加 URL Schemes 白名单 **
  • ** 添加成功后,会在Info.plist显示这两个 Key (LSApplicationQueriesSchemes 与 App Transport Security Settings)**

Info.plist显示这两个 Key

  • 设置微信 APPID 为 URL Schemes

设置微信 APPID 为 URL Schemes

  • 在AppDelegate.m 导入头文件 #import "WXApi.h"
  • 在AppDelegate.m 填写你的微信APPID
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    

    [WXApi registerApp:@"填写你的微信APPID" withDescription:@"weixinDemo"];
    
    
    return YES;
}
  • 这个方法会收到来自微信回应的处理结果
/*! @brief 发送一个sendReq后,收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * @param resp具体的回应内容,是自动释放的
 */
-(void)onResp:(BaseResp *)resp
{
    if ([resp isKindOfClass:[PayResp class]])
    {
        //返回支付结果,实际支付结果需要取微信服务端查询
        NSString *strMsg = @"支付结果";
        switch (resp.errCode) {
            case WXSuccess:
                strMsg = @"支付成功";
                NSLog(@"支付成功-PaySuccess,resp.errCode = %d",resp.errCode);
                break;
                
            default:
                strMsg = @"支付失败";
                NSLog(@"支付失败-PaySuccess,resp.errCode = %d,resp.errStr = %@",resp. errCode,resp.errStr);
                break;
        }
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%d",resp.errCode ]message:resp.errStr preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:sure];
        [self.window.rootViewController presentViewController:alertController animated:YES completion:^{
            
        }];
    }
}
  • 让 AppDelegate.m 遵守 WXApiDelegate 代理协议
#pragma mark - 跳转到微信应用(第三方)
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    // 处理微信通过URL启动App时传递的数据
    return [WXApi handleOpenURL:url delegate:self];
}
  • **在点击响应事件的类导入微信支付的头文件 "WXApi.h",最后在你点击事件的方法中调用下面这个方法 **
+ (NSString *)jumpToBizPay {

    if (![WXApi isWXAppInstalled]) {
        NSLog(@"该设备没有安装微信");
        return @"该设备没有安装微信";
    }
    
    if (![WXApi isWXAppSupportApi]) {
        NSLog(@"该设备不支持微信");
        return @"该设备不支持微信";
    }
    
    //============================================================
    // V3&V4支付流程实现
    // 注意:参数配置请查看服务器端Demo
    // 更新时间:2015年11月20日
    //============================================================
    NSString *urlString   = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
    //解析服务端返回json数据
    NSError *error;
    //加载一个NSURL对象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //将请求的url数据放到NSData对象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if ( response != nil) {
        NSMutableDictionary *dict = NULL;
        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
        
        NSLog(@"url:%@",urlString);
        if(dict != nil){
            NSMutableString *retcode = [dict objectForKey:@"retcode"];
            if (retcode.intValue == 0){
                NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
                
                //调起微信支付
                PayReq* req             = [[PayReq alloc] init];
                req.partnerId           = [dict objectForKey:@"partnerid"];
                req.prepayId            = [dict objectForKey:@"prepayid"];
                req.nonceStr            = [dict objectForKey:@"noncestr"];
                req.timeStamp           = stamp.intValue;
                req.package             = [dict objectForKey:@"package"];
                req.sign                = [dict objectForKey:@"sign"];
                [WXApi sendReq:req];
                //日志输出
                NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
                return @"";
            }else{
                return [dict objectForKey:@"retmsg"];
            }
        }else{
            return @"服务器返回错误,未获取到json对象";
        }
    }else{
        return @"服务器返回错误";
    }
}

源码Demo地址

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.03.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • APP微信商户申请APPID步骤地址
  • 微信支付 SDK与 Demo地址下载
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档