旋转的钢笔
2020-07-13 09:22
采纳率: 66.7%
浏览 210
已采纳

微信充值成功,但是数据库却没有记录,createOrder 没有执行?

改方法的业务逻辑是,当用户点击下单的时候执行orders方法,很奇怪的是,微信支付回调成功,数据库中却没有用户下单的数据,也就是createOrder方法没有执行,日志也没有输出什么信息,方法也单元测试了,没有找到问题所在,请大佬帮助,万分感谢!!1

/**
 * 微信支付
 *
 * @author yadu
 */
public class SendPayAction extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(SendPayAction.class);
    private Connection con_kq;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        if("orders".equals(method)){
            try {
                orders(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * @Description 微信浏览器内微信支付/公众号支付(JSAPI)
     * @param request
     * @return Map
     */
    public  void orders(HttpServletRequest request, HttpServletResponse response) {
        String wxid = request.getParameter("wxid");
        String money=request.getParameter("money");
        try {
            //判断用户
            boolean flag1=userAccount(wxid);
            if (flag1==false){
                Map<String, String> payTip = new HashMap<String, String>(16);
                payTip.put("tip","抱歉,您还没有开通微信充值账号,不能充值!");
                JSONObject json = JSONObject.fromObject(payTip);
                PrintWriter out1 = response.getWriter();
                out1.print(json);
                out1.flush();
                out1.close();
            }else {
                //String openId = "用户的openid";
                // 拼接统一下单地址参数
                Map<String, String> paraMap = new HashMap<String, String>();
                // 获取请求ip地址
                String ip = request.getHeader("x-forwarded-for");
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("Proxy-Client-IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getHeader("WL-Proxy-Client-IP");
                }
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                    ip = request.getRemoteAddr();
                }
                if (ip.indexOf(",") != -1) {
                    String[] ips = ip.split(",");
                    ip = ips[0].trim();
                }
                // 商家平台ID
                paraMap.put("appid", AuthUtil.APPID);
                // 商家名称-销售商品类目、String(128)
                paraMap.put("body", "餐卡充值");
                // 商户ID
                paraMap.put("mch_id", AuthUtil.MCHID);
                // UUID
                paraMap.put("nonce_str", WXPayUtil.generateNonceStr());
                paraMap.put("openid", wxid);
                // 订单号,每次都不同
                String orderId=OrderId.getSuffix();
                paraMap.put("out_trade_no", orderId);
                paraMap.put("spbill_create_ip", ip);
                // 支付金额,单位分
                paraMap.put("total_fee", String.valueOf(Arith.changeY2F(Double.valueOf(money))));
                // 支付类型
                paraMap.put("trade_type", "JSAPI");
                // 此路径是微信服务器调用支付结果通知路径随意写
                // paraMap.put("notify_url", "用户支付完成后,你想微信调你的哪个接口");
                String url="http://kq.yadugroup.com:8443/yadu_OA/servlet/PayResultAction";
                paraMap.put("notify_url", url);
                String sign = WXPayUtil.generateSignature(paraMap, AuthUtil.PATERNERKEY);
                paraMap.put("sign", sign);
                // 将所有参数(map)转xml格式
                String xml = WXPayUtil.mapToXml(paraMap);
                // 统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder
                String unifiedorder_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
                System.out.println("xml为:" + xml);
                // String xmlStr = HttpRequest.sendPost(unifiedorder_url,
                // xml);//发送post请求"统一下单接口"返回预支付id:prepay_id
                String xmlStr = HttpRequest.httpsRequest(unifiedorder_url, "POST", xml);
                System.out.println("xmlStr为:" + xmlStr);

                // 以下内容是返回前端页面的json数据
                // 预支付id
                String prepay_id = "";
                if (xmlStr.indexOf("SUCCESS") != -1) {
                    Map<String, String> map = WXPayUtil.xmlToMap(xmlStr);
                    prepay_id = (String) map.get("prepay_id");
                }
                Map<String, String> payMap = new HashMap<String, String>();
                payMap.put("appId", AuthUtil.APPID);
                payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp() + "");
                payMap.put("nonceStr", WXPayUtil.generateNonceStr());
                payMap.put("signType", "MD5");
                payMap.put("package", "prepay_id=" + prepay_id);
                String paySign = WXPayUtil.generateSignature(payMap, AuthUtil.PATERNERKEY);
                payMap.put("paySign", paySign);
                payMap.put("tip","");
                System.out.println("prepay_id是:" + prepay_id);

                //讲订单写入数据库
                createOrder(wxid,orderId,money,ip);
                //判断微信接口是否能链接
                if (StringUtils.isBlank(prepay_id)){
                    Map<String, String> payTip = new HashMap<String, String>(16);
                    payTip.put("tip","微信服务器连接超时,请稍后再试!");
                    JSONObject json = JSONObject.fromObject(payTip);
                    PrintWriter out1 = response.getWriter();
                    out1.print(json);
                    out1.flush();
                    out1.close();
                }else{
                    JSONObject jsonobject = JSONObject.fromObject(payMap);
                    PrintWriter out = response.getWriter();
                    out.print(jsonobject);
                    out.flush();
                    out.close();
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询员工账号是否存在
     * @param wxid
     * @return
     */
    public boolean userAccount(String wxid) throws SQLException {
        JdbcCard jdbcCard=new JdbcCard();
        if (jdbcCard.connection == null || jdbcCard.connection.isClosed()) {
            jdbcCard.connection = jdbcCard.getConnection("ydsy");
        }
        ResultSet rs_oaqj=null;
        try {
            String sql = " SELECT p.jobNumber jobNo,p.cpsn_name personName,p.vIDNo certificateNo,p.cPsnMobilePhone phoneNo,p.cpsnURL wxid from hr_hi_person p where p.cPsnURL = '" + wxid + "'  and p.dleaveDate is null ";
            Statement st_oaqj = jdbcCard.connection.createStatement();
            rs_oaqj = st_oaqj.executeQuery(sql);

            if(!rs_oaqj.isBeforeFirst()){
                jdbcCard.connection = jdbcCard.getConnection("wps");
                Statement st = jdbcCard.connection.createStatement();
                rs_oaqj = st.executeQuery(sql);
                if(!rs_oaqj.isBeforeFirst()){
                    jdbcCard.connection = jdbcCard.getConnection("mdk");
                    Statement st1 = jdbcCard.connection.createStatement();
                    rs_oaqj = st1.executeQuery(sql);
                }
            }
            while (rs_oaqj.next()) {
                String certificateNo = rs_oaqj.getString("certificateNo");
                Employee emp=new Employee();
                emp.setCertificateNo(certificateNo);
                emp.setCertificateType("111");
                String StringeResult = callPostStringApi(emp,"/api/resource/v1/person/certificateNo/personInfo");
                com.alibaba.fastjson.JSONObject jsonx = JSON.parseObject(StringeResult);
                com.alibaba.fastjson.JSONObject obj = jsonx.getJSONObject("data");
                if(obj!=null){
                    return true;
                }else {
                    return false;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.info(e.getMessage());
        }finally {
            try {
                if(rs_oaqj != null){
                    rs_oaqj.close();
                }
                if(jdbcCard.connection != null){
                    jdbcCard.connection.close();
                }
            } catch (SQLException e) {
                logger.error(e.getMessage());
            }
        }

        return false;
    }

    /**
     *
     * 下订单
     *
     */
    public void createOrder(String wxid,String orderId,String amount,String ip) throws SQLException {
        JdbcCard jdbcCard=new JdbcCard();
        if (jdbcCard.connection == null || jdbcCard.connection.isClosed()) {
            jdbcCard.connection = jdbcCard.getConnection("ydsy");
        }
        ResultSet rs_oaqj=null;
        String  insertOderSql = "insert into wx_order (orderId,totalFee,status,pid,wxurl,phone,name,ip) values(?,?,?,?,?,?,?,?)";
        try {
            String sql = " SELECT p.jobNumber jobNo,p.cpsn_name personName,p.vIDNo certificateNo,p.cPsnMobilePhone phoneNo,p.cpsnURL wxid from hr_hi_person p where p.cPsnURL = '" + wxid + "'  and p.dleaveDate is null ";
            Statement st_oaqj = jdbcCard.connection.createStatement();
            rs_oaqj = st_oaqj.executeQuery(sql);

            if(!rs_oaqj.isBeforeFirst()){
                jdbcCard.connection = jdbcCard.getConnection("wps");
                Statement st = jdbcCard.connection.createStatement();
                rs_oaqj = st.executeQuery(sql);
                if(!rs_oaqj.isBeforeFirst()){
                    jdbcCard.connection = jdbcCard.getConnection("mdk");
                    Statement st1 = jdbcCard.connection.createStatement();
                    rs_oaqj = st1.executeQuery(sql);
                }
            }
            while (rs_oaqj.next()) {
                String certificateNo = rs_oaqj.getString("certificateNo");
                String Name = rs_oaqj.getString("personName");
                String phoneNo = rs_oaqj.getString("phoneNo");
                String wxid1 = rs_oaqj.getString("wxid");
                List list=new ArrayList();
                list.add(orderId);
                list.add(amount);
                list.add("0");
                list.add(certificateNo);
                list.add(wxid1);
                list.add(phoneNo);
                list.add(Name);
                list.add(ip);
               boolean flag=updDetailed(list,insertOderSql);
               if(flag){
                   logger.info("创建订单成功");
               }else{
                   logger.info("创建订单失败");
               }
            }

        }catch (Exception e){
            e.printStackTrace();
            logger.info(e.getMessage());
        }finally {
            try {
                if(rs_oaqj != null){
                    rs_oaqj.close();
                }
                if(jdbcCard.connection != null){
                    jdbcCard.connection.close();
                }
            } catch (SQLException e) {
                logger.error(e.getMessage());
            }
        }


    }


    public boolean updDetailed(List list, String sql) {
        try {

            return updateByPreparedStatement(sql, list);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return false;
    }

    public boolean updateByPreparedStatement(String sql, List<Object> params) throws SQLException, ClassNotFoundException {
        JdbcCard jdbcCard=new JdbcCard();
        boolean flag = false;
        int result = -1;
        if ((this.con_kq == null) || (this.con_kq.isClosed())) {
            this.con_kq = jdbcCard.getKqjlCon();
        }
        PreparedStatement pstmt = con_kq.prepareStatement(sql);
        int index = 1;
        if (params != null && !params.isEmpty()) {
            for (int i = 0; i < params.size(); i++) {
                //System.out.println("^^^^^^^^"+index++);//输出1,2,3
                pstmt.setObject(index++, params.get(i));
            }
        }
        result = pstmt.executeUpdate();
        flag = result > 0 ? true : false;
        return flag;
    }

}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题