iOS微信支付

作者: 芝麻绿豆 | 来源:发表于2015-12-21 15:36 被阅读1471次

    抽个时间总结一下微信支付;

    在AppDelegate中注册

    在-(BOOL)application:didFinishLaunchingWithOptions:launchOptions中注册:

    [WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:nil];
    

    配置应用间跳转的URL:


    点击支付

            NSString *res = [WXplay jumpToBizPay];
            if( ![@"" isEqual:res] ){
                UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"支付失败" message:res delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alter show];
                [alter release];
            }
    

    WXplay.m

    + (NSString *)jumpToBizPay {
        
        //============================================================
        // 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]autorelease];
                    req.partnerId           = [dict objectForKey:@"partnerid"]; // 商户号
                    req.prepayId            = [dict objectForKey:@"prepayid"];// 预支付交易会话ID
                    req.nonceStr            = [dict objectForKey:@"noncestr"];//随机字符串
                    req.timeStamp           = stamp.intValue; //时间戳
                    req.package             = [dict objectForKey:@"package"];//Sign=WXPay
                    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 @"服务器返回错误";
        }
    }
    

    WXApiManager 需要遵守 WXApiDelegate

    实现单例:

    +(instancetype)sharedManager {
        static dispatch_once_t onceToken;
        static WXApiManager *instance;
        dispatch_once(&onceToken, ^{
            instance = [[WXApiManager alloc] init];
        });
        return instance;
    }
    

    WXApiDelegate代理方法的实现

    • 实现onResp方法、完成支付后,微信app会返回到商家app页面,并且回调onResp函数,开发者需要在该方法中接收通知,判断返回码,如果支付成功则去后台查询支付结果再展示给用户实际支付结果
    -(void)onResp:(BaseResp *)resp {
      if([resp isKindOfClass:[PayResp class]]){
            //支付返回结果,实际支付结果需要去微信服务器端查询
            NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"];
            switch (resp.errCode) {
                case WXSuccess:
                    // 在此显示支付结果数据(从服务器获取)
                    strMsg = @"支付结果:成功!";
                    NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                    break;
                default:
                    strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                    NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                    break;
            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }
    

    相关文章

      网友评论

      本文标题:iOS微信支付

      本文链接:https://www.haomeiwen.com/subject/ciemhttx.html