25
25
import org .apache .http .impl .client .HttpClients ;
26
26
import org .apache .http .ssl .SSLContexts ;
27
27
import org .apache .http .util .EntityUtils ;
28
+ import org .slf4j .Logger ;
29
+ import org .slf4j .LoggerFactory ;
28
30
29
31
import javax .net .ssl .SSLContext ;
30
32
import java .io .File ;
40
42
*/
41
43
public class WxMpPayServiceImpl implements WxMpPayService {
42
44
45
+ protected final Logger log = LoggerFactory .getLogger (this .getClass ());
46
+
43
47
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com" ;
44
48
private static final String [] TRADE_TYPES = new String []{"JSAPI" ,"NATIVE" , "APP" };
45
49
private static final String [] REFUND_ACCOUNT = new String []{"REFUND_SOURCE_RECHARGE_FUNDS" ,
@@ -269,7 +273,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
269
273
String responseContent = this .executeRequest (url , xstream .toXML (request ));
270
274
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult ) xstream
271
275
.fromXML (responseContent );
272
- if ("FAIL" .equals (result .getResultCode ())) {
276
+ if ("FAIL" .equals (result .getResultCode ()) || "FAIL" . equals ( result . getReturnCode ()) ) {
273
277
throw new WxErrorException (WxError .newBuilder ()
274
278
.setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ())
275
279
.build ());
@@ -390,17 +394,19 @@ private String executeRequest( String url, String requestStr) throws WxErrorExce
390
394
httpPost .setEntity (new StringEntity (new String (requestStr .getBytes ("UTF-8" ), "ISO-8859-1" )));
391
395
392
396
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 ;
394
400
}
395
401
} catch (IOException e ) {
402
+ this .log .error ("\n [URL]: {}\n [PARAMS]: {}\n [EXCEPTION]: {}" , url , requestStr , e .getMessage ());
396
403
throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
397
404
}finally {
398
405
httpPost .releaseConnection ();
399
406
}
400
407
}
401
408
402
409
private String executeRequestWithKeyFile ( String url , File keyFile , String requestStr , String mchId ) throws WxErrorException {
403
-
404
410
try (FileInputStream inputStream = new FileInputStream (keyFile )) {
405
411
KeyStore keyStore = KeyStore .getInstance ("PKCS12" );
406
412
keyStore .load (inputStream , mchId .toCharArray ());
@@ -417,12 +423,15 @@ private String executeRequestWithKeyFile( String url, File keyFile, String reque
417
423
try (CloseableHttpClient httpclient = HttpClients .custom ().setSSLSocketFactory (sslsf ).build ()) {
418
424
httpPost .setEntity (new StringEntity (new String (requestStr .getBytes ("UTF-8" ), "ISO-8859-1" )));
419
425
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 ;
421
429
}
422
430
}finally {
423
431
httpPost .releaseConnection ();
424
432
}
425
433
} catch (Exception e ) {
434
+ this .log .error ("\n [URL]: {}\n [PARAMS]: {}\n [EXCEPTION]: {}" , url , requestStr , e .getMessage ());
426
435
throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
427
436
}
428
437
}
0 commit comments