iOS微信支付

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

  • 需要和微信申请APPID然后审核 (300块大洋)

  • 首先从官网上下载一个demo[网址](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=1417694084&token=1b41037aa554145bdb95b7430eb139850a46b1d7&lang=zh_CN);

  • 如果你用模拟器运行这个demo是看不到什么效果的,要用真机调试;

  • 根据demo中的示例,将支付代码提取出来;


    iOS微信支付_第1张图片

    iOS微信支付_第2张图片

    iOS微信支付_第3张图片


    iOS微信支付_第4张图片
iOS微信支付_第5张图片

在AppDelegate中注册

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

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

配置应用间跳转的URL:


iOS微信支付_第6张图片

点击支付

        NSString *res = [WXplay jumpToBizPay];
        if( ![@"" isEqual:res] ){
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"支付失败" message:res delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alter show];
            [alter release];
        }
iOS微信支付_第7张图片

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 @"服务器返回错误";
    }
}
iOS微信支付_第8张图片

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微信支付_第9张图片
iOS微信支付_第10张图片

你可能感兴趣的:(iOS微信支付)