@@ -46,64 +46,26 @@ public WxMpPayServiceImpl(WxMpService wxMpService) {
4646 }
4747
4848 @ Override
49- public WxMpPayResult getJSSDKPayResult (String transactionId ,
50- String outTradeNo ) throws WxErrorException {
51- String nonce_str = System .currentTimeMillis () + "" ;
52-
53- SortedMap <String , String > packageParams = new TreeMap <>();
54- packageParams .put ("appid" ,
55- this .wxMpService .getWxMpConfigStorage ().getAppId ());
56- packageParams .put ("mch_id" ,
57- this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
58-
59- if (transactionId != null && !"" .equals (transactionId .trim ())) {
60- packageParams .put ("transaction_id" , transactionId );
61- } else if (outTradeNo != null && !"" .equals (outTradeNo .trim ())) {
62- packageParams .put ("out_trade_no" , outTradeNo );
63- } else {
64- throw new IllegalArgumentException (
65- "Either 'transactionId' or 'outTradeNo' must be given." );
66- }
67-
68- packageParams .put ("nonce_str" , nonce_str );
69- packageParams .put ("sign" , this .createSign (packageParams ,
70- this .wxMpService .getWxMpConfigStorage ().getPartnerKey ()));
71-
72- StringBuilder request = new StringBuilder ("<xml>" );
73- for (Map .Entry <String , String > para : packageParams .entrySet ()) {
74- request .append (String .format ("<%s>%s</%s>" , para .getKey (),
75- para .getValue (), para .getKey ()));
76- }
77- request .append ("</xml>" );
78-
79- String url = PAY_BASE_URL + "/pay/orderquery" ;
80- String responseContent = this .wxMpService .post (url , request .toString ());
81- XStream xstream = XStreamInitializer .getInstance ();
82- xstream .alias ("xml" , WxMpPayResult .class );
83- return (WxMpPayResult ) xstream .fromXML (responseContent );
84- }
85-
86- @ Override
87- public WxMpPayCallback getJSSDKCallbackData (String xmlData ) {
49+ public WxPayJsSDKCallback getJSSDKCallbackData (String xmlData ) {
8850 try {
8951 XStream xstream = XStreamInitializer .getInstance ();
90- xstream .alias ("xml" , WxMpPayCallback .class );
91- return (WxMpPayCallback ) xstream .fromXML (xmlData );
52+ xstream .alias ("xml" , WxPayJsSDKCallback .class );
53+ return (WxPayJsSDKCallback ) xstream .fromXML (xmlData );
9254 } catch (Exception e ) {
9355 e .printStackTrace ();
9456 }
9557
96- return new WxMpPayCallback ();
58+ return new WxPayJsSDKCallback ();
9759 }
9860
9961 @ Override
100- public WxMpPayRefundResult refund (WxMpPayRefundRequest request , File keyFile )
62+ public WxPayRefundResult refund (WxPayRefundRequest request , File keyFile )
10163 throws WxErrorException {
10264 checkParameters (request );
10365
10466 XStream xstream = XStreamInitializer .getInstance ();
105- xstream .processAnnotations (WxMpPayRefundResult .class );
106- xstream .processAnnotations (WxMpPayRefundRequest .class );
67+ xstream .processAnnotations (WxPayRefundResult .class );
68+ xstream .processAnnotations (WxPayRefundRequest .class );
10769
10870 request .setAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
10971 String partnerId = this .wxMpService .getWxMpConfigStorage ().getPartnerId ();
@@ -115,7 +77,7 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
11577
11678 String url = PAY_BASE_URL + "/secapi/pay/refund" ;
11779 String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), partnerId );
118- WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult ) xstream .fromXML (responseContent );
80+ WxPayRefundResult wxMpPayRefundResult = (WxPayRefundResult ) xstream .fromXML (responseContent );
11981
12082 if (!"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getResultCode ())
12183 || !"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getReturnCode ())) {
@@ -132,7 +94,7 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
13294 return wxMpPayRefundResult ;
13395 }
13496
135- private void checkParameters (WxMpPayRefundRequest request ) throws WxErrorException {
97+ private void checkParameters (WxPayRefundRequest request ) throws WxErrorException {
13698 BeanUtils .checkRequiredFields (request );
13799
138100 if (StringUtils .isNotBlank (request .getRefundAccount ())) {
@@ -154,11 +116,11 @@ public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm,
154116 }
155117
156118 @ Override
157- public WxRedpackResult sendRedpack (WxSendRedpackRequest request , File keyFile )
119+ public WxPaySendRedpackResult sendRedpack (WxPaySendRedpackRequest request , File keyFile )
158120 throws WxErrorException {
159121 XStream xstream = XStreamInitializer .getInstance ();
160- xstream .processAnnotations (WxSendRedpackRequest .class );
161- xstream .processAnnotations (WxRedpackResult .class );
122+ xstream .processAnnotations (WxPaySendRedpackRequest .class );
123+ xstream .processAnnotations (WxPaySendRedpackResult .class );
162124
163125 request .setWxAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
164126 String mchId = this .wxMpService .getWxMpConfigStorage ().getPartnerId ();
@@ -176,7 +138,7 @@ public WxRedpackResult sendRedpack(WxSendRedpackRequest request, File keyFile)
176138 }
177139
178140 String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), mchId );
179- WxRedpackResult redpackResult = (WxRedpackResult ) xstream
141+ WxPaySendRedpackResult redpackResult = (WxPaySendRedpackResult ) xstream
180142 .fromXML (responseContent );
181143 if ("FAIL" .equals (redpackResult .getResultCode ())) {
182144 throw new WxErrorException (WxError .newBuilder ()
@@ -212,13 +174,49 @@ private String createSign(Map<String, String> packageParams, String signKey) {
212174 }
213175
214176 @ Override
215- public WxUnifiedOrderResult unifiedOrder (WxUnifiedOrderRequest request )
177+ public WxPayOrderQueryResult queryOrder (String transactionId , String outTradeNo ) throws WxErrorException {
178+ if ((StringUtils .isBlank (transactionId ) && StringUtils .isBlank (outTradeNo )) ||
179+ (StringUtils .isNotBlank (transactionId ) && StringUtils .isNotBlank (outTradeNo ))) {
180+ throw new IllegalArgumentException ("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一" );
181+ }
182+
183+ XStream xstream = XStreamInitializer .getInstance ();
184+ xstream .processAnnotations (WxPayOrderQueryRequest .class );
185+ xstream .processAnnotations (WxPayOrderQueryResult .class );
186+
187+ WxPayOrderQueryRequest request = new WxPayOrderQueryRequest ();
188+ request .setOutTradeNo (StringUtils .trimToNull (outTradeNo ));
189+ request .setTransactionId (StringUtils .trimToNull (transactionId ));
190+ request .setAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
191+ request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
192+ request .setNonceStr (System .currentTimeMillis () + "" );
193+
194+ String sign = this .createSign (BeanUtils .xmlBean2Map (request ),
195+ this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
196+ request .setSign (sign );
197+
198+ String url = PAY_BASE_URL + "/pay/orderquery" ;
199+
200+ String responseContent = this .wxMpService .post (url , xstream .toXML (request ));
201+ WxPayOrderQueryResult result = (WxPayOrderQueryResult ) xstream .fromXML (responseContent );
202+ result .composeCoupons (responseContent );
203+ if ("FAIL" .equals (result .getResultCode ())) {
204+ throw new WxErrorException (WxError .newBuilder ()
205+ .setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ())
206+ .build ());
207+ }
208+
209+ return result ;
210+ }
211+
212+ @ Override
213+ public WxPayUnifiedOrderResult unifiedOrder (WxPayUnifiedOrderRequest request )
216214 throws WxErrorException {
217215 checkParameters (request );
218216
219217 XStream xstream = XStreamInitializer .getInstance ();
220- xstream .processAnnotations (WxUnifiedOrderRequest .class );
221- xstream .processAnnotations (WxUnifiedOrderResult .class );
218+ xstream .processAnnotations (WxPayUnifiedOrderRequest .class );
219+ xstream .processAnnotations (WxPayUnifiedOrderResult .class );
222220
223221 request .setAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
224222 request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
@@ -231,7 +229,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
231229 String url = PAY_BASE_URL + "/pay/unifiedorder" ;
232230
233231 String responseContent = this .wxMpService .post (url , xstream .toXML (request ));
234- WxUnifiedOrderResult result = (WxUnifiedOrderResult ) xstream
232+ WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult ) xstream
235233 .fromXML (responseContent );
236234 if ("FAIL" .equals (result .getResultCode ())) {
237235 throw new WxErrorException (WxError .newBuilder ()
@@ -242,7 +240,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
242240 return result ;
243241 }
244242
245- private void checkParameters (WxUnifiedOrderRequest request ) throws WxErrorException {
243+ private void checkParameters (WxPayUnifiedOrderRequest request ) throws WxErrorException {
246244 BeanUtils .checkRequiredFields (request );
247245
248246 if (! ArrayUtils .contains (TRADE_TYPES , request .getTradeType ())) {
@@ -259,8 +257,8 @@ private void checkParameters(WxUnifiedOrderRequest request) throws WxErrorExcept
259257 }
260258
261259 @ Override
262- public Map <String , String > getPayInfo (WxUnifiedOrderRequest request ) throws WxErrorException {
263- WxUnifiedOrderResult unifiedOrderResult = this .unifiedOrder (request );
260+ public Map <String , String > getPayInfo (WxPayUnifiedOrderRequest request ) throws WxErrorException {
261+ WxPayUnifiedOrderResult unifiedOrderResult = this .unifiedOrder (request );
264262
265263 if (!"SUCCESS" .equalsIgnoreCase (unifiedOrderResult .getReturnCode ())
266264 || !"SUCCESS" .equalsIgnoreCase (unifiedOrderResult .getResultCode ())) {
0 commit comments