@@ -73,11 +73,17 @@ public class WxMpPayServiceImpl implements WxMpPayService {
7373 "REFUND_SOURCE_UNSETTLED_FUNDS" };
7474 protected final Logger log = LoggerFactory .getLogger (this .getClass ());
7575 private WxMpService wxMpService ;
76- private WxMpConfigStorage config ;
76+ private WxMpConfigStorage config = null ;
7777
7878 public WxMpPayServiceImpl (WxMpService wxMpService ) {
7979 this .wxMpService = wxMpService ;
80- this .config = wxMpService .getWxMpConfigStorage ();
80+ }
81+
82+ private WxMpConfigStorage getConfig (){
83+ if (config == null ){
84+ this .config = wxMpService .getWxMpConfigStorage ();
85+ }
86+ return this .config ;
8187 }
8288
8389 @ Override
@@ -89,8 +95,8 @@ public WxPayRefundResult refund(WxPayRefundRequest request, File keyFile)
8995 xstream .processAnnotations (WxPayRefundRequest .class );
9096 xstream .processAnnotations (WxPayRefundResult .class );
9197
92- request .setAppid (this . config .getAppId ());
93- String partnerId = this . config .getPartnerId ();
98+ request .setAppid (getConfig () .getAppId ());
99+ String partnerId = getConfig () .getPartnerId ();
94100 request .setMchId (partnerId );
95101 request .setNonceStr (System .currentTimeMillis () + "" );
96102 request .setOpUserId (partnerId );
@@ -120,8 +126,8 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
120126 request .setOutRefundNo (StringUtils .trimToNull (outRefundNo ));
121127 request .setRefundId (StringUtils .trimToNull (refundId ));
122128
123- request .setAppid (this . config .getAppId ());
124- request .setMchId (this . config .getPartnerId ());
129+ request .setAppid (getConfig () .getAppId ());
130+ request .setMchId (getConfig () .getPartnerId ());
125131 request .setNonceStr (System .currentTimeMillis () + "" );
126132 request .setSign (this .createSign (request ));
127133
@@ -138,17 +144,20 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
138144 private void checkResult (WxPayBaseResult result ) throws WxErrorException {
139145 //校验返回结果签名
140146 if (!checkSign (result .toMap ())){
147+ log .debug ("校验结果签名失败,参数:{}" ,result .toMap ());
141148 throw new WxErrorException (WxError .newBuilder ().setErrorCode (-1 ).setErrorMsg ("参数格式校验错误!" ).build ());
142149 }
143150
144151 //校验结果是否成功
145152 if (!"SUCCESS" .equalsIgnoreCase (result .getReturnCode ())
146153 || !"SUCCESS" .equalsIgnoreCase (result .getResultCode ())) {
147- throw new WxErrorException (WxError .newBuilder ().setErrorCode (-1 )
148- .setErrorMsg ("返回代码: " + result .getReturnCode () + ", 返回信息: "
149- + result .getReturnMsg () + ", 结果代码: " + result .getResultCode () + ", 错误代码: "
150- + result .getErrCode () + ", 错误详情: " + result .getErrCodeDes ())
151- .build ());
154+ WxError error = WxError .newBuilder ().setErrorCode (-1 )
155+ .setErrorMsg ("返回代码: " + result .getReturnCode () + ", 返回信息: "
156+ + result .getReturnMsg () + ", 结果代码: " + result .getResultCode () + ", 错误代码: "
157+ + result .getErrCode () + ", 错误详情: " + result .getErrCodeDes ())
158+ .build ();
159+ log .debug ("结果校验失败,参数:{},详细:{}" ,result .toMap (),error );
160+ throw new WxErrorException (error );
152161 }
153162 }
154163
@@ -157,7 +166,7 @@ private void checkParameters(WxPayRefundRequest request) throws WxErrorException
157166
158167 if (StringUtils .isNotBlank (request .getRefundAccount ())) {
159168 if (!ArrayUtils .contains (REFUND_ACCOUNT , request .getRefundAccount ())) {
160- throw new IllegalArgumentException ("refund_account目前必须为" + Arrays .toString (REFUND_ACCOUNT ) + "其中之一" );
169+ throw new IllegalArgumentException ("refund_account目前必须为" + Arrays .toString (REFUND_ACCOUNT ) + "其中之一,实际值:" + request . getRefundAccount () );
161170 }
162171 }
163172
@@ -169,10 +178,12 @@ private void checkParameters(WxPayRefundRequest request) throws WxErrorException
169178 @ Override
170179 public WxPayOrderNotifyResult getOrderNotifyResult (String xmlData ) throws WxErrorException {
171180 try {
181+ log .trace ("微信支付回调参数详细:{}" , xmlData );
172182 XStream xstream = XStreamInitializer .getInstance ();
173- xstream .autodetectAnnotations ( true );
183+ xstream .processAnnotations ( WxPayOrderNotifyResult . class );
174184 xstream .registerConverter (new WxPayOrderNotifyResultConverter (xstream .getMapper (),xstream .getReflectionProvider ()));
175185 WxPayOrderNotifyResult result = (WxPayOrderNotifyResult ) xstream .fromXML (xmlData );
186+ log .debug ("微信支付回调结果对象:{}" ,result );
176187 this .checkResult (result );
177188 return result ;
178189 }catch (WxErrorException e ) {
@@ -192,8 +203,8 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File
192203 xstream .processAnnotations (WxPaySendRedpackRequest .class );
193204 xstream .processAnnotations (WxPaySendRedpackResult .class );
194205
195- request .setWxAppid (this . config .getAppId ());
196- String mchId = this . config .getPartnerId ();
206+ request .setWxAppid (getConfig () .getAppId ());
207+ String mchId = getConfig () .getPartnerId ();
197208 request .setMchId (mchId );
198209 request .setNonceStr (System .currentTimeMillis () + "" );
199210 request .setSign (this .createSign (request ));
@@ -220,8 +231,8 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) thro
220231 request .setMchBillNo (mchBillNo );
221232 request .setBillType ("MCHT" );
222233
223- request .setAppid (this . config .getAppId ());
224- String mchId = this . config .getPartnerId ();
234+ request .setAppid (getConfig () .getAppId ());
235+ String mchId = getConfig () .getPartnerId ();
225236 request .setMchId (mchId );
226237 request .setNonceStr (System .currentTimeMillis () + "" );
227238
@@ -237,7 +248,7 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) thro
237248
238249 @ Override
239250 public String createSign (Object xmlBean ) {
240- return createSign (BeanUtils .xmlBean2Map (xmlBean ),this . config .getPartnerKey ());
251+ return createSign (BeanUtils .xmlBean2Map (xmlBean ),getConfig () .getPartnerKey ());
241252 }
242253
243254 @ Override
@@ -247,7 +258,7 @@ public String createSign(Object xmlBean, String signKey) {
247258
248259 @ Override
249260 public String createSign (Map <String , String > params ) {
250- return createSign (params ,this . config .getPartnerKey ());
261+ return createSign (params ,getConfig () .getPartnerKey ());
251262 }
252263
253264 @ Override
@@ -268,7 +279,7 @@ public String createSign(Map<String, String> params, String signKey) {
268279
269280 @ Override
270281 public boolean checkSign (Object xmlBean ) {
271- return checkSign (BeanUtils .xmlBean2Map (xmlBean ) , this . config .getPartnerKey ());
282+ return checkSign (BeanUtils .xmlBean2Map (xmlBean ) , getConfig () .getPartnerKey ());
272283 }
273284
274285 @ Override
@@ -278,7 +289,7 @@ public boolean checkSign(Object xmlBean, String signKey) {
278289
279290 @ Override
280291 public boolean checkSign (Map <String , String > params ) {
281- return checkSign (params , this . config .getPartnerKey ());
292+ return checkSign (params , getConfig () .getPartnerKey ());
282293 }
283294
284295 @ Override
@@ -302,8 +313,8 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
302313 WxPayOrderQueryRequest request = new WxPayOrderQueryRequest ();
303314 request .setOutTradeNo (StringUtils .trimToNull (outTradeNo ));
304315 request .setTransactionId (StringUtils .trimToNull (transactionId ));
305- request .setAppid (this . config .getAppId ());
306- request .setMchId (this . config .getPartnerId ());
316+ request .setAppid (getConfig () .getAppId ());
317+ request .setMchId (getConfig () .getPartnerId ());
307318 request .setNonceStr (System .currentTimeMillis () + "" );
308319 request .setSign (this .createSign (request ));
309320
@@ -328,8 +339,8 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
328339
329340 WxPayOrderCloseRequest request = new WxPayOrderCloseRequest ();
330341 request .setOutTradeNo (StringUtils .trimToNull (outTradeNo ));
331- request .setAppid (this . config .getAppId ());
332- request .setMchId (this . config .getPartnerId ());
342+ request .setAppid (getConfig () .getAppId ());
343+ request .setMchId (getConfig () .getPartnerId ());
333344 request .setNonceStr (System .currentTimeMillis () + "" );
334345 request .setSign (this .createSign (request ));
335346
@@ -345,37 +356,27 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
345356 @ Override
346357 public WxPayUnifiedOrderResult unifiedOrder (WxPayUnifiedOrderRequest request )
347358 throws WxErrorException {
348- checkParameters (request );
349359
350360 XStream xstream = XStreamInitializer .getInstance ();
351361 xstream .processAnnotations (WxPayUnifiedOrderRequest .class );
352362 xstream .processAnnotations (WxPayUnifiedOrderResult .class );
353363
354- WxMpConfigStorage config = this .config ;
364+ request .setAppid (getConfig ().getAppId ());
365+ request .setMchId (getConfig ().getPartnerId ());
366+ request .setNotifyURL (getConfig ().getNotifyURL ());
367+ request .setTradeType (getConfig ().getTradeType ());
368+ request .setNonceStr (System .currentTimeMillis () + "" );
369+
370+ checkParameters (request );//校验参数
355371
356- //如果没有设置,则使用配置中默认值
357- if (StringUtils .isBlank (request .getAppid ())){
358- request .setAppid (config .getAppId ());
359- }
360- if (StringUtils .isBlank (request .getMchId ())){
361- request .setMchId (config .getPartnerId ());
362- }
363- if (StringUtils .isBlank (request .getNotifyURL ())){
364- request .setNotifyURL (config .getNotifyURL ());
365- }
366- if (StringUtils .isBlank (request .getTradeType ())){
367- request .setTradeType (config .getTradeType ());
368- }
369- if (StringUtils .isBlank (request .getNonceStr ())){
370- request .setNonceStr (System .currentTimeMillis () + "" );
371- }
372372 request .setSign (this .createSign (request ));
373-
374373 String url = PAY_BASE_URL + "/pay/unifiedorder" ;
374+ String xmlParam = xstream .toXML (request );
375+ log .debug ("微信统一下单接口,URL:{},参数:{}" ,url , xmlParam );
375376
376- String responseContent = this .executeRequest (url , xstream . toXML ( request ) );
377- WxPayUnifiedOrderResult result = ( WxPayUnifiedOrderResult ) xstream
378- .fromXML (responseContent );
377+ String responseContent = this .executeRequest (url , xmlParam );
378+ log . debug ( "微信统一下单接口,URL:{},结果:{}" , url , responseContent );
379+ WxPayUnifiedOrderResult result = ( WxPayUnifiedOrderResult ) xstream .fromXML (responseContent );
379380 this .checkResult (result );
380381 return result ;
381382 }
@@ -384,7 +385,7 @@ private void checkParameters(WxPayUnifiedOrderRequest request) throws WxErrorExc
384385 BeanUtils .checkRequiredFields (request );
385386
386387 if (!ArrayUtils .contains (TRADE_TYPES , request .getTradeType ())) {
387- throw new IllegalArgumentException ("trade_type目前必须为" + Arrays .toString (TRADE_TYPES ) + "其中之一" );
388+ throw new IllegalArgumentException ("trade_type目前必须为" + Arrays .toString (TRADE_TYPES ) + "其中之一,实际值:" + request . getTradeType () );
388389 }
389390
390391 if ("JSAPI" .equals (request .getTradeType ()) && request .getOpenid () == null ) {
@@ -406,7 +407,7 @@ public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws W
406407 }
407408
408409 Map <String , String > payInfo = new HashMap <>();
409- payInfo .put ("appId" , this . config .getAppId ());
410+ payInfo .put ("appId" , getConfig () .getAppId ());
410411 // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
411412 payInfo .put ("timeStamp" , String .valueOf (System .currentTimeMillis () / 1000 ));
412413 payInfo .put ("nonceStr" , System .currentTimeMillis () + "" );
@@ -427,8 +428,8 @@ public WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErr
427428 xstream .processAnnotations (WxEntPayRequest .class );
428429 xstream .processAnnotations (WxEntPayResult .class );
429430
430- request .setMchAppid (this . config .getAppId ());
431- request .setMchId (this . config .getPartnerId ());
431+ request .setMchAppid (getConfig () .getAppId ());
432+ request .setMchId (getConfig () .getPartnerId ());
432433 request .setNonceStr (System .currentTimeMillis () + "" );
433434 request .setSign (this .createSign (request ));
434435
@@ -447,8 +448,8 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
447448 xstream .processAnnotations (WxEntPayQueryResult .class );
448449
449450 WxEntPayQueryRequest request = new WxEntPayQueryRequest ();
450- request .setAppid (this . config .getAppId ());
451- request .setMchId (this . config .getPartnerId ());
451+ request .setAppid (getConfig () .getAppId ());
452+ request .setMchId (getConfig () .getPartnerId ());
452453 request .setNonceStr (System .currentTimeMillis () + "" );
453454 request .setSign (this .createSign (request ));
454455
0 commit comments