为了账号安全,请及时绑定邮箱和手机立即绑定

微信支付-微信扫码支付

标签:
Java
  @RequestMapping("/wechatScanPay.shtml")
    @ResponseBody
    public Object wechatScanPay(HttpServletRequest req, HttpServletResponse resp) throws IOException, DocumentException, WriterException {
        try {
            String reqId = req.getParameter("id");
            Preconditions.checkArgument(StringUtils.isNotBlank(reqId), "支付请求号为空");

            OnlinePaymentRequest opr = onlinePaymentRequestManager.selectByPrimaryKey(new Long(reqId));
            if (opr == null)
                return new ResultModel(ResultCode.NOT_FIND_PAY_REQUEST).createResp();

            Integer operateStatus = opr.getOperateStatus();
            if (operateStatus.intValue() == PayDict.OPERATE_STATUS_1)
                return new ResultModel(ResultCode.DOUBLE_PAID).createResp();

            Map map = Maps.newHashMap();
            map.put("appid", WeChatConstant.APP_ID);
            map.put("mch_id", WeChatConstant.MCH_ID);
            map.put("nonce_str", UUIDUtil.getUUID());
            //map.put("sign_type", WeChatConstant.SIGN_TYPE);
            map.put("body", opr.getBody());//商品描述
            //todo
//            map.put("out_trade_no", String.valueOf(opr.getOnlinePaymentRequestId()));
            map.put("out_trade_no", String.valueOf(DateUtil.getCurrentTimeMillis()));
            map.put("fee_type", WeChatConstant.FEE_TYPE);
            map.put("total_fee", String.valueOf(opr.getPayMoney()));
            map.put("spbill_create_ip", CusAccessObjectUtil.getIpAddress(req));//终端IP
            map.put("notify_url", WeChatConstant.NOTIFY_URL);
            map.put("trade_type", "NATIVE");
            String reqXml = WechatUtil.getRequestData(map, WeChatConstant.KEY);
            String result = HttpRequestUtil.sendHttpPostRequestXML(WeChatConstant.WECHAT_UNIFIEDORDER_URL, reqXml);

            Map<String, String> orderMap = XMLUtil.testGetRoot(result);
            logger.info("orderMap===========================" + orderMap);
            if ("FAIL".equals(orderMap.get("return_code"))) {
                logger.info("微信创建支付订单失败,错误原因为:" + orderMap.get("return_msg"));
                return new ResultModel(ResultCode.FAIL, orderMap.get("return_msg")).createResp();
            }
            if ("FAIL".equals(orderMap.get("result_code"))) {
                logger.info("微信创建支付订单失败,错误原因为:" + orderMap.get("err_code_des"));
                return new ResultModel(ResultCode.FAIL, orderMap.get("err_code_des")).createResp();
            }

            //生成分账信息
            SplitFundInfo info = new SplitFundInfo();
            info.setCurrency(WeChatConstant.FEE_TYPE);
            info.setAccount(WeChatConstant.APP_ID);
            info.setAccountType("1");
            info.setAmount(opr.getPayMoney());
            info.setRatio(100L);
            info.setOnlinePaymentRequestId(opr.getOnlinePaymentRequestId());
            List<SplitFundInfo> infos = splitFundInfoManager.queryEntity(info);
            if (infos != null && infos.size() > 0) {
                info.setId(infos.get(0).getId());
                splitFundInfoManager.updateByPrimaryKeySelective(info);
            } else {
                splitFundInfoManager.insertSelective(info);
            }

            //将url连接生成二维码图片
//        QRCode.writeToFile(code_url, "png", 200, 200, resp.getOutputStream());

            //得到的扫码支付url
            Object code_url = orderMap.get("code_url");
            return new ResultModel(ResultCode.OK, code_url).createResp();
        } catch (IllegalArgumentException iae) {
            logger.error("前置检查条件错误", iae);
            return new ResultModel(ResultCode.MISSING_OR_INVALID_ARGUMENTS).createResp();
        }
    }
点击查看更多内容
9人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消