11package me .chanjar .weixin .mp .api .impl ;
22
3- import java .io .IOException ;
43import java .lang .reflect .Field ;
54import java .util .HashMap ;
65import java .util .List ;
109import java .util .TreeMap ;
1110
1211import 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 ;
1912import org .joor .Reflect ;
2013import org .slf4j .Logger ;
2114import org .slf4j .LoggerFactory ;
22- import org .slf4j .helpers .MessageFormatter ;
2315
2416import com .google .common .collect .Lists ;
2517import com .google .common .collect .Maps ;
2921import me .chanjar .weixin .common .annotation .Required ;
3022import me .chanjar .weixin .common .bean .result .WxError ;
3123import me .chanjar .weixin .common .exception .WxErrorException ;
32- import me .chanjar .weixin .common .util .http .Utf8ResponseHandler ;
3324import me .chanjar .weixin .common .util .xml .XStreamInitializer ;
3425import me .chanjar .weixin .mp .api .WxMpPayService ;
26+ import me .chanjar .weixin .mp .api .WxMpService ;
3527import me .chanjar .weixin .mp .bean .pay .WxMpPayCallback ;
3628import me .chanjar .weixin .mp .bean .pay .WxMpPayRefundResult ;
3729import me .chanjar .weixin .mp .bean .pay .WxMpPayResult ;
@@ -54,19 +46,17 @@ public class WxMpPayServiceImpl implements WxMpPayService {
5446 private final String [] REQUIRED_ORDER_PARAMETERS = new String [] { "appid" ,
5547 "mch_id" , "body" , "out_trade_no" , "total_fee" , "spbill_create_ip" ,
5648 "notify_url" , "trade_type" };
57- private HttpHost httpProxy ;
58- private WxMpServiceImpl wxMpService ;
49+ private WxMpService wxMpService ;
5950
60- public WxMpPayServiceImpl (WxMpServiceImpl wxMpService ) {
51+ public WxMpPayServiceImpl (WxMpService wxMpService ) {
6152 this .wxMpService = wxMpService ;
62- this .httpProxy = wxMpService .getHttpProxy ();
6353 }
6454
6555 @ Override
6656 @ Deprecated
6757 public WxMpPrepayIdResult getPrepayId (String openId , String outTradeNo ,
6858 double amt , String body , String tradeType , String ip ,
69- String callbackUrl ) {
59+ String callbackUrl ) throws WxErrorException {
7060 Map <String , String > packageParams = new HashMap <>();
7161 packageParams .put ("appid" ,
7262 this .wxMpService .getWxMpConfigStorage ().getAppId ());
@@ -85,7 +75,8 @@ public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo,
8575
8676 @ Override
8777 @ Deprecated
88- public WxMpPrepayIdResult getPrepayId (final Map <String , String > parameters ) {
78+ public WxMpPrepayIdResult getPrepayId (final Map <String , String > parameters )
79+ throws WxErrorException {
8980 final SortedMap <String , String > packageParams = new TreeMap <>(parameters );
9081 packageParams .put ("appid" ,
9182 this .wxMpService .getWxMpConfigStorage ().getAppId ());
@@ -106,29 +97,11 @@ public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
10697
10798 request .append ("</xml>" );
10899
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 );
132105 }
133106
134107 private void checkParameters (Map <String , String > parameters ) {
@@ -238,7 +211,7 @@ public Map<String, String> getPayInfo(Map<String, String> parameters)
238211
239212 @ Override
240213 public WxMpPayResult getJSSDKPayResult (String transactionId ,
241- String outTradeNo ) {
214+ String outTradeNo ) throws WxErrorException {
242215 String nonce_str = System .currentTimeMillis () + "" ;
243216
244217 SortedMap <String , String > packageParams = new TreeMap <>();
@@ -267,27 +240,11 @@ public WxMpPayResult getJSSDKPayResult(String transactionId,
267240 }
268241 request .append ("</xml>" );
269242
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 );
291248 }
292249
293250 @ Override
@@ -325,49 +282,26 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
325282 }
326283 request .append ("</xml>" );
327284
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 );
358291
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 );
370302 }
303+
304+ return wxMpPayRefundResult ;
371305 }
372306
373307 @ Override
@@ -400,34 +334,12 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
400334
401335 request .append ("</xml>" );
402336
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" ;
410338
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 );
431343 }
432344
433345 @ Override
@@ -544,7 +456,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
544456
545457 private void checkParameters (WxUnifiedOrderRequest request ) {
546458
547- List <String > nullFields = com . google . common . collect . Lists .newArrayList ();
459+ List <String > nullFields = Lists .newArrayList ();
548460 for (Entry <String , Reflect > entry : Reflect .on (request ).fields ()
549461 .entrySet ()) {
550462 Reflect reflect = entry .getValue ();
0 commit comments