iOS开发支付那些事(一)微信支付

很多网友面试的时候 都会被问到你的APP使用什么支付,说下支付的流程,什么,不知道!!!!!!那就回家等通知吧 如何 就没有然后了
接入支付宝开流程 点击这里 iOS支付那些事(二)支付宝
-一、 微信支付业务流程

商户APP调用微信提供的SDK调用微信支付模块,商户APP会跳转到微信中完成支付,支付完后跳回到商户APP内,最后展示支付结果。下面是一个完整的支付发起到结束的流程,

  • 步骤1
    用户进入商户APP,选择商品下单、确认购买,进入支付环节。商户服务后台生成支付订单,签名后将数据传输到APP端。
  • 步骤2
    用户点击后发起支付操作,进入到微信界面,调起微信支付,出现确认支付界面。
  • 步骤3
    用户确认收款方和金额,点击立即支付后出现输入密码界面,可选择零钱或银行卡支付。
  • 步骤4
    输入正确密码后,支付完成,用户端微信出现支付详情页面。见图。
  • 步骤5
    回跳到商户APP中,商户APP根据支付结果个性化展示订单处理结果。

  • 二、微信支付开发流程

    1、项目设置APPID
    商户在微信开放平台申请开发APP应用后,微信开放平台会生成APP的唯一标识APPID。在Xcode中打开项目,设置项目属性中的URL Schemes为您的APPID。如图标红位置所示。
    这里写图片描述
    2、注册APPID
    商户APP工程中引入微信lib库和头文件,调用API前,需要先向微信注册您的APPID,代码如下:
    [WXApi registerApp:@”wxd930ea5d5a258f4f” withDescription:@”demo 2.0”];
    3、调起支付
    商户服务器生成支付订单,先调用【统一下单API】生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付。以下是调起微信支付的关键代码:

PayReq *request = [[[PayReq alloc] init] autorelease];
request.partnerId = @"10000100";
request.prepayId= @"1101000000140415649af9fc314aa427";
request.package = @"Sign=WXPay";
request.nonceStr= @"a462b76e7436e98e0ed6e13c64b4fd1c";
request.timeStamp= @"1397527777";
request.sign= @"582282D72DD2B03AD892830965F428CB16E7A256";
[WXApi sendReq:request];

注意:该sign生成字段名列表见调起支付API
4、支付结果回调
照微信SDK Sample,在类实现onResp函数,支付完成后,微信APP会返回到商户APP并回调onResp函数,开发者需要在该函数中接收通知,判断返回错误码,如果支付成功则去后台查询支付结果再展示用户实际支付结果。注意 一定不能以客户端返回作为用户支付的结果,应以服务器端的接收的支付通知或查询API返回的结果为准。代码示例如下:

            -(void)onResp:(BaseResp*)resp{
              if ([respisKindOfClass:[PayRespclass]]){
                  PayResp*response=(PayResp*)resp;
                  switch(response.errCode){
                      caseWXSuccess:
                                //服务器端查询支付通知或查询API返回的结果再提示成功
                                NSlog(@"支付成功");
                        break;
                        default:
                        NSlog(@"支付失败,retcode=%d",resp.errCode);
                        break;
                  }
                    }
                } 

回调中errCode值列表:

0 成功 展示成功页面

-1 错误 可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。

-2 用户取消 无需处理。发生场景:用户不支付了,点击取消,返回APP。

  • 三、现在我们来创建第一个微信支付
    1. 导入微信支付库

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

  • 2.在AppDelegate中导入


  • (1)AppDelegate中导入
#import "WXApi.h"
#import "WXApiObject.h"

(2)注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:WXAppId withDescription:@"yishuPayDes"];

}

(3)跳转处理

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

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

//监听通知
- (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];
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值