Skip to content

Commit 4fa672d

Browse files
committed
微信支付接口增加日志
1 parent d88a8d3 commit 4fa672d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.http.impl.client.HttpClients;
2626
import org.apache.http.ssl.SSLContexts;
2727
import org.apache.http.util.EntityUtils;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
2830

2931
import javax.net.ssl.SSLContext;
3032
import java.io.File;
@@ -40,6 +42,8 @@
4042
*/
4143
public class WxMpPayServiceImpl implements WxMpPayService {
4244

45+
protected final Logger log = LoggerFactory.getLogger(this.getClass());
46+
4347
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com";
4448
private static final String[] TRADE_TYPES = new String[]{"JSAPI","NATIVE", "APP"};
4549
private static final String[] REFUND_ACCOUNT = new String[]{"REFUND_SOURCE_RECHARGE_FUNDS",
@@ -269,7 +273,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
269273
String responseContent = this.executeRequest(url, xstream.toXML(request));
270274
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
271275
.fromXML(responseContent);
272-
if ("FAIL".equals(result.getResultCode())) {
276+
if ("FAIL".equals(result.getResultCode()) || "FAIL".equals(result.getReturnCode())) {
273277
throw new WxErrorException(WxError.newBuilder()
274278
.setErrorMsg(result.getErrCode() + ":" + result.getErrCodeDes())
275279
.build());
@@ -390,17 +394,19 @@ private String executeRequest( String url, String requestStr) throws WxErrorExce
390394
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
391395

392396
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
393-
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
397+
String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
398+
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",url, requestStr, result);
399+
return result;
394400
}
395401
} catch (IOException e) {
402+
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
396403
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
397404
}finally {
398405
httpPost.releaseConnection();
399406
}
400407
}
401408

402409
private String executeRequestWithKeyFile( String url, File keyFile, String requestStr, String mchId) throws WxErrorException {
403-
404410
try (FileInputStream inputStream = new FileInputStream(keyFile)) {
405411
KeyStore keyStore = KeyStore.getInstance("PKCS12");
406412
keyStore.load(inputStream, mchId.toCharArray());
@@ -417,12 +423,15 @@ private String executeRequestWithKeyFile( String url, File keyFile, String reque
417423
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
418424
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
419425
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
420-
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
426+
String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
427+
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",url, requestStr, result);
428+
return result;
421429
}
422430
}finally {
423431
httpPost.releaseConnection();
424432
}
425433
} catch (Exception e) {
434+
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
426435
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
427436
}
428437
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class WxMpServiceImpl implements WxMpService {
3333

3434
private static final JsonParser JSON_PARSER = new JsonParser();
3535

36-
protected final Logger log = LoggerFactory.getLogger(WxMpServiceImpl.class);
36+
protected final Logger log = LoggerFactory.getLogger(this.getClass());
3737

3838
/**
3939
* 全局的是否正在刷新access token的锁

0 commit comments

Comments
 (0)