写一个微信和支付宝订单的查询方法:

1.支付宝的订单查询

 /**
     * 支付宝订单查询接口
     * 2019-08-13
     * @param orderId 我们系统的订单号
     * @return Map: status:0 支付成功 1:失败
     */
    public static Map alipayCreateOrderQuery(String orderId) {
        Map map = new LinkedHashMap();
        //产生的订单字符串
        String orderStatus = "";

        //实例化客户端(参数:网关地址、商户appid、商户私钥、格式、编码、支付宝公钥、加密类型)
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig. aliPayUrl,
                AlipayConfig.appId, AlipayConfig.developerSpoon,
                "json", AlipayConfig.input_charset, AlipayConfig.aliPaySpoon, "RSA2");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        AlipayTradeQueryModel model= new AlipayTradeQueryModel();
        model.setOutTradeNo(orderId);//商户网站唯一订单号,请保证OutTradeNo值每次保证唯一
        request.setBizModel(model);
        AlipayTradeQueryResponse response = null;
        try {
            response = alipayClient.execute(request);
            System.out.println(JSONObject.fromObject(response).toString());
            Map queryMap=JSONObject.fromObject(response);
            /*System.out.println("code="+queryMap.get("code"));
            System.out.println("tradeNo="+queryMap.get("tradeNo"));
            System.out.println("outTradeNo="+queryMap.get("outTradeNo"));
            System.out.println("tradeStatus="+queryMap.get("tradeStatus"));
            System.out.println("totalAmount="+queryMap.get("totalAmount"));*/
             /*交易状态:WAIT_BUYER_PAY(交易创建,等待买家付款)、
             TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
             TRADE_SUCCESS(交易支付成功)、
             TRADE_FINISHED(交易结束,不可退款)
             */
            map.put("tradeStatus",queryMap.get("tradeStatus"));
            map.put("totalAmount",queryMap.get("totalAmount"));
            map.put("outTradeNo",queryMap.get("outTradeNo"));
            map.put("tradeNo",queryMap.get("tradeNo"));
            map.put("code",queryMap.get("code"));
            map.put("subMsg",queryMap.get("subMsg"));
            map.put("success",queryMap.get("success"));
            orderStatus=queryMap.get("tradeStatus").toString();
        } catch (AlipayApiException e) {
            String massage = "alipay.trade.query接口:订单签名错误";
            System.out.println(massage);
        }
        if (response.isSuccess()) {
            if (orderStatus.equals("TRADE_SUCCESS")){
                map.put("status", 0);//订单支付成功  status:0 成功 1:失败
                logger.info("支付宝:支付订单支付结果查询:订单out_trade_no----"+orderId+"---支付成功!");
            }else{
                map.put("status",1);//订单支付成功  status:0 成功 1:失败
                logger.info("支付宝:支付订单支付结果查询:订单out_trade_no----"+orderId+"---支付失败!"+map.get("subMsg"));
            }

        } else {
            logger.info("支付宝:支付订单支付结果查询:订单out_trade_no----"+orderId+"---支付失败!");
            map.put("status",1);//订单支付成功  status:0 成功 1:失败
        }
        return map;
    }

