分享

iOS微信支付开发 | iOS开发

 南山岳麓书院 2015-06-02
微信支付的开发!(文档自己做的,仅供参考)
前言:之前听说过微信支付有很多坑,其实没有想象的那么坑,整体感觉很容易上手,按照它的流程来不会有错!PS:官方的流程看的TMD烦,好啦,废话有点多,进入开发。(ps:每个微信的版本一直都在更新,这是2015/6/1给你们做的标记下QQ:1242384226,欢迎随便骚扰,女士优先!大神也优先!咦…)
一:怎么用官方DEMO
1.    申请得到AppID秘钥什么的这里不写了。
2.    下载官方DEMO,真机测试,目的是你看看里面需要哪些参数,实现哪些方法等等。
3.    
4.    问题来了,测试不了,解决:因为你没有更改商户相关参数
(划线位置添加AppID)

5.    


6.    把相关商户参数AppID,AppSECRET,MCH_ID(商户号),填好就行
7.    运行以下微信官方demo真机 调试截图
8.     《我的手机5S,最近准备*****买6Plus,亲们能支援下我嘛,哈哈!》
9.     《有了加密的东西,模拟后台演示,我是不是很好》
10.     启动图,说明调其支付,强行一波带走


11.     《启动图都给你截出来了,一开始我是拒绝的》
12.     (能支付了,我擦类(河南话!),不错哦!)
13.     《1分钱,小意思啦!》
14.     《我想静静的等待》
15.     《1分钱就这样没了》
16.    点击返回就可以回去啦!官方DEMO ,演示结束,亲!能来个逼格的App嘛??


二:逼格APP,(掌拍艺术!2个月做完的,原谅我的界面杀马特…)

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!-.-》


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多