1
1
package me .chanjar .weixin .mp .api .impl ;
2
2
3
- import com .google .common .collect .Lists ;
4
- import com .google .common .collect .Maps ;
5
3
import com .thoughtworks .xstream .XStream ;
6
- import com .thoughtworks .xstream .annotations .XStreamAlias ;
7
- import me .chanjar .weixin .common .annotation .Required ;
8
4
import me .chanjar .weixin .common .bean .result .WxError ;
9
5
import me .chanjar .weixin .common .exception .WxErrorException ;
6
+ import me .chanjar .weixin .common .util .BeanUtils ;
10
7
import me .chanjar .weixin .common .util .xml .XStreamInitializer ;
11
8
import me .chanjar .weixin .mp .api .WxMpPayService ;
12
9
import me .chanjar .weixin .mp .api .WxMpService ;
23
20
import org .apache .http .impl .client .HttpClients ;
24
21
import org .apache .http .ssl .SSLContexts ;
25
22
import org .apache .http .util .EntityUtils ;
26
- import org .joor .Reflect ;
27
23
28
24
import javax .net .ssl .SSLContext ;
29
25
import java .io .File ;
30
26
import java .io .FileInputStream ;
31
- import java .lang .reflect .Field ;
32
27
import java .security .KeyStore ;
33
28
import java .util .*;
34
- import java .util .Map .Entry ;
35
29
36
30
/**
37
31
* Created by Binary Wang on 2016/7/28.
@@ -116,7 +110,7 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
116
110
request .setMchId (partnerId );
117
111
request .setNonceStr ( System .currentTimeMillis () + "" );
118
112
request .setOpUserId (partnerId );
119
- String sign = this .createSign (this .xmlBean2Map (request ), this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
113
+ String sign = this .createSign (BeanUtils .xmlBean2Map (request ), this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
120
114
request .setSign (sign );
121
115
122
116
String url = PAY_BASE_URL + "/secapi/pay/refund" ;
@@ -138,8 +132,8 @@ public WxMpPayRefundResult refund(WxMpPayRefundRequest request, File keyFile)
138
132
return wxMpPayRefundResult ;
139
133
}
140
134
141
- private void checkParameters (WxMpPayRefundRequest request ) {
142
- checkNotNullParams (request );
135
+ private void checkParameters (WxMpPayRefundRequest request ) throws WxErrorException {
136
+ BeanUtils . checkRequiredFields (request );
143
137
144
138
if (StringUtils .isNotBlank (request .getRefundAccount ())) {
145
139
if (!ArrayUtils .contains (REFUND_ACCOUNT , request .getRefundAccount ())){
@@ -171,7 +165,7 @@ public WxRedpackResult sendRedpack(WxSendRedpackRequest request, File keyFile)
171
165
request .setMchId (mchId );
172
166
request .setNonceStr (System .currentTimeMillis () + "" );
173
167
174
- String sign = this .createSign (this .xmlBean2Map (request ),
168
+ String sign = this .createSign (BeanUtils .xmlBean2Map (request ),
175
169
this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
176
170
request .setSign (sign );
177
171
@@ -194,29 +188,6 @@ public WxRedpackResult sendRedpack(WxSendRedpackRequest request, File keyFile)
194
188
return redpackResult ;
195
189
}
196
190
197
- private Map <String , String > xmlBean2Map (Object bean ) {
198
- Map <String , String > result = Maps .newHashMap ();
199
- for (Entry <String , Reflect > entry : Reflect .on (bean ).fields ().entrySet ()) {
200
- Reflect reflect = entry .getValue ();
201
- if (reflect .get () == null ) {
202
- continue ;
203
- }
204
-
205
- try {
206
- Field field = bean .getClass ().getDeclaredField (entry .getKey ());
207
- if (field .isAnnotationPresent (XStreamAlias .class )) {
208
- result .put (field .getAnnotation (XStreamAlias .class ).value (),
209
- reflect .get ().toString ());
210
- }
211
- } catch (NoSuchFieldException | SecurityException e ) {
212
- e .printStackTrace ();
213
- }
214
-
215
- }
216
-
217
- return result ;
218
- }
219
-
220
191
/**
221
192
* 微信公众号支付签名算法(详见:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=4_3)
222
193
* @param packageParams 原始参数
@@ -253,7 +224,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
253
224
request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
254
225
request .setNonceStr (System .currentTimeMillis () + "" );
255
226
256
- String sign = this .createSign (this .xmlBean2Map (request ),
227
+ String sign = this .createSign (BeanUtils .xmlBean2Map (request ),
257
228
this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
258
229
request .setSign (sign );
259
230
@@ -271,8 +242,8 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
271
242
return result ;
272
243
}
273
244
274
- private void checkParameters (WxUnifiedOrderRequest request ) {
275
- checkNotNullParams (request );
245
+ private void checkParameters (WxUnifiedOrderRequest request ) throws WxErrorException {
246
+ BeanUtils . checkRequiredFields (request );
276
247
277
248
if (! ArrayUtils .contains (TRADE_TYPES , request .getTradeType ())) {
278
249
throw new IllegalArgumentException ("trade_type目前必须为" + Arrays .toString (TRADE_TYPES ) + "其中之一" );
@@ -287,27 +258,6 @@ private void checkParameters(WxUnifiedOrderRequest request) {
287
258
}
288
259
}
289
260
290
- private void checkNotNullParams (Object request ) {
291
- List <String > nullFields = Lists .newArrayList ();
292
- for (Entry <String , Reflect > entry : Reflect .on (request ).fields ()
293
- .entrySet ()) {
294
- Reflect reflect = entry .getValue ();
295
- try {
296
- Field field = request .getClass ().getDeclaredField (entry .getKey ());
297
- if (field .isAnnotationPresent (Required .class )
298
- && reflect .get () == null ) {
299
- nullFields .add (entry .getKey ());
300
- }
301
- } catch (NoSuchFieldException | SecurityException e ) {
302
- e .printStackTrace ();
303
- }
304
- }
305
-
306
- if (!nullFields .isEmpty ()) {
307
- throw new IllegalArgumentException ("必填字段[" + nullFields + "]必须提供值" );
308
- }
309
- }
310
-
311
261
@ Override
312
262
public Map <String , String > getPayInfo (WxUnifiedOrderRequest request ) throws WxErrorException {
313
263
WxUnifiedOrderResult unifiedOrderResult = this .unifiedOrder (request );
@@ -345,7 +295,7 @@ public Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxEr
345
295
346
296
@ Override
347
297
public WxEntPayResult entPay (WxEntPayRequest request , File keyFile ) throws WxErrorException {
348
- checkNotNullParams (request );
298
+ BeanUtils . checkRequiredFields (request );
349
299
350
300
XStream xstream = XStreamInitializer .getInstance ();
351
301
xstream .processAnnotations (WxEntPayRequest .class );
@@ -355,7 +305,7 @@ public WxEntPayResult entPay(WxEntPayRequest request, File keyFile) throws WxErr
355
305
request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
356
306
request .setNonceStr (System .currentTimeMillis () + "" );
357
307
358
- String sign = this .createSign (xmlBean2Map (request ), this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
308
+ String sign = this .createSign (BeanUtils . xmlBean2Map (request ), this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
359
309
request .setSign (sign );
360
310
361
311
String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers" ;
@@ -380,7 +330,7 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
380
330
request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
381
331
request .setNonceStr (System .currentTimeMillis () + "" );
382
332
383
- String sign = this .createSign (xmlBean2Map (request ), this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
333
+ String sign = this .createSign (BeanUtils . xmlBean2Map (request ), this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
384
334
request .setSign (sign );
385
335
386
336
String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo" ;
0 commit comments