liujie
5 小时以前 67d06a785d5fc4524639ace27651c01ff05a4b58
ruoyi-service/ruoyi-payment/src/main/java/com/ruoyi/payment/wx/utils/WxAbstractPay.java
@@ -1,9 +1,12 @@
package com.ruoyi.payment.wx.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.ruoyi.common.core.utils.WebUtils;
import com.ruoyi.payment.wx.model.WxCloseOrderModel;
import com.ruoyi.payment.wx.model.WxPaymentInfoModel;
import com.ruoyi.payment.wx.model.WxPaymentRefundModel;
import com.ruoyi.payment.api.model.WxPaymentRefundModel;
import com.ruoyi.payment.wx.resp.NotifyV3PayDecodeRespBody;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
@@ -68,7 +71,7 @@
     */
    protected String buildBaseParam(WxPaymentInfoModel requestBody, String notifyUrl) {
        // 封装基础数据
        requestBody.setNotify_url(notifyUrl);
        requestBody.setNotify_url(notifyUrl + requestBody.getNotify_url());
        String reqBody = WxJsonUtils.toJsonString(requestBody);
        return reqBody;
    }
@@ -198,9 +201,46 @@
                url
                , socketTimeout
                , connectTimeout);
        String repBody = result(httpClient, httpGet);
        NotifyV3PayDecodeRespBody body = WxJsonUtils.parseObject(repBody, NotifyV3PayDecodeRespBody.class);
        return body;
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            String respBodyStr = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            if (WxUtils.getLogger().isDebugEnabled()) {
                WxUtils.debug("请求成功:{}", respBodyStr);
            }
            if(404 == statusCode){
               return null;
            }
            // 成功相应
            if (STATUS_CODE == statusCode || OTHER_STATUS_CODE == statusCode ) {
                NotifyV3PayDecodeRespBody body = WxJsonUtils.parseObject(respBodyStr, NotifyV3PayDecodeRespBody.class);
                return body;
            } else {
                WxUtils.error("failed,resp code = {},return body = {}", statusCode, respBodyStr);
                throw new RuntimeException(respBodyStr);
            }
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            throw new RuntimeException("接口超时");
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            throw new RuntimeException("读取接口数据超时");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("接口请求失败,请尝试检查网络环境或请求接口是否能正常访问");
        } finally {
            // 关闭响应
            try {
                if (response != null) {
                    //关闭结果集
                    response.getEntity().getContent().close();
                    response.close();
                }
            } catch (IOException e) {
                throw new RuntimeException("关闭流异常");
            }
        }
    }
    /**
@@ -210,7 +250,7 @@
     * @param mchid
     * @return
     */
    public abstract NotifyV3PayDecodeRespBody query(String out_trade_no, String mchid);
    public abstract NotifyV3PayDecodeRespBody query(String out_trade_no);
    /**
@@ -220,6 +260,10 @@
     * @return
     */
    public abstract Map<String, Object> refund(WxPaymentRefundModel refundModel);
    public abstract String close(String out_trade_no);
    /**
     * 订单退款
@@ -246,7 +290,6 @@
        Map<String, Object> body = WxJsonUtils.parseObject(repBody, Map.class);
        return body;
    }
    /**
     * 请求结果
@@ -329,5 +372,30 @@
            writer.close();
        }
    }
    /**
     * 关闭订单
     * @param httpClient
     * @param uri
     * @param httpReadTimeoutMs
     * @param httpConnectTimeoutMs
     * @param closeModel
     * @return
     */
    public String close(CloseableHttpClient httpClient,
                                      String uri,
                                      int httpReadTimeoutMs,
                                      int httpConnectTimeoutMs,
                                     WxCloseOrderModel closeModel) {
        String reqBody = WxJsonUtils.toJsonString(closeModel);
        //请求URL
        HttpEntityEnclosingRequestBase httpPost = requestPost(
                uri
                , httpReadTimeoutMs
                , httpConnectTimeoutMs, reqBody);
        String repBody = result(httpClient, httpPost);
        return repBody;
    }
}