@@ -44,8 +44,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
44
44
45
45
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com" ;
46
46
private static final String [] TRADE_TYPES = new String []{"JSAPI" , "NATIVE" , "APP" };
47
- private static final String [] REFUND_ACCOUNT = new String []{"REFUND_SOURCE_RECHARGE_FUNDS" ,
48
- "REFUND_SOURCE_UNSETTLED_FUNDS" };
47
+ private static final String [] REFUND_ACCOUNT = new String []{"REFUND_SOURCE_RECHARGE_FUNDS" ,"REFUND_SOURCE_UNSETTLED_FUNDS" };
49
48
private final Logger log = LoggerFactory .getLogger (this .getClass ());
50
49
private WxMpService wxMpService ;
51
50
@@ -58,23 +57,20 @@ private WxMpConfigStorage getConfig() {
58
57
}
59
58
60
59
@ Override
61
- public WxPayRefundResult refund (WxPayRefundRequest request , File keyFile )
62
- throws WxErrorException {
63
- checkParameters (request );
64
-
60
+ public WxPayRefundResult refund (WxPayRefundRequest request , File keyFile ) throws WxErrorException {
65
61
XStream xstream = XStreamInitializer .getInstance ();
66
62
xstream .processAnnotations (WxPayRefundRequest .class );
67
63
xstream .processAnnotations (WxPayRefundResult .class );
68
-
69
- request . setAppid ( getConfig (). getAppId () );
70
- String partnerId = getConfig (). getPartnerId ();
71
- request .setMchId ( partnerId );
72
- request . setNonceStr ( System . currentTimeMillis () + "" );
73
- request . setOpUserId ( partnerId );
64
+
65
+ initRequest ( request );
66
+ if ( StringUtils . isBlank ( request . getOpUserId ())){
67
+ request .setOpUserId ( getConfig (). getPartnerId () );
68
+ }
69
+ checkParameters ( request );
74
70
request .setSign (this .createSign (request ));
75
71
76
72
String url = PAY_BASE_URL + "/secapi/pay/refund" ;
77
- String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), partnerId );
73
+ String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), request . getMchId () );
78
74
WxPayRefundResult result = (WxPayRefundResult ) xstream .fromXML (responseContent );
79
75
this .checkResult (result );
80
76
return result ;
@@ -92,18 +88,14 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
92
88
xstream .processAnnotations (WxPayRefundQueryResult .class );
93
89
94
90
WxPayRefundQueryRequest request = new WxPayRefundQueryRequest ();
91
+ initRequest (request );
95
92
request .setOutTradeNo (StringUtils .trimToNull (outTradeNo ));
96
93
request .setTransactionId (StringUtils .trimToNull (transactionId ));
97
94
request .setOutRefundNo (StringUtils .trimToNull (outRefundNo ));
98
95
request .setRefundId (StringUtils .trimToNull (refundId ));
99
-
100
- request .setAppid (getConfig ().getAppId ());
101
- request .setMchId (getConfig ().getPartnerId ());
102
- request .setNonceStr (System .currentTimeMillis () + "" );
103
96
request .setSign (this .createSign (request ));
104
97
105
98
String url = PAY_BASE_URL + "/pay/refundquery" ;
106
-
107
99
String responseContent = this .executeRequest (url , xstream .toXML (request ));
108
100
WxPayRefundQueryResult result = (WxPayRefundQueryResult ) xstream .fromXML (responseContent );
109
101
result .composeRefundRecords (responseContent );
@@ -127,7 +119,7 @@ private void checkResult(WxPayBaseResult result) throws WxErrorException {
127
119
+ result .getReturnMsg () + ", 结果代码: " + result .getResultCode () + ", 错误代码: "
128
120
+ result .getErrCode () + ", 错误详情: " + result .getErrCodeDes ())
129
121
.build ();
130
- log .debug ( "结果校验失败 ,参数:{},详细:{}" , result .toMap (), error );
122
+ log .error ( "结果业务代码异常 ,参数:{},详细:{}" , result .toMap (), error );
131
123
throw new WxErrorException (error );
132
124
}
133
125
}
@@ -174,19 +166,15 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File
174
166
xstream .processAnnotations (WxPaySendRedpackRequest .class );
175
167
xstream .processAnnotations (WxPaySendRedpackResult .class );
176
168
177
- request .setWxAppid (getConfig ().getAppId ());
178
- String mchId = getConfig ().getPartnerId ();
179
- request .setMchId (mchId );
180
- request .setNonceStr (System .currentTimeMillis () + "" );
169
+ initRequest (request );
181
170
request .setSign (this .createSign (request ));
182
171
183
172
String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack" ;
184
173
if (request .getAmtType () != null ) {
185
174
//裂变红包
186
175
url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack" ;
187
176
}
188
-
189
- String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), mchId );
177
+ String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), request .getMchId ());
190
178
WxPaySendRedpackResult result = (WxPaySendRedpackResult ) xstream .fromXML (responseContent );
191
179
this .checkResult (result );
192
180
return result ;
@@ -201,75 +189,16 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) thro
201
189
WxPayRedpackQueryRequest request = new WxPayRedpackQueryRequest ();
202
190
request .setMchBillNo (mchBillNo );
203
191
request .setBillType ("MCHT" );
204
-
205
- request .setAppid (getConfig ().getAppId ());
206
- String mchId = getConfig ().getPartnerId ();
207
- request .setMchId (mchId );
208
- request .setNonceStr (System .currentTimeMillis () + "" );
209
-
210
- String sign = this .createSign (request );
211
- request .setSign (sign );
192
+ initRequest (request );
193
+ request .setSign (this .createSign (request ));
212
194
213
195
String url = PAY_BASE_URL + "/mmpaymkttransfers/gethbinfo" ;
214
- String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), mchId );
196
+ String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), request . getMchId () );
215
197
WxPayRedpackQueryResult result = (WxPayRedpackQueryResult ) xstream .fromXML (responseContent );
216
198
this .checkResult (result );
217
199
return result ;
218
200
}
219
201
220
- @ Override
221
- public String createSign (Object xmlBean ) {
222
- return createSign (BeanUtils .xmlBean2Map (xmlBean ), getConfig ().getPartnerKey ());
223
- }
224
-
225
- @ Override
226
- public String createSign (Object xmlBean , String signKey ) {
227
- return createSign (BeanUtils .xmlBean2Map (xmlBean ), signKey );
228
- }
229
-
230
- @ Override
231
- public String createSign (Map <String , String > params ) {
232
- return createSign (params , getConfig ().getPartnerKey ());
233
- }
234
-
235
- @ Override
236
- public String createSign (Map <String , String > params , String signKey ) {
237
- SortedMap <String , String > sortedMap = new TreeMap <>(params );
238
-
239
- StringBuilder toSign = new StringBuilder ();
240
- for (String key : sortedMap .keySet ()) {
241
- String value = params .get (key );
242
- if (StringUtils .isNotEmpty (value ) && !"sign" .equals (key ) && !"key" .equals (key )) {
243
- toSign .append (key + "=" + value + "&" );
244
- }
245
- }
246
-
247
- toSign .append ("key=" + signKey );
248
- return DigestUtils .md5Hex (toSign .toString ()).toUpperCase ();
249
- }
250
-
251
- @ Override
252
- public boolean checkSign (Object xmlBean ) {
253
- return checkSign (BeanUtils .xmlBean2Map (xmlBean ), getConfig ().getPartnerKey ());
254
- }
255
-
256
- @ Override
257
- public boolean checkSign (Object xmlBean , String signKey ) {
258
- return checkSign (BeanUtils .xmlBean2Map (xmlBean ), signKey );
259
- }
260
-
261
- @ Override
262
- public boolean checkSign (Map <String , String > params ) {
263
- return checkSign (params , getConfig ().getPartnerKey ());
264
- }
265
-
266
- @ Override
267
- public boolean checkSign (Map <String , String > params , String signKey ) {
268
- String sign = this .createSign (params , signKey );
269
- return sign .equals (params .get ("sign" ));
270
- }
271
-
272
-
273
202
@ Override
274
203
public WxPayOrderQueryResult queryOrder (String transactionId , String outTradeNo ) throws WxErrorException {
275
204
if ((StringUtils .isBlank (transactionId ) && StringUtils .isBlank (outTradeNo )) ||
@@ -284,13 +213,10 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
284
213
WxPayOrderQueryRequest request = new WxPayOrderQueryRequest ();
285
214
request .setOutTradeNo (StringUtils .trimToNull (outTradeNo ));
286
215
request .setTransactionId (StringUtils .trimToNull (transactionId ));
287
- request .setAppid (getConfig ().getAppId ());
288
- request .setMchId (getConfig ().getPartnerId ());
289
- request .setNonceStr (System .currentTimeMillis () + "" );
216
+ initRequest (request );
290
217
request .setSign (this .createSign (request ));
291
218
292
219
String url = PAY_BASE_URL + "/pay/orderquery" ;
293
-
294
220
String responseContent = this .executeRequest (url , xstream .toXML (request ));
295
221
WxPayOrderQueryResult result = (WxPayOrderQueryResult ) xstream .fromXML (responseContent );
296
222
result .composeCoupons (responseContent );
@@ -310,13 +236,10 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
310
236
311
237
WxPayOrderCloseRequest request = new WxPayOrderCloseRequest ();
312
238
request .setOutTradeNo (StringUtils .trimToNull (outTradeNo ));
313
- request .setAppid (getConfig ().getAppId ());
314
- request .setMchId (getConfig ().getPartnerId ());
315
- request .setNonceStr (System .currentTimeMillis () + "" );
239
+ initRequest (request );
316
240
request .setSign (this .createSign (request ));
317
241
318
242
String url = PAY_BASE_URL + "/pay/closeorder" ;
319
-
320
243
String responseContent = this .executeRequest (url , xstream .toXML (request ));
321
244
WxPayOrderCloseResult result = (WxPayOrderCloseResult ) xstream .fromXML (responseContent );
322
245
this .checkResult (result );
@@ -331,16 +254,17 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
331
254
XStream xstream = XStreamInitializer .getInstance ();
332
255
xstream .processAnnotations (WxPayUnifiedOrderRequest .class );
333
256
xstream .processAnnotations (WxPayUnifiedOrderResult .class );
334
-
335
- request .setAppid (getConfig ().getAppId ());
336
- request .setMchId (getConfig ().getPartnerId ());
337
- request .setNotifyURL (getConfig ().getNotifyURL ());
338
- request .setTradeType (getConfig ().getTradeType ());
339
- request .setNonceStr (System .currentTimeMillis () + "" );
340
-
257
+
258
+ initRequest (request );
259
+ if (StringUtils .isBlank (request .getNotifyURL ())){
260
+ request .setNotifyURL (getConfig ().getNotifyURL ());
261
+ }
262
+ if (StringUtils .isBlank (request .getTradeType ())){
263
+ request .setTradeType (getConfig ().getTradeType ());
264
+ }
341
265
checkParameters (request );//校验参数
342
-
343
266
request .setSign (this .createSign (request ));
267
+
344
268
String url = PAY_BASE_URL + "/pay/unifiedorder" ;
345
269
String xmlParam = xstream .toXML (request );
346
270
log .debug ("微信统一下单接口,URL:{},参数:{}" , url , xmlParam );
@@ -351,6 +275,18 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
351
275
this .checkResult (result );
352
276
return result ;
353
277
}
278
+
279
+ private void initRequest (WxPayBaseRequest request ){
280
+ if (StringUtils .isBlank (request .getAppid ())){
281
+ request .setAppid (getConfig ().getAppId ());
282
+ }
283
+ if (StringUtils .isBlank (request .getMchId ())){
284
+ request .setMchId (getConfig ().getPartnerId ());
285
+ }
286
+ if (StringUtils .isBlank (request .getNonceStr ())){
287
+ request .setNonceStr (String .valueOf (System .currentTimeMillis ()));
288
+ }
289
+ }
354
290
355
291
private void checkParameters (WxPayUnifiedOrderRequest request ) throws WxErrorException {
356
292
BeanUtils .checkRequiredFields (request );
@@ -381,7 +317,7 @@ public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws W
381
317
payInfo .put ("appId" , getConfig ().getAppId ());
382
318
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
383
319
payInfo .put ("timeStamp" , String .valueOf (System .currentTimeMillis () / 1000 ));
384
- payInfo .put ("nonceStr" , System .currentTimeMillis () + "" );
320
+ payInfo .put ("nonceStr" , String . valueOf ( System .currentTimeMillis ()) );
385
321
payInfo .put ("package" , "prepay_id=" + prepayId );
386
322
payInfo .put ("signType" , "MD5" );
387
323
if ("NATIVE" .equals (request .getTradeType ())) {
@@ -393,15 +329,12 @@ public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws W
393
329
394
330
@ Override
395
331
public WxEntPayResult entPay (WxEntPayRequest request , File keyFile ) throws WxErrorException {
396
- BeanUtils .checkRequiredFields (request );
397
-
398
332
XStream xstream = XStreamInitializer .getInstance ();
399
333
xstream .processAnnotations (WxEntPayRequest .class );
400
334
xstream .processAnnotations (WxEntPayResult .class );
401
-
402
- request .setMchAppid (getConfig ().getAppId ());
403
- request .setMchId (getConfig ().getPartnerId ());
404
- request .setNonceStr (System .currentTimeMillis () + "" );
335
+
336
+ initRequest (request );
337
+ BeanUtils .checkRequiredFields (request );
405
338
request .setSign (this .createSign (request ));
406
339
407
340
String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers" ;
@@ -419,13 +352,10 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
419
352
xstream .processAnnotations (WxEntPayQueryResult .class );
420
353
421
354
WxEntPayQueryRequest request = new WxEntPayQueryRequest ();
422
- request .setAppid (getConfig ().getAppId ());
423
- request .setMchId (getConfig ().getPartnerId ());
424
- request .setNonceStr (System .currentTimeMillis () + "" );
355
+ initRequest (request );
425
356
request .setSign (this .createSign (request ));
426
357
427
358
String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo" ;
428
-
429
359
String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), request .getMchId ());
430
360
WxEntPayQueryResult result = (WxEntPayQueryResult ) xstream .fromXML (responseContent );
431
361
this .checkResult (result );
@@ -483,4 +413,56 @@ private String executeRequestWithKeyFile(String url, File keyFile, String reques
483
413
throw new WxErrorException (WxError .newBuilder ().setErrorCode (-1 ).setErrorMsg (e .getMessage ()).build (), e );
484
414
}
485
415
}
416
+
417
+ @ Override
418
+ public String createSign (Object xmlBean ) {
419
+ return createSign (BeanUtils .xmlBean2Map (xmlBean ), getConfig ().getPartnerKey ());
420
+ }
421
+
422
+ @ Override
423
+ public String createSign (Object xmlBean , String signKey ) {
424
+ return createSign (BeanUtils .xmlBean2Map (xmlBean ), signKey );
425
+ }
426
+
427
+ @ Override
428
+ public String createSign (Map <String , String > params ) {
429
+ return createSign (params , getConfig ().getPartnerKey ());
430
+ }
431
+
432
+ @ Override
433
+ public String createSign (Map <String , String > params , String signKey ) {
434
+ SortedMap <String , String > sortedMap = new TreeMap <>(params );
435
+
436
+ StringBuilder toSign = new StringBuilder ();
437
+ for (String key : sortedMap .keySet ()) {
438
+ String value = params .get (key );
439
+ if (StringUtils .isNotEmpty (value ) && !"sign" .equals (key ) && !"key" .equals (key )) {
440
+ toSign .append (key + "=" + value + "&" );
441
+ }
442
+ }
443
+
444
+ toSign .append ("key=" + signKey );
445
+ return DigestUtils .md5Hex (toSign .toString ()).toUpperCase ();
446
+ }
447
+
448
+ @ Override
449
+ public boolean checkSign (Object xmlBean ) {
450
+ return checkSign (BeanUtils .xmlBean2Map (xmlBean ), getConfig ().getPartnerKey ());
451
+ }
452
+
453
+ @ Override
454
+ public boolean checkSign (Object xmlBean , String signKey ) {
455
+ return checkSign (BeanUtils .xmlBean2Map (xmlBean ), signKey );
456
+ }
457
+
458
+ @ Override
459
+ public boolean checkSign (Map <String , String > params ) {
460
+ return checkSign (params , getConfig ().getPartnerKey ());
461
+ }
462
+
463
+ @ Override
464
+ public boolean checkSign (Map <String , String > params , String signKey ) {
465
+ String sign = this .createSign (params , signKey );
466
+ return sign .equals (params .get ("sign" ));
467
+ }
486
468
}
0 commit comments