微信支付增加获取微信的请求和响应数据的方法getWxApiData(),方便使用者获取使用该数据

This commit is contained in:
Binary Wang 2017-08-25 15:08:30 +08:00 committed by Binary Wang
parent bdf7f796e7
commit 077e44534c
7 changed files with 123 additions and 5 deletions

View File

@ -0,0 +1,87 @@
package com.github.binarywang.wxpay.bean;
/**
* <pre>
* 微信支付接口请求数据封装对象
* Created by Binary Wang on 2017-8-25.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public class WxPayApiData {
/**
* 接口请求地址
*/
private String url;
/**
* 请求数据
*/
private String requestData;
/**
* 响应数据
*/
private String responseData;
/**
* 接口请求异常信息
*/
private String exceptionMsg;
/**
* @param url 接口请求地址
* @param requestData 请求数据
* @param responseData 响应数据
* @param exceptionMsg 接口请求异常信息
*/
public WxPayApiData(String url, String requestData, String responseData, String exceptionMsg) {
this.url = url;
this.requestData = requestData;
this.responseData = responseData;
this.exceptionMsg = exceptionMsg;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getRequestData() {
return this.requestData;
}
public void setRequestData(String requestData) {
this.requestData = requestData;
}
public String getResponseData() {
return this.responseData;
}
public void setResponseData(String responseData) {
this.responseData = responseData;
}
public String getExceptionMsg() {
return this.exceptionMsg;
}
public void setExceptionMsg(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}
@Override
public String toString() {
if (this.exceptionMsg != null) {
return String.format("\n【请求地址】%s\n【请求数据】%s\n【异常信息】%s",
this, url, this.requestData, this.exceptionMsg);
}
return String.format("\n【请求地址】%s\n【请求数据】%s\n【响应数据】%s",
this.url, this.requestData, this.responseData);
}
}

View File

@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
@ -368,4 +369,9 @@ public interface WxPayService {
* </pre>
*/
WxPayCouponInfoQueryResult queryCouponInfo(WxPayCouponInfoQueryRequest request) throws WxPayException;
/**
* 获取微信请求数据方便接口调用方获取处理
*/
WxPayApiData getWxApiData();
}

View File

@ -1,6 +1,7 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.utils.qrcode.QrcodeUtils;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
@ -33,6 +34,7 @@ import java.util.Map;
public abstract class WxPayServiceAbstractImpl implements WxPayService {
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com";
protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected static ThreadLocal<WxPayApiData> wxApiData = new ThreadLocal<>();
protected WxPayConfig config;
@ -501,4 +503,15 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
result.checkResult(this);
return result;
}
@Override
public WxPayApiData getWxApiData() {
try {
return wxApiData.get();
}finally {
//一般来说接口请求会在一个线程内进行这种情况下每个线程get的会是之前所存入的数据
// 但以防万一有同一线程多次请求的问题所以每次获取完数据后移除对应数据
wxApiData.remove();
}
}
}

View File

@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.AuthScope;
@ -66,15 +67,17 @@ public class WxPayServiceApacheHttpImpl extends WxPayServiceAbstractImpl {
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)));
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
this.log.info("\n【请求地址】{}\n【请求数据】{}\n【响应数据】{}", url, requestStr, result);
return result;
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
this.log.info("\n【请求地址】{}\n【请求数据】{}\n【响应数据】{}", url, requestStr, responseString);
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
return responseString;
}
} finally {
httpPost.releaseConnection();
}
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
throw new WxPayException(e.getMessage(), e);
}
}

View File

@ -1,5 +1,6 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
@ -53,9 +54,11 @@ public class WxPayServiceJoddHttpImpl extends WxPayServiceAbstractImpl {
String responseString = this.getResponseString(request.send());
this.log.info("\n【请求地址】{}\n【请求数据】{}\n【响应数据】{}", url, requestStr, responseString);
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
return responseString;
} catch (Exception e) {
this.log.error("\n【请求地址】{}\n【请求数据】{}\n【异常信息】{}", url, requestStr, e.getMessage());
wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
throw new WxPayException(e.getMessage(), e);
}
}

View File

@ -53,6 +53,7 @@ public class WxPayServiceAbstractImplTest {
.outTradeNo("1111112")
.build());
this.logger.info(result.toString());
this.logger.warn(this.payService.getWxApiData().toString());
}
@Test

View File

@ -12,11 +12,16 @@ import java.io.IOException;
import java.io.InputStream;
public class ApiTestModule implements Module {
private static final String TEST_CONFIG_XML = "test-config.xml";
@Override
public void configure(Binder binder) {
try (InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml")) {
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, is1);
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
if (inputStream == null) {
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到");
}
XmlWxPayConfig config = this.fromXml(XmlWxPayConfig.class, inputStream);
WxPayService wxService = new WxPayServiceImpl();
wxService.setConfig(config);