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
 

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];
        }
    }
}

 

posted @ 2019-07-01 15:53  时光清浅、  阅读(257)  评论(0编辑  收藏  举报