admin
2024-01-23 81da61b828e29b7745e1382dfbbaeb685dc083ef
utils/src/main/java/org/yeshi/utils/wx/WXPayV3Util.java
@@ -11,6 +11,7 @@
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
@@ -65,7 +66,7 @@
     */
    private static JSONObject request(String url, String requestData, WXAPPInfo app) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        HttpGet httpGet=new HttpGet(url);
        if (StringUtil.isNullOrEmpty(requestData)) {
            requestData = "{}";
@@ -81,6 +82,43 @@
        //完成签名并执行请求
        CloseableHttpClient httpClient = getHttpClient(app);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
                String result = EntityUtils.toString(response.getEntity());
                JSONObject resultJson = JSONObject.fromObject(result);
                return resultJson;
            } else if (statusCode == 204) {
                System.out.println("success");
            } else {
                System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity()));
                throw new Exception("request failed");
            }
        } finally {
            response.close();
        }
        return null;
    }
    private static JSONObject requestGet(String url, String requestData, WXAPPInfo app) throws Exception {
        HttpGet httpGet=new HttpGet(url);
        if (StringUtil.isNullOrEmpty(requestData)) {
            requestData = "{}";
        }
//        if (!StringUtil.isNullOrEmpty(requestData)) {
//            StringEntity entity = new StringEntity(requestData, ContentType.APPLICATION_JSON.withCharset(Charset.forName("UTF-8")));
//            entity.setContentType("application/json;charset=utf-8");
//            httpPost.setEntity(entity);
//        }
        httpGet.setHeader("Accept", "application/json;charset=utf-8");
        //完成签名并执行请求
        CloseableHttpClient httpClient = getHttpClient(app);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
@@ -188,7 +226,7 @@
     */
    public static WXPayOrderInfoV3 getPayOrderInfo(String orderNo, WXAPPInfo app) throws Exception {
        String url = String.format("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/%s?mchid=%s", URLEncoder.encode(orderNo), app.getMchId());
        JSONObject result = request(url, "", app);
        JSONObject result = requestGet(url, "", app);
        if (result == null)
            return null;
        return new Gson().fromJson(result.toString(), WXPayOrderInfoV3.class);