2.微信的订单结果查询:

  /**
     * 查询微信订单信息
     * 20190813
     * 场景:刷卡支付、公共号支付、扫码支付、APP支付
     * @param order_no 外部商家的唯一订单号
     * @return Map: status:0 支付成功 1:支付失败 2:通信错误 3:未知错误
     */
    public static Map orderQuery(String order_no) {
        //订单查询结果存放地点
        Map map = new LinkedHashMap();

        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
        String noncStr = WebUtil.genNonceStr();//随机字符串
        params.put("appid", WeixinPay.APPID);
        params.put("&mch_id", WeixinPay.MCH_ID);
        params.put("&nonce_str", noncStr);
        params.put("&out_trade_no", order_no);//商户订单号
        //添加密钥key
        params.put("&key", WeixinPay.api_sec);
        //生成签名
        String sign = WeixinPay.generateSign(params);
        //System.out.println("sign==" + sign);
        //组装查询订单接口需要的xml文件
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        sb.append("<appid>" + WeixinPay.APPID + "</appid>");//公众账号ID
        sb.append("<mch_id>" + WeixinPay.MCH_ID + "</mch_id>");//商户号
        sb.append("<nonce_str>" + noncStr + "</nonce_str>");
        sb.append("<out_trade_no>" + order_no + "</out_trade_no>");
        sb.append("<sign>" + sign + "</sign>");
        sb.append("</xml>");
        System.out.println("微信查询订单接口请求:" + sb);

        //调用微信的查询订单接口
        String result = HttpUitl.postXML(WeixinPay.query_order, sb.toString());
        System.out.println("微信查询订单接口返回数据:" + result);
        LOG.info("订单查询返回结果:" + result);

        //将结果转成可以获取的Element
        Element root = WebUtil.getXmlRoot(result);

        //微信返回状态码 return_code  此字段是通信标识,非交易标识,交易是否成功需要查看trade_state来判断
        String return_code = root.element("return_code").getTextTrim();
        String return_msg = root.element("return_msg").getTextTrim();
        System.out.println("微信返回状态码:return_code: " + return_code);
        if (!WebUtil.checkEmpty(return_code) && return_code.equals("SUCCESS")) {
            //result_code 业务结果 有可能是失败或者成功
            String result_code = root.element("result_code").getTextTrim();
            if (!WebUtil.checkEmpty(result_code) && result_code.equals("SUCCESS")) {
                /**
                 * 交易状态 trade_state:SUCCESS—支付成功   REFUND—转入退款   NOTPAY—未支付
                 * CLOSED—已关闭  REVOKED—已撤销(付款码支付)  USERPAYING--用户支付中(付款码支付)  PAYERROR--支付失败(其他原因,如银行返回失败)
                 */
                String trade_state = root.element("trade_state").getTextTrim();  //交易状态
                System.out.println("支付结果:" + trade_state);
                if (trade_state.equals("SUCCESS")) {
                    System.out.println("支付成功:" + trade_state);
                    map.put("status", 0);//订单支付成功  status:0 支付成功 1:支付失败 2:接口通信失败
                    map.put("returnMsg", return_msg);
                    map.put("result_code", result_code);//业务结果
                    map.put("trade_state", trade_state);//支付状态
                    map.put("trade_state_desc", root.element("trade_state_desc").getTextTrim());//支付结果描述
                    LOG.info("微信查询订单接口通信支付成功:" + order_no + "===" + trade_state + "==" + root.element("trade_state_desc").getTextTrim());
                    return map;
                } else {
                    map.put("status", 1);//订单支付成功  status:0 支付成功 1:支付失败 2:接口通信失败
                    map.put("returnMsg", return_msg);
                    map.put("result_code", result_code);//业务结果
                    map.put("trade_state", trade_state);//支付状态
                    map.put("trade_state_desc", root.element("trade_state_desc").getTextTrim());//支付结果描述
                    LOG.info("微信查询订单接口通信支付失败:" + order_no + "===" + trade_state + "==" + root.element("trade_state_desc").getTextTrim());
                    System.out.println(root.element("trade_state_desc").getTextTrim() + ":" + trade_state);
                    return map;
                }
            } else {
                //业务失败
                String err_code = root.element("err_code").getTextTrim();//错误代码
                String err_code_des = root.element("err_code_des").getTextTrim();//错误代码描述
                System.out.println("业务结果:" + result_code);
                System.out.println("错误代码:" + err_code);
                System.out.println("错误代码描述:" + err_code_des);

                map.put("status", 1);//订单支付成功  status:0 支付成功 1:支付失败 2:接口通信失败
                map.put("returnMsg", return_msg);
                map.put("result_code", result_code);//业务结果
                map.put("err_code", err_code);//错误代码
                map.put("err_code_des", err_code_des);//错误代码描述:
                return map;
            }
        } else if (!WebUtil.checkEmpty(return_code) && return_code.equals("FAIL")) {
            LOG.info("微信查询订单接口通信失败:" + return_msg);
            map.put("status", 2);//订单支付成功  status:0 支付成功 1:支付失败 2:接口通信失败
            map.put("returnMsg", return_msg);
            return map;
        } else {
            map.put("status", 3);//订单支付成功  status:0 支付成功 1:支付失败 2:接口通信失败
            map.put("returnMsg", "未知错误");
            return map;
        }
    }

