@@ -46,64 +46,26 @@ public WxMpPayServiceImpl(WxMpService wxMpService) {
46
46
}
47
47
48
48
@ 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 ) {
88
50
try {
89
51
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 );
92
54
} catch (Exception e ) {
93
55
e .printStackTrace ();
94
56
}
95
57
96
- return new WxMpPayCallback ();
58
+ return new WxPayJsSDKCallback ();
97
59
}
98
60
99
61
@ Override
100
- public WxMpPayRefundResult refund (WxMpPayRefundRequest request , File keyFile )
62
+ public WxPayRefundResult refund (WxPayRefundRequest request , File keyFile )
101
63
throws WxErrorException {
102
64
checkParameters (request );
103
65
104
66
XStream xstream = XStreamInitializer .getInstance ();
105
- xstream .processAnnotations (WxMpPayRefundResult .class );
106
- xstream .processAnnotations (WxMpPayRefundRequest .class );
67
+ xstream .processAnnotations (WxPayRefundResult .class );
68
+ xstream .processAnnotations (WxPayRefundRequest .class );
107
69
108
70
request .setAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
109
71
String partnerId = this .wxMpService .getWxMpConfigStorage ().getPartnerId ();
@@ -115,7 +77,7 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
115
77
116
78
String url = PAY_BASE_URL + "/secapi/pay/refund" ;
117
79
String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), partnerId );
118
- WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult ) xstream .fromXML (responseContent );
80
+ WxPayRefundResult wxMpPayRefundResult = (WxPayRefundResult ) xstream .fromXML (responseContent );
119
81
120
82
if (!"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getResultCode ())
121
83
|| !"SUCCESS" .equalsIgnoreCase (wxMpPayRefundResult .getReturnCode ())) {
@@ -132,7 +94,7 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
132
94
return wxMpPayRefundResult ;
133
95
}
134
96
135
- private void checkParameters (WxMpPayRefundRequest request ) throws WxErrorException {
97
+ private void checkParameters (WxPayRefundRequest request ) throws WxErrorException {
136
98
BeanUtils .checkRequiredFields (request );
137
99
138
100
if (StringUtils .isNotBlank (request .getRefundAccount ())) {
@@ -154,11 +116,11 @@ public boolean checkJSSDKCallbackDataSignature(Map<String, String> kvm,
154
116
}
155
117
156
118
@ Override
157
- public WxRedpackResult sendRedpack (WxSendRedpackRequest request , File keyFile )
119
+ public WxPaySendRedpackResult sendRedpack (WxPaySendRedpackRequest request , File keyFile )
158
120
throws WxErrorException {
159
121
XStream xstream = XStreamInitializer .getInstance ();
160
- xstream .processAnnotations (WxSendRedpackRequest .class );
161
- xstream .processAnnotations (WxRedpackResult .class );
122
+ xstream .processAnnotations (WxPaySendRedpackRequest .class );
123
+ xstream .processAnnotations (WxPaySendRedpackResult .class );
162
124
163
125
request .setWxAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
164
126
String mchId = this .wxMpService .getWxMpConfigStorage ().getPartnerId ();
@@ -176,7 +138,7 @@ public WxRedpackResult sendRedpack(WxSendRedpackRequest request, File keyFile)
176
138
}
177
139
178
140
String responseContent = this .executeRequestWithKeyFile (url , keyFile , xstream .toXML (request ), mchId );
179
- WxRedpackResult redpackResult = (WxRedpackResult ) xstream
141
+ WxPaySendRedpackResult redpackResult = (WxPaySendRedpackResult ) xstream
180
142
.fromXML (responseContent );
181
143
if ("FAIL" .equals (redpackResult .getResultCode ())) {
182
144
throw new WxErrorException (WxError .newBuilder ()
@@ -212,13 +174,49 @@ private String createSign(Map<String, String> packageParams, String signKey) {
212
174
}
213
175
214
176
@ 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 )
216
214
throws WxErrorException {
217
215
checkParameters (request );
218
216
219
217
XStream xstream = XStreamInitializer .getInstance ();
220
- xstream .processAnnotations (WxUnifiedOrderRequest .class );
221
- xstream .processAnnotations (WxUnifiedOrderResult .class );
218
+ xstream .processAnnotations (WxPayUnifiedOrderRequest .class );
219
+ xstream .processAnnotations (WxPayUnifiedOrderResult .class );
222
220
223
221
request .setAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
224
222
request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
@@ -231,7 +229,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
231
229
String url = PAY_BASE_URL + "/pay/unifiedorder" ;
232
230
233
231
String responseContent = this .wxMpService .post (url , xstream .toXML (request ));
234
- WxUnifiedOrderResult result = (WxUnifiedOrderResult ) xstream
232
+ WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult ) xstream
235
233
.fromXML (responseContent );
236
234
if ("FAIL" .equals (result .getResultCode ())) {
237
235
throw new WxErrorException (WxError .newBuilder ()
@@ -242,7 +240,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
242
240
return result ;
243
241
}
244
242
245
- private void checkParameters (WxUnifiedOrderRequest request ) throws WxErrorException {
243
+ private void checkParameters (WxPayUnifiedOrderRequest request ) throws WxErrorException {
246
244
BeanUtils .checkRequiredFields (request );
247
245
248
246
if (! ArrayUtils .contains (TRADE_TYPES , request .getTradeType ())) {
@@ -259,8 +257,8 @@ private void checkParameters(WxUnifiedOrderRequest request) throws WxErrorExcept
259
257
}
260
258
261
259
@ 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 );
264
262
265
263
if (!"SUCCESS" .equalsIgnoreCase (unifiedOrderResult .getReturnCode ())
266
264
|| !"SUCCESS" .equalsIgnoreCase (unifiedOrderResult .getResultCode ())) {
0 commit comments