iOS微信支付

1、集成SDK
1.1、(手动集成)下载并解压SDK​(下载那个带支付的SDK):下载链接
1.2、(通过CocoaPods集成)
pod 'WechatOpenSDK'

2、添加依赖库(CocoaPods集成的不用添加)
SystemConfiguration.framework
libz.tbd
libsqlite3.0.tbd
libc++.tbd
CoreTelephony.framework
CFNetwork.framework
Security.framework
UIKit.framework
Foundation.framework

3、新增⼀一条URL Scheme:选中⼯工程Target -> Info -> URLTypes;
identifier随便写,URL scheme写成微信开放平台申请的appid

微信URL scheme

4、添加⽩白名单:LSApplicationQueriesSchemes新增⽩名单****

微信白名单

5、AppDelegate中向微信注册Appid,并添加处理回调的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [WXApi registerApp:WxAppid enableMTA:YES];
     return YES;
}

//iOS9.0以前使用

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
return YES;
}

// 9.0以后使用新API接口

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{
  [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    return YES;
}

代理在哪个类实现的回调方法 就写哪个类就行了。

6、接下来调起支付
我们这里是把后台需要的参数传给后台 然后把后台返回的结果dict传到下面的方法里就可以调起支付了。

+ (NSString *)jumpToBizPay:(NSDictionary *)dict{
    if (![BMWFactory isBlankDictionary:dict]) {
        //调起微信支付
        PayReq* req             = [[PayReq alloc] init];
        req.partnerId           = [dict objectForKey:@"partnerid"];
        req.prepayId            = [dict objectForKey:@"prepayid"];
        req.nonceStr            = [dict objectForKey:@"noncestr"];
        req.timeStamp           = [[dict objectForKey:@"timestamp"] intValue];
        req.package             = [dict objectForKey:@"package_value"];
        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{
        [SVProgressHUD showErrorWithStatus:@"支付失败"];
    }
    return nil;
}

支付结果返回并根据需求处理跳转(微信支付、登录、分享都走这个方法)

#pragma mark - WXApiDelegate==========================WXApiDelegate
- (void)onResp:(BaseResp *)resp {
    /*
     enum  WXErrCode {
     WXSuccess           = 0,    成功
     WXErrCodeCommon     = -1,  普通错误类型
     WXErrCodeUserCancel = -2,    用户点击取消并返回
     WXErrCodeSentFail   = -3,   发送失败
     WXErrCodeAuthDeny   = -4,    授权失败
     WXErrCodeUnsupport  = -5,   微信不支持
     };
     */
    //微信支付的类
    if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果,实际支付结果需要去微信服务器端查询
        NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"];
        if (resp.errCode == 0) {
            strMsg = @"支付结果:成功!";
            NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
            PaySucceedViewController *vc = [[PaySucceedViewController alloc] init];
            vc.backStr = @"1";
            UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
            AppDelegate *appDelegate =
            (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate.window.rootViewController presentViewController:navi animated:NO completion:nil];
        }else{
            strMsg = [NSString stringWithFormat:@"支付结果:失败!"];
            NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
            PayFailedViewController *vc = [[PayFailedViewController alloc] init];
            vc.backStr = @"1";
            UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
            AppDelegate *appDelegate =
            (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate.window.rootViewController presentViewController:navi animated:NO completion:nil];
        }
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
    }
    
    //微信登录的类
    if([resp isKindOfClass:[SendAuthResp class]]){
        if (resp.errCode == 0) {  //成功。
            //这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。
            if (_delegate && [_delegate respondsToSelector:@selector(loginSuccessByCode:)]) {
                SendAuthResp *resp2 = (SendAuthResp *)resp;
                [_delegate loginSuccessByCode:resp2.code];
            }
        }else{ //失败
            NSLog(@"error %@",resp.errStr);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }

    //微信分享的类
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        //微信分享 微信回应给第三方应用程序的类
        SendMessageToWXResp *response = (SendMessageToWXResp *)resp;
        NSLog(@"error code %d  error msg %@  lang %@   country %@",response.errCode,response.errStr,response.lang,response.country);
        if (resp.errCode == 0) {//成功。
            //这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。
                if(_delegate && [_delegate respondsToSelector:@selector(shareSuccessByCode:)]){
                    [_delegate shareSuccessByCode:response.errCode];
                }
        }else{ //失败
            NSLog(@"error %@",resp.errStr);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }
}

推荐阅读更多精彩内容

  • 前言:本篇文章目的在于梳理知识,巩固思想,学习总结。有什么好的建议,都可以留言。互相促进!总观,微信支付,也没心思...
    _正阳_阅读 8,647评论 8 69
  • 1. 准备 微信平台分为微信公众平台和微信开放平台,公众平台是运营微信公众号的管理系统,开放平台主要针对app、网...
    帝步凡阅读 12,534评论 0 491
  • 微信支付的流程 注册微信开放平台,创建应用获取appid,appSecret,申请支付功能,申请成功之后会返回一些...
    管家頗阅读 885评论 2 5
  • 最近公司需要开发支付功能,经过这两天的工作成功集成了app微信支付、支付宝支付。本文主要讲述微信支付的流程和在集成...
    予独爱秋天的梅花阅读 1,072评论 0 5
  • 一:介绍 项目中要用到支付功能,需要支付宝支付、支付宝网页支付、微信支付、银联支付、Apple_pay,所以打算总...
    展菲阅读 9,681评论 5 25