注意微信的返回的格式有几种:

  /*
        orderQuery("12333354");//手机微信支付 未支付
        orderQuery("12335769");//成功支付手机微信支付
        orderQuery("12198362");//H5微信支付
        */
        /**
         *   取消订单
         *     "return_code":"SUCCESS",
         *     "return_msg":"OK",
         *     "appid":"wxa1261e22bf0e9094",
         *     "mch_id":"1490693182",
         *     "nonce_str":"nGm8i4qKwM9RDnMG",
         *     "sign":"DEF82798EC4A27B2B272DE72F90D4949",
         *     "result_code":"FAIL",
         *     "err_code":"ORDERNOTEXIST",
         *     "err_code_des":"订单不存在"
         */

        /***
         *  订单未支付
         * <xml>
         * <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg>
         * <appid><![CDATA[wxa1261e22bf0e9094]]></appid><mch_id><![CDATA[1490693182]]></mch_id>
         * <device_info><![CDATA[]]></device_info>
         * <nonce_str><![CDATA[DkYcSIPJ4bxqHWdL]]></nonce_str>
         * <sign><![CDATA[E4E638E10110B80B7023EDB71AD16CC6]]></sign>
         * <result_code><![CDATA[SUCCESS]]></result_code>
         * <total_fee>100</total_fee><out_trade_no><![CDATA[12333354]]></out_trade_no>
         * <trade_state><![CDATA[NOTPAY]]></trade_state>
         * <trade_state_desc><![CDATA[订单未支付]]></trade_state_desc>
         * </xml>
         * {
         *     "return_code":"SUCCESS",
         *     "return_msg":"OK",
         *     "appid":"wxa1261e22bf0e9094",
         *     "mch_id":"1490693182",
         *     "device_info":"",
         *     "nonce_str":"DkYcSIPJ4bxqHWdL",
         *     "sign":"E4E638E10110B80B7023EDB71AD16CC6",
         *     "result_code":"SUCCESS",
         *     "total_fee":"100",
         *     "out_trade_no":"12333354",
         *     "trade_state":"NOTPAY",
         *     "trade_state_desc":"订单未支付"
         * }
         */
        /****
         *  订单支付成功
         *
         *  <xml>
         *   <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg>
         *  <appid><![CDATA[wxa1261e22bf0e9094]]></appid><mch_id><![CDATA[1490693182]]></mch_id>
         *  <nonce_str><![CDATA[H61dzJmI0YUk4QmF]]></nonce_str>
         *  <sign><![CDATA[273C5CBF28C69823454049A4FE55F88E]]></sign>
         *  <result_code><![CDATA[SUCCESS]]></result_code><openid><![CDATA[ovo1y1Ge0OFaFHi__BfYlNTKh3v4]]></openid>
         *  <is_subscribe><![CDATA[N]]></is_subscribe><trade_type><![CDATA[APP]]></trade_type>
         *  <bank_type><![CDATA[CFT]]></bank_type><total_fee>1</total_fee><fee_type><![CDATA[CNY]]></fee_type>
         *  <transaction_id><![CDATA[4200000387201908095282847008]]></transaction_id>
         *  <out_trade_no><![CDATA[12335769]]></out_trade_no><attach><![CDATA[]]></attach>
         *  <time_end><![CDATA[20190809174000]]></time_end>
         *  <trade_state><![CDATA[SUCCESS]]></trade_state>
         *  <cash_fee>1</cash_fee>
         *  <trade_state_desc><![CDATA[支付成功]]></trade_state_desc>
         *  </xml>
         *
                 {
                 "return_code":"SUCCESS",
                 "return_msg":"OK",
                 "appid":"wxa1261e22bf0e9094",
                 "mch_id":"1490693182",
                 "nonce_str":"H61dzJmI0YUk4QmF",
                 "sign":"273C5CBF28C69823454049A4FE55F88E",
                 "result_code":"SUCCESS",
                 "openid":"ovo1y1Ge0OFaFHi__BfYlNTKh3v4",
                 "is_subscribe":"N",
                 "trade_type":"APP",
                 "bank_type":"CFT",
                 "total_fee":"1",
                 "fee_type":"CNY",
                 "transaction_id":"4200000387201908095282847008",
                 "out_trade_no":"12335769",
                 "attach":"",
                 "time_end":"20190809174000",
                 "trade_state":"SUCCESS",
                 "cash_fee":"1",
                 "trade_state_desc":"支付成功"
                 }
         *
         */

 

posted @ 2019-08-13 11:34  坠落凡尘的魔鬼  阅读(1471)  评论(0编辑  收藏  举报