1
1
package me .chanjar .weixin .mp .api .impl ;
2
2
3
- import java .io .IOException ;
4
3
import java .lang .reflect .Field ;
5
4
import java .util .HashMap ;
6
5
import java .util .List ;
10
9
import java .util .TreeMap ;
11
10
12
11
import org .apache .commons .codec .digest .DigestUtils ;
13
- import org .apache .http .Consts ;
14
- import org .apache .http .HttpHost ;
15
- import org .apache .http .client .config .RequestConfig ;
16
- import org .apache .http .client .methods .CloseableHttpResponse ;
17
- import org .apache .http .client .methods .HttpPost ;
18
- import org .apache .http .entity .StringEntity ;
19
12
import org .joor .Reflect ;
20
13
import org .slf4j .Logger ;
21
14
import org .slf4j .LoggerFactory ;
22
- import org .slf4j .helpers .MessageFormatter ;
23
15
24
16
import com .google .common .collect .Lists ;
25
17
import com .google .common .collect .Maps ;
29
21
import me .chanjar .weixin .common .annotation .Required ;
30
22
import me .chanjar .weixin .common .bean .result .WxError ;
31
23
import me .chanjar .weixin .common .exception .WxErrorException ;
32
- import me .chanjar .weixin .common .util .http .Utf8ResponseHandler ;
33
24
import me .chanjar .weixin .common .util .xml .XStreamInitializer ;
34
25
import me .chanjar .weixin .mp .api .WxMpPayService ;
26
+ import me .chanjar .weixin .mp .api .WxMpService ;
35
27
import me .chanjar .weixin .mp .bean .pay .WxMpPayCallback ;
36
28
import me .chanjar .weixin .mp .bean .pay .WxMpPayRefundResult ;
37
29
import me .chanjar .weixin .mp .bean .pay .WxMpPayResult ;
@@ -54,19 +46,17 @@ public class WxMpPayServiceImpl implements WxMpPayService {
54
46
private final String [] REQUIRED_ORDER_PARAMETERS = new String [] { "appid" ,
55
47
"mch_id" , "body" , "out_trade_no" , "total_fee" , "spbill_create_ip" ,
56
48
"notify_url" , "trade_type" };
57
- private HttpHost httpProxy ;
58
- private WxMpServiceImpl wxMpService ;
49
+ private WxMpService wxMpService ;
59
50
60
- public WxMpPayServiceImpl (WxMpServiceImpl wxMpService ) {
51
+ public WxMpPayServiceImpl (WxMpService wxMpService ) {
61
52
this .wxMpService = wxMpService ;
62
- this .httpProxy = wxMpService .getHttpProxy ();
63
53
}
64
54
65
55
@ Override
66
56
@ Deprecated
67
57
public WxMpPrepayIdResult getPrepayId (String openId , String outTradeNo ,
68
58
double amt , String body , String tradeType , String ip ,
69
- String callbackUrl ) {
59
+ String callbackUrl ) throws WxErrorException {
70
60
Map <String , String > packageParams = new HashMap <>();
71
61
packageParams .put ("appid" ,
72
62
this .wxMpService .getWxMpConfigStorage ().getAppId ());
@@ -85,7 +75,8 @@ public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo,
85
75
86
76
@ Override
87
77
@ Deprecated
88
- public WxMpPrepayIdResult getPrepayId (final Map <String , String > parameters ) {
78
+ public WxMpPrepayIdResult getPrepayId (final Map <String , String > parameters )
79
+ throws WxErrorException {
89
80
final SortedMap <String , String > packageParams = new TreeMap <>(parameters );
90
81
packageParams .put ("appid" ,
91
82
this .wxMpService .getWxMpConfigStorage ().getAppId ());
@@ -106,29 +97,11 @@ public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
106
97
107
98
request .append ("</xml>" );
108
99
109
- HttpPost httpPost = new HttpPost (
110
- "https://api.mch.weixin.qq.com/pay/unifiedorder" );
111
- if (this .httpProxy != null ) {
112
- RequestConfig config = RequestConfig .custom ().setProxy (this .httpProxy )
113
- .build ();
114
- httpPost .setConfig (config );
115
- }
116
-
117
- StringEntity entity = new StringEntity (request .toString (), Consts .UTF_8 );
118
- httpPost .setEntity (entity );
119
- try (CloseableHttpResponse response = this .wxMpService .getHttpclient ()
120
- .execute (httpPost )) {
121
- String responseContent = Utf8ResponseHandler .INSTANCE
122
- .handleResponse (response );
123
- XStream xstream = XStreamInitializer .getInstance ();
124
- xstream .alias ("xml" , WxMpPrepayIdResult .class );
125
- return (WxMpPrepayIdResult ) xstream .fromXML (responseContent );
126
- } catch (IOException e ) {
127
- throw new RuntimeException ("Failed to get prepay id due to IO exception." ,
128
- e );
129
- } finally {
130
- httpPost .releaseConnection ();
131
- }
100
+ String url = "https://api.mch.weixin.qq.com/pay/unifiedorder" ;
101
+ String responseContent = this .wxMpService .post (url , request .toString ());
102
+ XStream xstream = XStreamInitializer .getInstance ();
103
+ xstream .alias ("xml" , WxMpPrepayIdResult .class );
104
+ return (WxMpPrepayIdResult ) xstream .fromXML (responseContent );
132
105
}
133
106
134
107
private void checkParameters (Map <String , String > parameters ) {
@@ -238,7 +211,7 @@ public Map<String, String> getPayInfo(Map<String, String> parameters)
238
211
239
212
@ Override
240
213
public WxMpPayResult getJSSDKPayResult (String transactionId ,
241
- String outTradeNo ) {
214
+ String outTradeNo ) throws WxErrorException {
242
215
String nonce_str = System .currentTimeMillis () + "" ;
243
216
244
217
SortedMap <String , String > packageParams = new TreeMap <>();
@@ -267,27 +240,11 @@ public WxMpPayResult getJSSDKPayResult(String transactionId,
267
240
}
268
241
request .append ("</xml>" );
269
242
270
- HttpPost httpPost = new HttpPost (
271
- "https://api.mch.weixin.qq.com/pay/orderquery" );
272
- if (this .httpProxy != null ) {
273
- RequestConfig config = RequestConfig .custom ().setProxy (this .httpProxy )
274
- .build ();
275
- httpPost .setConfig (config );
276
- }
277
-
278
- StringEntity entity = new StringEntity (request .toString (), Consts .UTF_8 );
279
- httpPost .setEntity (entity );
280
- try (CloseableHttpResponse response = this .wxMpService .getHttpclient ()
281
- .execute (httpPost )) {
282
- String responseContent = Utf8ResponseHandler .INSTANCE
283
- .handleResponse (response );
284
- XStream xstream = XStreamInitializer .getInstance ();
285
- xstream .alias ("xml" , WxMpPayResult .class );
286
- return (WxMpPayResult ) xstream .fromXML (responseContent );
287
- } catch (IOException e ) {
288
- throw new RuntimeException ("Failed to query order due to IO exception." ,
289
- e );
290
- }
243
+ String url = "https://api.mch.weixin.qq.com/pay/orderquery" ;
244
+ String responseContent = this .wxMpService .post (url , request .toString ());
245
+ XStream xstream = XStreamInitializer .getInstance ();
246
+ xstream .alias ("xml" , WxMpPayResult .class );
247
+ return (WxMpPayResult ) xstream .fromXML (responseContent );
291
248
}
292
249
293
250
@ Override
@@ -325,49 +282,26 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
325
282
}
326
283
request .append ("</xml>" );
327
284
328
- HttpPost httpPost = new HttpPost (
329
- "https://api.mch.weixin.qq.com/secapi/pay/refund" );
330
- if (this .httpProxy != null ) {
331
- RequestConfig config = RequestConfig .custom ().setProxy (this .httpProxy )
332
- .build ();
333
- httpPost .setConfig (config );
334
- }
335
-
336
- StringEntity entity = new StringEntity (request .toString (), Consts .UTF_8 );
337
- httpPost .setEntity (entity );
338
- try (CloseableHttpResponse response = this .wxMpService .getHttpclient ()
339
- .execute (httpPost )) {
340
- String responseContent = Utf8ResponseHandler .INSTANCE
341
- .handleResponse (response );
342
- XStream xstream = XStreamInitializer .getInstance ();
343
- xstream .processAnnotations (WxMpPayRefundResult .class );
344
- WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult ) xstream
345
- .fromXML (responseContent );
346
-
347
- if (!"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getResultCode ())
348
- || !"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getReturnCode ())) {
349
- WxError error = new WxError ();
350
- error .setErrorCode (-1 );
351
- error .setErrorMsg ("return_code:" + wxMpPayRefundResult .getReturnCode ()
352
- + ";return_msg:" + wxMpPayRefundResult .getReturnMsg ()
353
- + ";result_code:" + wxMpPayRefundResult .getResultCode ()
354
- + ";err_code" + wxMpPayRefundResult .getErrCode () + ";err_code_des"
355
- + wxMpPayRefundResult .getErrCodeDes ());
356
- throw new WxErrorException (error );
357
- }
285
+ String url = "https://api.mch.weixin.qq.com/secapi/pay/refund" ;
286
+ String responseContent = this .wxMpService .post (url , request .toString ());
287
+ XStream xstream = XStreamInitializer .getInstance ();
288
+ xstream .processAnnotations (WxMpPayRefundResult .class );
289
+ WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult ) xstream
290
+ .fromXML (responseContent );
358
291
359
- return wxMpPayRefundResult ;
360
- } catch (IOException e ) {
361
- String message = MessageFormatter
362
- .format ("Exception happened when sending refund '{}'." ,
363
- request .toString ())
364
- .getMessage ();
365
- this .log .error (message , e );
366
- throw new WxErrorException (
367
- WxError .newBuilder ().setErrorMsg (message ).build ());
368
- } finally {
369
- httpPost .releaseConnection ();
292
+ if (!"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getResultCode ())
293
+ || !"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getReturnCode ())) {
294
+ WxError error = new WxError ();
295
+ error .setErrorCode (-1 );
296
+ error .setErrorMsg ("return_code:" + wxMpPayRefundResult .getReturnCode ()
297
+ + ";return_msg:" + wxMpPayRefundResult .getReturnMsg ()
298
+ + ";result_code:" + wxMpPayRefundResult .getResultCode () + ";err_code"
299
+ + wxMpPayRefundResult .getErrCode () + ";err_code_des"
300
+ + wxMpPayRefundResult .getErrCodeDes ());
301
+ throw new WxErrorException (error );
370
302
}
303
+
304
+ return wxMpPayRefundResult ;
371
305
}
372
306
373
307
@ Override
@@ -400,34 +334,12 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
400
334
401
335
request .append ("</xml>" );
402
336
403
- HttpPost httpPost = new HttpPost (
404
- "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" );
405
- if (this .httpProxy != null ) {
406
- RequestConfig config = RequestConfig .custom ().setProxy (this .httpProxy )
407
- .build ();
408
- httpPost .setConfig (config );
409
- }
337
+ String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" ;
410
338
411
- StringEntity entity = new StringEntity (request .toString (), Consts .UTF_8 );
412
- httpPost .setEntity (entity );
413
- try (CloseableHttpResponse response = this .wxMpService .getHttpclient ()
414
- .execute (httpPost )) {
415
- String responseContent = Utf8ResponseHandler .INSTANCE
416
- .handleResponse (response );
417
- XStream xstream = XStreamInitializer .getInstance ();
418
- xstream .processAnnotations (WxRedpackResult .class );
419
- return (WxRedpackResult ) xstream .fromXML (responseContent );
420
- } catch (IOException e ) {
421
- String message = MessageFormatter
422
- .format ("Exception occured when sending redpack '{}'." ,
423
- request .toString ())
424
- .getMessage ();
425
- this .log .error (message , e );
426
- throw new WxErrorException (
427
- WxError .newBuilder ().setErrorMsg (message ).build ());
428
- } finally {
429
- httpPost .releaseConnection ();
430
- }
339
+ String responseContent = this .wxMpService .post (url , request .toString ());
340
+ XStream xstream = XStreamInitializer .getInstance ();
341
+ xstream .processAnnotations (WxRedpackResult .class );
342
+ return (WxRedpackResult ) xstream .fromXML (responseContent );
431
343
}
432
344
433
345
@ Override
@@ -544,7 +456,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
544
456
545
457
private void checkParameters (WxUnifiedOrderRequest request ) {
546
458
547
- List <String > nullFields = com . google . common . collect . Lists .newArrayList ();
459
+ List <String > nullFields = Lists .newArrayList ();
548
460
for (Entry <String , Reflect > entry : Reflect .on (request ).fields ()
549
461
.entrySet ()) {
550
462
Reflect reflect = entry .getValue ();
0 commit comments