Skip to content

Commit 077e445

Browse files
committed
微信支付增加获取微信的请求和响应数据的方法getWxApiData(),方便使用者获取使用该数据
1 parent bdf7f79 commit 077e445

File tree

7 files changed

+123
-5
lines changed

7 files changed

+123
-5
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.github.binarywang.wxpay.bean;
2+
3+
/**
4+
* <pre>
5+
* 微信支付接口请求数据封装对象
6+
* Created by Binary Wang on 2017-8-25.
7+
* </pre>
8+
*
9+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
10+
*/
11+
public class WxPayApiData {
12+
/**
13+
* 接口请求地址
14+
*/
15+
private String url;
16+
17+
/**
18+
* 请求数据
19+
*/
20+
private String requestData;
21+
22+
/**
23+
* 响应数据
24+
*/
25+
private String responseData;
26+
27+
/**
28+
* 接口请求异常信息
29+
*/
30+
private String exceptionMsg;
31+
32+
/**
33+
* @param url 接口请求地址
34+
* @param requestData 请求数据
35+
* @param responseData 响应数据
36+
* @param exceptionMsg 接口请求异常信息
37+
*/
38+
public WxPayApiData(String url, String requestData, String responseData, String exceptionMsg) {
39+
this.url = url;
40+
this.requestData = requestData;
41+
this.responseData = responseData;
42+
this.exceptionMsg = exceptionMsg;
43+
}
44+
45+
public String getUrl() {
46+
return this.url;
47+
}
48+
49+
public void setUrl(String url) {
50+
this.url = url;
51+
}
52+
53+
public String getRequestData() {
54+
return this.requestData;
55+
}
56+
57+
public void setRequestData(String requestData) {
58+
this.requestData = requestData;
59+
}
60+
61+
public String getResponseData() {
62+
return this.responseData;
63+
}
64+
65+
public void setResponseData(String responseData) {
66+
this.responseData = responseData;
67+
}
68+
69+
public String getExceptionMsg() {
70+
return this.exceptionMsg;
71+
}
72+
73+
public void setExceptionMsg(String exceptionMsg) {
74+
this.exceptionMsg = exceptionMsg;
75+
}
76+
77+
@Override
78+
public String toString() {
79+
if (this.exceptionMsg != null) {
80+
return String.format("\n【请求地址】:%s\n【请求数据】:%s\n【异常信息】:%s",
81+
this, url, this.requestData, this.exceptionMsg);
82+
}
83+
84+
return String.format("\n【请求地址】:%s\n【请求数据】:%s\n【响应数据】:%s",
85+
this.url, this.requestData, this.responseData);
86+
}
87+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.service;
22

3+
import com.github.binarywang.wxpay.bean.WxPayApiData;
34
import com.github.binarywang.wxpay.bean.coupon.*;
45
import com.github.binarywang.wxpay.bean.request.*;
56
import com.github.binarywang.wxpay.bean.result.*;
@@ -368,4 +369,9 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
368369
* </pre>
369370
*/
370371
WxPayCouponInfoQueryResult queryCouponInfo(WxPayCouponInfoQueryRequest request) throws WxPayException;
372+
373+
/**
374+
* 获取微信请求数据,方便接口调用方获取处理
375+
*/
376+
WxPayApiData getWxApiData();
371377
}

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

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

33
import com.github.binarywang.utils.qrcode.QrcodeUtils;
4+
import com.github.binarywang.wxpay.bean.WxPayApiData;
45
import com.github.binarywang.wxpay.bean.coupon.*;
56
import com.github.binarywang.wxpay.bean.request.*;
67
import com.github.binarywang.wxpay.bean.result.*;
@@ -33,6 +34,7 @@
3334
public abstract class WxPayServiceAbstractImpl implements WxPayService {
3435
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com";
3536
protected final Logger log = LoggerFactory.getLogger(this.getClass());
37+
protected static ThreadLocal<WxPayApiData> wxApiData = new ThreadLocal<>();
3638

3739
protected WxPayConfig config;
3840

@@ -501,4 +503,15 @@ public WxPayCouponInfoQueryResult queryCouponInfo(WxPayCouponInfoQueryRequest re
501503
result.checkResult(this);
502504
return result;
503505
}
506+
507+
@Override
508+
public WxPayApiData getWxApiData() {
509+
try {
510+
return wxApiData.get();
511+
}finally {
512+
//一般来说,接口请求会在一个线程内进行,这种情况下,每个线程get的会是之前所存入的数据,
513+
// 但以防万一有同一线程多次请求的问题,所以每次获取完数据后移除对应数据
514+
wxApiData.remove();
515+
}
516+
}
504517
}

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

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

3+
import com.github.binarywang.wxpay.bean.WxPayApiData;
34
import com.github.binarywang.wxpay.exception.WxPayException;
45
import org.apache.commons.lang3.StringUtils;
56
import org.apache.http.auth.AuthScope;
@@ -66,15 +67,17 @@ protected String post(String url, String requestStr, boolean useKey) throws WxPa
6667
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
6768
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)));
6869
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
69-
String result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
70-
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, result);
71-
return result;
70+
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
71+
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
72+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
73+
return responseString;
7274
}
7375
} finally {
7476
httpPost.releaseConnection();
7577
}
7678
} catch (Exception e) {
7779
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
80+
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
7881
throw new WxPayException(e.getMessage(), e);
7982
}
8083
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.binarywang.wxpay.service.impl;
22

3+
import com.github.binarywang.wxpay.bean.WxPayApiData;
34
import com.github.binarywang.wxpay.exception.WxPayException;
45
import jodd.http.HttpConnectionProvider;
56
import jodd.http.HttpRequest;
@@ -53,9 +54,11 @@ protected String post(String url, String requestStr, boolean useKey) throws WxPa
5354
String responseString = this.getResponseString(request.send());
5455

5556
this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
57+
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
5658
return responseString;
5759
} catch (Exception e) {
5860
this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
61+
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
5962
throw new WxPayException(e.getMessage(), e);
6063
}
6164
}

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceAbstractImplTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void testUnifiedOrder() throws WxPayException {
5353
.outTradeNo("1111112")
5454
.build());
5555
this.logger.info(result.toString());
56+
this.logger.warn(this.payService.getWxApiData().toString());
5657
}
5758

5859
@Test

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/testbase/ApiTestModule.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
import java.io.InputStream;
1313

1414
public class ApiTestModule implements Module {
15+
private static final String TEST_CONFIG_XML = "test-config.xml";
1516

1617
@Override
1718
public void configure(Binder binder) {
18-
try (InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml")) {
19-
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, is1);
19+
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
20+
if (inputStream == null) {
21+
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到");
22+
}
23+
24+
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream);
2025
WxPayService wxService = new WxPayServiceImpl();
2126
wxService.setConfig(config);
2227

0 commit comments

Comments
 (0)