微信支付流程及填坑(时间久了,想起来继续更新)

2016-07-19  本文已影响789人  小弱鸡

应用场景:

去年六月应公司业务发展需要,我们开发要求接入微信支付和支付宝支付。一直没有总结出来留下只言片语告诫后来人,现如今有时间来整理下。
总的来说国内的微信支付还是比较坑的,demo一般开发过程中会遇到很多问题问题。打开微信支付开放平台给了一张图,说明其流程,但是我想说的是这TM有个卵用。老子只要的是一个支付而已,就是生成一订单提交给服务器,并调起支付好么?(这都是后话)好了不在吐槽说正事


Paste_Image.png
第一步

打开该连接https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F 注册商户,忘记了是每年三百块还是总共三百块,注册之后会有几个工作日的审核,请耐心等待,待审核通过后注册应用后我们会拿到应用的ID用于参数填写。

第二步

pods入微信支付sdk,并填写相关参数

 [WXApi registerApp:@"你的微信APPID" withDescription:@"Wechat"];

applelegate中添加用这个方法,这个另外一个是坑

-(void)onResp:(BaseResp*)resp{//微信ios9.0以下的方法
    if ([resp isKindOfClass:[PayResp class]]){
        PayResp *response = (PayResp *)resp;
        NSString *codeString=[NSString stringWithFormat:@"%d",response.errCode];
        switch (response.errCode) {
            case WXSuccess: {
                UIWindow *window = [[UIApplication sharedApplication] keyWindow];
                [MBProgressHUD showMessage:@"正在处理支付结果..." toView:window];
                [Noti_Center postNotificationName:WX_PAY object:codeString];
                break;
            }
            case WXErrCodeCommon:
                [Noti_Center postNotificationName:WX_PAY object:codeString];
                break;
            case WXErrCodeUserCancel:
                [Noti_Center postNotificationName:WX_PAY object:codeString];
                break;
                
            default:
                break;
        }
    } // 向微信请求授权后,得到响应结果
    if ([resp isKindOfClass:[SendAuthResp class]]) {
        SendAuthResp *temp = (SendAuthResp *)resp;
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *accessUrlStr = [NSString stringWithFormat:@"%@/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", WX_BASE_URL, kWeiXinAppID, kWeiXinSecret, temp.code];
        manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", nil];
        [manager GET:accessUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"请求access的response = %@", responseObject);
            NSDictionary *accessDict = [NSDictionary dictionaryWithDictionary:responseObject];
            NSString *accessToken = [accessDict objectForKey:kWeiXinAccessToken];
            NSString *openID = [accessDict objectForKey:kWeiXinOpenId];
            NSString *refreshToken = [accessDict objectForKey:kWeiXinRefreshToken];
            //本地持久化,以便access_token的使用、刷新或者持续
            //NSLog(@"bbb%@  %@",accessToken,openID);
            if (accessToken && ![accessToken isEqualToString:@""] && openID && ![openID isEqualToString:@""]) {
                [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:kWeiXinAccessToken];
                [[NSUserDefaults standardUserDefaults] setObject:openID forKey:kWeiXinOpenId];
                [[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:kWeiXinRefreshToken];
                [[NSUserDefaults standardUserDefaults] synchronize]; // 命令直接同步到文件里,来避免数据的丢失
            }
            [self wechatLoginByRequestForUserInfo];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"获取access_token时出错 = %@", error);
        }];
    }
}
第三步

调起app支付操作

if ([balanceData.paymentMold isEqualToString:WX_PAY]) {//微信支付
            //判断微信是否存在
            if ([WXApi isWXAppInstalled]) {
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WX_PAYStatus:) name:WX_PAY object:nil];
                HAOrderSubmitViewController *submitVc = (HAOrderSubmitViewController *)[self viewController];
                NSString *productName = @"违章代缴";
                HAProduct *product = [[HAProduct alloc] init];
                product.amount = [NSString stringWithFormat:@"%@f",balanceData.lastMoney];//金额
                product.tradeNO = orderMsg.orderNo;//订单号
                product.productName = productName;//商品名
                [submitVc sendWX_PayWithOrderProduct:product];
            }else{
                WX_APPAlert=[[HAAlertView alloc]initWithTitle:nil message:@"微信尚未安装,是否安装微信" delegate:self cancelButtonTitle:@"取消" sureButtonTitle:@"确认"];
                [WX_APPAlert show];
            }
        }

支付回调,有坑见文末

- (void)WX_PAYStatus:(NSNotification *)notication{
    [self removeNotificationWithName:WX_PAY];
    if ([notication.object isEqualToString:@"0"]) {
        //修改订单状态
        // NSLog(@"支付成功");
        //修改服务端订单状态
        [self changePaySatusWitherrCode:notication.object];
        [[NSNotificationCenter defaultCenter] postNotificationName:tableViewData object:nil];
    }else if ([notication.object isEqualToString:@"-2"]){
        //取消支付,关闭微信订单,生成新订单,避免影响下一次操作
        //NSLog(@"操作取消");
        [MBProgressHUD showMessage:@"取消支付"];
        HAOrderSubmitViewController *submitVc = (HAOrderSubmitViewController *)[self viewController];
        HAPayOnLineContriller *test =(HAPayOnLineContriller *)[self viewController];
        [submitVc.navigationController popToViewController:test animated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:tableViewData object:nil];

        [HAMKManger createOrderWithOrderDetail:orderMsg.orderDetail
                                               CouponsId:orderMsg.couponsId
                                             OrderAmount:orderMsg.orderAmount
                                             GoodsAmount:orderMsg.goodsAmount
         OrderType:@"1" Completion:^(NSDictionary *dict,NSInteger state) {
             //DLog(@"%@",dict);
             [self closeWX_OrderNetworkOrderCode:dict[@"result"]];
         }];
    }
}

遇见的坑:

上一篇 下一篇

猜你喜欢

热点阅读