2525import org .apache .http .impl .client .HttpClients ;
2626import org .apache .http .ssl .SSLContexts ;
2727import org .apache .http .util .EntityUtils ;
28+ import org .slf4j .Logger ;
29+ import org .slf4j .LoggerFactory ;
2830
2931import javax .net .ssl .SSLContext ;
3032import java .io .File ;
4042 */
4143public 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 }
0 commit comments