微信支付的开发

前言:之前听说过微信支付有很多坑,其实没有想象的那么坑,整体感觉很容易上手,按照它的流程来不会有错!PS:官方的流程看的TMD烦,好啦,废话有点多,进入开发。(ps:每个微信的版本一直都在更新,这是2015/6/1给你们做的标记

1.    导入微信支付库

微信开放平台新增了微信模块用户统计功能,便于开发者统计微信功能模块的用户使用和活跃情况。开发者需要在工程中链接上:SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib。

最重要的时这个库:libc++.dylib《ps:官方的文档没说,艹!》

2.在AppDelegate中导入:

(1)#import "WXApi.h"

#import "WXApiObject.h"

遵守WXApiDelegate

在这个方法里注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[WXApi registerApp:WXAppId withDescription:@"yishuPayDes"];


}

(2)跳转处理

- (BOOL)application:(UIApplication *)application

            openURL:(NSURL *)url

  sourceApplication:(NSString *)sourceApplication

         annotation:(id)annotation

{

    NSLog(@"跳转到URL schema中配置的地址-->%@",url);//跳转到URL schema中配置的地址 

    if ([UMSocialSnsService handleOpenURL:url]) {

        return  [UMSocialSnsService handleOpenURL:url];

    }else{

        return [WXApi handleOpenURL:url delegate:self];

    }

}


(3)回调方法

-(void) onResp:(BaseResp*)resp
{

    NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];

    NSString *strTitle;

    if([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
    }

    if([resp isKindOfClass:[PayResp class]]){

        //支付返回结果,实际支付结果需要去微信服务器端查询

        strTitle = [NSString stringWithFormat:@"支付结果"];

        

        switch (resp.errCode) {

            case WXSuccess:{

                strMsg = @"支付结果:成功!";

                NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);

                NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"];

                [[NSNotificationCenter defaultCenter] postNotification:notification];

                break;

            }

            default:{

                strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];

                NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);

                NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"fail"];

                [[NSNotificationCenter defaultCenter] postNotification:notification];

                break;

            }

        }

    }

//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

//    [alert show];

}

说明:这里掌拍艺术App的调试和真机,建议把通知的东西注销,打开alert,方便测试,便于检查回调错误信息,如果你够牛,可以无视,代码难看,见谅,但是人很帅,哈哈!

(4).接下来在需要支付的界面做这些事:

//监听通知

- (void)viewWillAppear:(BOOL)animated{

    [self requestDownloadData];

    if([WXApi isWXAppInstalled]) // 判断 用户是否安装微信

    {

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:ORDER_PAY_NOTIFICATION object:nil];//监听一个通知

    }

    [super viewWillAppear:animated];

}

/*ORDER_PAY_NOTIFICATION*/这个写个宏,全局里写,怎么写?建.h文件!

//移除通知

- (void)viewWillDisappear:(BOOL)animated{

    [[NSNotificationCenter defaultCenter]removeObserver:self]; 

}


开始支付-----→终于等到你,还好没放弃!

1.    预备工作,(1)我这里封装了下载类AF(自己感觉比较方便,亲们自己写下载就好了,因为我们公司的网络数据就那么几种)主要用于请求后台服务器已经做好的数据,请求下来的参数给微信,用于支付!(2)获取每台设备的IP地址,(3)HUD是啥,大家都用过,不说了(ps:HUD特效,自己定义看看那种效果好!)(4)后台做了什么:http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php这个地址给后台参考下,需要的参数都在上面移动端不需要写,如果你要写,我不拦你…哈哈,当练手吧!

2.    代码
#pragma mark - 微信支付
- (void)WeiXinPay{

    if([WXApi isWXAppInstalled]) // 判断 用户是否安装微信

    {

       HUD.delegate = self;

        HUD.labelText = @"正在为您支付...";

        [HUD show:YES];

        

        NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];

        NSString *ipAdress = [MyHttpDownload GetIPAddress:YES];

        NSLog(@"ipAdress%@",ipAdress);

        NSLog(@"self.order_orderinfoid%@",self.order_orderinfoid);

        NSLog(@"提交地址%@",[NSString stringWithFormat:TESTWXPayUrl,userID,self.order_orderinfoid,_WXPayStyleStr,ipAdress]);

        NSDictionary *dict = @{@"uid":userID,@"orderinfo_id":self.order_orderinfoid,@"type":_WXPayStyleStr,@"ip":ipAdress};

        [MyHttpDownload GetDownload:WXPayUrl param:dict finish:^(NSData *data, NSDictionary *obj, NSError *error) {

            if ([obj[@"data"] isKindOfClass:[NSDictionary class]]) {

                NSDictionary *dataDict = obj[@"data"];

                NSLog(@"respose信息--》%@",dataDict);

                if (obj != nil) {

                    [self WXPayRequest:dataDict[@"appid"] nonceStr:dataDict[@"noncestr"] package:dataDict[@"package"] partnerId:dataDict[@"partnerid"] prepayId:dataDict[@"prepayid"] timeStamp:dataDict[@"timestamp"] sign:dataDict[@"sign"]];

                }else{

                    [HUD hide:YES];

                    FlyAlertView *alert = [[FlyAlertView alloc] initWithTitle:@"提示" contentText:@"网络有误" leftButtonTitle:nil rightButtonTitle:@"确定"];

                    [alert show];

                }

            }else{

                [HUD hide:YES];
                NSString *mess = [NSString stringWithFormat:@"%@,退出重试!",obj[@"data"]];
                [self alert:@"提示" msg:mess];
            }

        }];

    }else{

        [HUD hide:YES];

        [self alert:@"提示" msg:@"您未安装微信!"];

    }

    

}

#pragma mark - 发起支付请求

- (void)WXPayRequest:(NSString *)appId nonceStr:(NSString *)nonceStr package:(NSString *)package partnerId:(NSString *)partnerId prepayId:(NSString *)prepayId timeStamp:(NSString *)timeStamp sign:(NSString *)sign{

       //调起微信支付

    PayReq* wxreq             = [[PayReq alloc] init];

    wxreq.openID              = WXAppId;

    wxreq.partnerId           = partnerId;

    wxreq.prepayId            = prepayId;

    wxreq.nonceStr            = nonceStr;

    wxreq.timeStamp           = [timeStamp intValue];

    wxreq.package             = package;

    wxreq.sign                = sign;

    [WXApi sendReq:wxreq];

}


#pragma mark - 通知信息

- (void)getOrderPayResult:(NSNotification *)notification{

    if ([notification.object isEqualToString:@"success"])

    {

        [HUD hide:YES];

        [self alert:@"恭喜" msg:@"您已成功支付啦!"];

        payStatusStr           = @"YES";

        _successPayView.hidden = NO;

        _toPayView.hidden      = YES;

        [self creatPaySuccess];

        

    }

    else

    {

        [HUD hide:YES];

        [self alert:@"提示" msg:@"支付失败"];

        

    }

}


//客户端提示信息

- (void)alert:(NSString *)title msg:(NSString *)msg

{

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alter show];

}




最后:测试,我后悔写这么多了,并没有什么用,我擦!

直接截图:

《钱太多,不付了,0.0!-.-》