Skip to content

Commit 6720960

Browse files
committed
微信支付模块配置中增加ifSaveApiData参数,可以选择是否保存接口请求信息到ThreadLocal中方便读取
1 parent a5c8f13 commit 6720960

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public class WxPayConfig {
8888
* 默认不使用
8989
*/
9090
private boolean useSandboxEnv = false;
91+
92+
/**
93+
* 是否将接口请求日志信息保存到threadLocal中.
94+
* 默认不保存
95+
*/
96+
private boolean ifSaveApiData = false;
97+
9198
private String httpProxyHost;
9299
private Integer httpProxyPort;
93100
private String httpProxyUsername;

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.github.binarywang.wxpay.service.impl;
22

3-
import com.github.binarywang.wxpay.bean.WxPayApiData;
4-
import com.github.binarywang.wxpay.exception.WxPayException;
5-
import jodd.util.Base64;
3+
import java.io.UnsupportedEncodingException;
4+
import java.nio.charset.StandardCharsets;
5+
import javax.net.ssl.SSLContext;
6+
67
import org.apache.commons.lang3.StringUtils;
78
import org.apache.http.HttpHost;
89
import org.apache.http.auth.AuthScope;
@@ -20,9 +21,9 @@
2021
import org.apache.http.impl.client.HttpClients;
2122
import org.apache.http.util.EntityUtils;
2223

23-
import javax.net.ssl.SSLContext;
24-
import java.io.UnsupportedEncodingException;
25-
import java.nio.charset.StandardCharsets;
24+
import com.github.binarywang.wxpay.bean.WxPayApiData;
25+
import com.github.binarywang.wxpay.exception.WxPayException;
26+
import jodd.util.Base64;
2627

2728
/**
2829
* <pre>
@@ -65,15 +66,19 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
6566
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
6667
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
6768
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
68-
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
69+
if (this.getConfig().isIfSaveApiData()) {
70+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
71+
}
6972
return responseString;
7073
}
7174
} finally {
7275
httpPost.releaseConnection();
7376
}
7477
} catch (Exception e) {
7578
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
76-
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
79+
if (this.getConfig().isIfSaveApiData()) {
80+
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
81+
}
7782
throw new WxPayException(e.getMessage(), e);
7883
}
7984
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public byte[] postForBytes(String url, String requestStr, boolean useKey) throws
3030
byte[] responseBytes = request.send().bodyBytes();
3131
final String responseString = Base64.encodeToString(responseBytes);
3232
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseString);
33-
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
33+
if (this.getConfig().isIfSaveApiData()) {
34+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
35+
}
3436
return responseBytes;
3537
} catch (Exception e) {
3638
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
@@ -46,7 +48,9 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
4648
String responseString = this.getResponseString(request.send());
4749

4850
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
49-
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
51+
if (this.getConfig().isIfSaveApiData()) {
52+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
53+
}
5054
return responseString;
5155
} catch (Exception e) {
5256
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());

0 commit comments

Comments
 (0)