@@ -74,21 +74,20 @@ public WxPayRefundResult refund(WxPayRefundRequest request, File keyFile)
74
74
75
75
String url = PAY_BASE_URL + "/secapi/pay/refund" ;
76
76
String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), partnerId );
77
- WxPayRefundResult wxMpPayRefundResult = (WxPayRefundResult ) xstream .fromXML (responseContent );
78
-
79
- if (!"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getResultCode ())
80
- || !"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getReturnCode ())) {
81
- WxError error = new WxError ();
82
- error .setErrorCode (-1 );
83
- error .setErrorMsg ("return_code:" + wxMpPayRefundResult .getReturnCode ()
84
- + ";return_msg:" + wxMpPayRefundResult .getReturnMsg ()
85
- + ";result_code:" + wxMpPayRefundResult .getResultCode () + ";err_code"
86
- + wxMpPayRefundResult .getErrCode () + ";err_code_des"
87
- + wxMpPayRefundResult .getErrCodeDes ());
88
- throw new WxErrorException (error );
89
- }
77
+ WxPayRefundResult result = (WxPayRefundResult ) xstream .fromXML (responseContent );
78
+ this .checkResult (result );
79
+ return result ;
80
+ }
90
81
91
- return wxMpPayRefundResult ;
82
+ private void checkResult (WxPayBaseResult result ) throws WxErrorException {
83
+ if (!"SUCCESS" .equalsIgnoreCase (result .getReturnCode ())
84
+ || !"SUCCESS" .equalsIgnoreCase (result .getResultCode ())) {
85
+ throw new WxErrorException (WxError .newBuilder ().setErrorCode (-1 )
86
+ .setErrorMsg ("返回代码:" + result .getReturnCode () + ", 返回信息: "
87
+ + result .getReturnMsg () + ", 结果代码: " + result .getResultCode () + ", 错误代码: "
88
+ + result .getErrCode () + ", 错误详情: " + result .getErrCodeDes ())
89
+ .build ());
90
+ }
92
91
}
93
92
94
93
private void checkParameters (WxPayRefundRequest request ) throws WxErrorException {
@@ -147,16 +146,10 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File
147
146
}
148
147
149
148
String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), mchId );
150
- WxPaySendRedpackResult redpackResult = (WxPaySendRedpackResult ) xstream
149
+ WxPaySendRedpackResult result = (WxPaySendRedpackResult ) xstream
151
150
.fromXML (responseContent );
152
- if ("FAIL" .equals (redpackResult .getResultCode ())) {
153
- throw new WxErrorException (WxError .newBuilder ()
154
- .setErrorMsg (
155
- redpackResult .getErrCode () + ":" + redpackResult .getErrCodeDes ())
156
- .build ());
157
- }
158
-
159
- return redpackResult ;
151
+ this .checkResult (result );
152
+ return result ;
160
153
}
161
154
162
155
/**
@@ -209,12 +202,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
209
202
String responseContent = this .executeRequest (url , xstream .toXML (request ));
210
203
WxPayOrderQueryResult result = (WxPayOrderQueryResult ) xstream .fromXML (responseContent );
211
204
result .composeCoupons (responseContent );
212
- if ("FAIL" .equals (result .getResultCode ())) {
213
- throw new WxErrorException (WxError .newBuilder ()
214
- .setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ())
215
- .build ());
216
- }
217
-
205
+ this .checkResult (result );
218
206
return result ;
219
207
}
220
208
@@ -242,11 +230,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
242
230
243
231
String responseContent = this .executeRequest (url , xstream .toXML (request ));
244
232
WxPayOrderCloseResult result = (WxPayOrderCloseResult ) xstream .fromXML (responseContent );
245
- if ("FAIL" .equals (result .getResultCode ())) {
246
- throw new WxErrorException (WxError .newBuilder ()
247
- .setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ())
248
- .build ());
249
- }
233
+ this .checkResult (result );
250
234
251
235
return result ;
252
236
}
@@ -273,12 +257,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
273
257
String responseContent = this .executeRequest (url , xstream .toXML (request ));
274
258
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult ) xstream
275
259
.fromXML (responseContent );
276
- if ("FAIL" .equals (result .getResultCode ()) || "FAIL" .equals (result .getReturnCode ())) {
277
- throw new WxErrorException (WxError .newBuilder ()
278
- .setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ())
279
- .build ());
280
- }
281
-
260
+ this .checkResult (result );
282
261
return result ;
283
262
}
284
263
@@ -301,16 +280,6 @@ private void checkParameters(WxPayUnifiedOrderRequest request) throws WxErrorExc
301
280
@ Override
302
281
public Map <String , String > getPayInfo (WxPayUnifiedOrderRequest request ) throws WxErrorException {
303
282
WxPayUnifiedOrderResult unifiedOrderResult = this .unifiedOrder (request );
304
-
305
- if (!"SUCCESS" .equalsIgnoreCase (unifiedOrderResult .getReturnCode ())
306
- || !"SUCCESS" .equalsIgnoreCase (unifiedOrderResult .getResultCode ())) {
307
- throw new WxErrorException (WxError .newBuilder ().setErrorCode (-1 )
308
- .setErrorMsg ("return_code:" + unifiedOrderResult .getReturnCode () + ";return_msg:"
309
- + unifiedOrderResult .getReturnMsg () + ";result_code:" + unifiedOrderResult .getResultCode () + ";err_code"
310
- + unifiedOrderResult .getErrCode () + ";err_code_des" + unifiedOrderResult .getErrCodeDes ())
311
- .build ());
312
- }
313
-
314
283
String prepayId = unifiedOrderResult .getPrepayId ();
315
284
if (StringUtils .isBlank (prepayId )) {
316
285
throw new RuntimeException (String .format ("Failed to get prepay id due to error code '%s'(%s)." ,
@@ -352,10 +321,7 @@ public WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErr
352
321
353
322
String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), request .getMchId ());
354
323
WxEntPayResult result = (WxEntPayResult ) xstream .fromXML (responseContent );
355
- if ("FAIL" .equals (result .getResultCode ())) {
356
- throw new WxErrorException (
357
- WxError .newBuilder ().setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ()).build ());
358
- }
324
+ this .checkResult (result );
359
325
return result ;
360
326
}
361
327
@@ -377,10 +343,7 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
377
343
378
344
String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), request .getMchId ());
379
345
WxEntPayQueryResult result = (WxEntPayQueryResult ) xstream .fromXML (responseContent );
380
- if ("FAIL" .equals (result .getResultCode ())) {
381
- throw new WxErrorException (
382
- WxError .newBuilder ().setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ()).build ());
383
- }
346
+ this .checkResult (result );
384
347
return result ;
385
348
}
386
349
@@ -400,7 +363,7 @@ private String executeRequest( String url, String requestStr) throws WxErrorExce
400
363
}
401
364
} catch (IOException e ) {
402
365
this .log .error ("\n [URL]: {}\n [PARAMS]: {}\n [EXCEPTION]: {}" , url , requestStr , e .getMessage ());
403
- throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
366
+ throw new WxErrorException (WxError .newBuilder ().setErrorCode (- 1 ). setErrorMsg (e .getMessage ()).build (), e );
404
367
}finally {
405
368
httpPost .releaseConnection ();
406
369
}
@@ -432,7 +395,7 @@ private String executeRequestWithKeyFile( String url, File keyFile, String reque
432
395
}
433
396
} catch (Exception e ) {
434
397
this .log .error ("\n [URL]: {}\n [PARAMS]: {}\n [EXCEPTION]: {}" , url , requestStr , e .getMessage ());
435
- throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
398
+ throw new WxErrorException (WxError .newBuilder ().setErrorCode (- 1 ). setErrorMsg (e .getMessage ()).build (), e );
436
399
}
437
400
}
438
401
0 commit comments