Pu Zhibing
2024-09-24 5636a3b962bc19c4e46e67646c74018037ee8792
UserZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/PayMoneyUtil.java
@@ -11,6 +11,10 @@
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.CloseOrderRequest;
import org.apache.commons.collections.map.HashedMap;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.dom4j.Document;
@@ -337,12 +341,13 @@
        xmlString.append("</xml>");
        Map<String, String> map1 = null;
        String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
        String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
        //将结果xml解析成map
        body1 = body1.replaceAll("<!\\[CDATA\\[","");
        body1 = body1.replaceAll("]]>", "");
        System.err.println("调起支付结果-------------->" + body1);
        try {
            map1 = this.xmlToMap(body1, "UTF-8");
            map1 = PayMoneyUtil.xmlToMap(body1, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
@@ -362,7 +367,7 @@
                        map2.put("nonceStr", map1.get("nonce_str"));
                        map2.put("package", "prepay_id=" + prepay_id);
                        map2.put("signType", "MD5");
                        map2.put("timeStamp", new Date().getTime() + "");
                        map2.put("timeStamp", System.currentTimeMillis() + "");
                        String s2 = this.weixinSignature(map2);
                        map2.put("prepay_id", prepay_id);
@@ -382,7 +387,7 @@
                        map3.put("package", "Sign=WXPay");
                        map3.put("partnerid", mchId);
                        map3.put("prepayid", prepay_id);
                        map3.put("timestamp", new Date().getTime() / 1000);
                        map3.put("timestamp", System.currentTimeMillis() / 1000);
                        String s1 = this.weixinSignature(map3);
                        map3.put("sign", s1);
                        System.err.println(map3);
@@ -390,11 +395,11 @@
                }
                return null;
            }else{
                System.err.println(map1.get("err_code_des"));
                System.err.println("调起支付异常-------------->" + map1.get("err_code_des"));
                return ResultUtil.error(map1.get("err_code_des"));
            }
        }else{
            System.err.println(map1.get("return_msg") + appid + "----" + mchId);
        }else{//签名错误,请检查后再试1----1602881362
            System.err.println("调起支付异常-------------->" + map1.get("return_msg") + appid + "----" + mchId);
            return ResultUtil.error(map1.get("return_msg"), new JSONObject());
        }
    }
@@ -412,7 +417,7 @@
            String param = this.getParam(request);
            param = param.replaceAll("<!\\[CDATA\\[","");
            param = param.replaceAll("]]>", "");
            Map<String, String> map = this.xmlToMap(param, "UTF-8");
            Map<String, String> map = PayMoneyUtil.xmlToMap(param, "UTF-8");
            String return_code = map.get("return_code");
            if("SUCCESS".equals(return_code)){
                String result_code = map.get("result_code");
@@ -495,7 +500,12 @@
        xmlString.append("</xml>");
        Map<String, String> map1 = null;
        String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
        String body1 = null;
        try {
            body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将结果xml解析成map
        body1 = body1.replaceAll("<!\\[CDATA\\[","");
        body1 = body1.replaceAll("]]>", "");
@@ -770,24 +780,25 @@
        }
        return null;
    }
    /**
     * 查询微信支付订单
     * @return
     * @throws Exception
     */
    public ResultUtil queryWXOrder() throws Exception{
    public ResultUtil<Map<String, Object>> queryWXOrder(String transaction_id, String out_trade_no, String tradeType) throws Exception{
        String url = "https://api.mch.weixin.qq.com/pay/orderquery";
        String nonce_str = UUIDUtil.getRandomCode(16);
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("appid", "APP".equals(tradeType) ? appid : appletsAppid);
        map.put("mch_id", mchId);
        map.put("transaction_id", nonce_str);//微信订单号
        map.put("transaction_id", transaction_id);//微信订单号
        map.put("out_trade_no", out_trade_no);//商户订单号
        map.put("nonce_str", nonce_str);//随机字符串
        String s = this.weixinSignature(map);
        map.put("sign", s);
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
@@ -801,14 +812,15 @@
            xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
        }
        xmlString.append("</xml>");
        Map<String, String> map1 = null;
        String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
        HttpResult httpResult = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
        String body1 = httpResult.getData();
        //将结果xml解析成map
        body1 = body1.replaceAll("<!\\[CDATA\\[","");
        body1 = body1.replaceAll("]]>", "");
        try {
            map1 = this.xmlToMap(body1, "UTF-8");
            map1 = PayMoneyUtil.xmlToMap(body1, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
@@ -819,21 +831,20 @@
            String result_code = map1.get("result_code");
            if("SUCCESS".equals(result_code)){
                String type = map1.get("trade_type");
                switch (type){
                    case "JSAPI":
                        break;
                    case "NATIVE":
                        String code_url = map1.get("code_url");
                        return ResultUtil.success(code_url);
                    case "APP":
                        String trade_state = map1.get("trade_state");
                        String time_end = map1.get("time_end");
                        Map<String, Object> map2 = new HashMap<>();
                        map2.put("trade_state", trade_state);//订单状态SUCCESS—支付成功,REFUND—转入退款,NOTPAY—未支付,CLOSED—已关闭,REVOKED—已撤销(刷卡支付),USERPAYING--用户支付中,PAYERROR--支付失败(其他原因,如银行返回失败)
                        map2.put("time_end", time_end);//订单支付时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。
                        return ResultUtil.success(map2);
                }
                return null;
                Map<String, Object> map2 = new HashMap<>();
                String trade_state = map1.get("trade_state");
                String time_end = map1.get("time_end");
                String transactionId = map1.get("transaction_id");
                map2.put("result_code", result_code);
                map2.put("trade_state", trade_state);//订单状态SUCCESS—支付成功,REFUND—转入退款,NOTPAY—未支付,CLOSED—已关闭,REVOKED—已撤销(刷卡支付),USERPAYING--用户支付中,PAYERROR--支付失败(其他原因,如银行返回失败)
                map2.put("time_end", time_end);//订单支付时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。
                map2.put("transaction_id", transactionId);
                return ResultUtil.success(map2);
            }
            if("FAIL".equals(result_code)){//订单不存在
                Map<String, Object> map2 = new HashMap<>();
                map2.put("result_code", result_code);
                return ResultUtil.success(map2);
            }else{
                System.err.println(map1.get("err_code_des"));
                return ResultUtil.error(map1.get("err_code_des"));
@@ -843,7 +854,31 @@
            return ResultUtil.error(map1.get("return_msg"), new JSONObject());
        }
    }
    /**
     * 微信关闭订单
     * @param out_trade_no
     */
    public void closeWXOrder(String out_trade_no) {
        // 使用自动更新平台证书的RSA配置
        // 一个商户号只能初始化一个配置,否则会因为重复的下载任务报错
        Config config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(mchId)
                    .privateKeyFromPath("D:\\app\\cert\\weixin\\1602881362\\apiclient_key.pem")
                    .merchantSerialNumber("7D555A6E50E9205504024685DF6B821319C4DD70")
                    .apiV3Key("MIIEvQIBADANBgkqhkiG9w0BAQEFAASC")
                    .build();
        // 构建service
        JsapiService service = new JsapiService.Builder().config(config).build();
        CloseOrderRequest closeRequest = new CloseOrderRequest();
        closeRequest.setMchid(mchId);
        closeRequest.setOutTradeNo(out_trade_no);
        // 方法没有返回值,意味着成功时API返回204 No Content
        service.closeOrder(closeRequest);
    }
    /**