11package me .chanjar .weixin .mp .api .impl ;
22
3+ import java .io .IOException ;
4+ import java .lang .reflect .Field ;
5+ import java .util .HashMap ;
6+ import java .util .Map ;
7+ import java .util .Map .Entry ;
8+ import java .util .SortedMap ;
9+ import java .util .TreeMap ;
10+
11+ import org .apache .http .Consts ;
12+ import org .apache .http .HttpHost ;
13+ import org .apache .http .client .config .RequestConfig ;
14+ import org .apache .http .client .methods .CloseableHttpResponse ;
15+ import org .apache .http .client .methods .HttpPost ;
16+ import org .apache .http .entity .StringEntity ;
17+ import org .joor .Reflect ;
18+ import org .slf4j .Logger ;
19+ import org .slf4j .LoggerFactory ;
20+ import org .slf4j .helpers .MessageFormatter ;
21+
22+ import com .beust .jcommander .internal .Maps ;
323import com .thoughtworks .xstream .XStream ;
24+ import com .thoughtworks .xstream .annotations .XStreamAlias ;
25+
426import me .chanjar .weixin .common .bean .result .WxError ;
527import me .chanjar .weixin .common .exception .WxErrorException ;
628import me .chanjar .weixin .common .util .crypto .WxCryptUtil ;
1133import me .chanjar .weixin .mp .bean .pay .WxMpPayRefundResult ;
1234import me .chanjar .weixin .mp .bean .pay .WxMpPayResult ;
1335import me .chanjar .weixin .mp .bean .pay .WxRedpackResult ;
14- import me .chanjar .weixin .mp .bean .result .*;
15- import org .apache .http .Consts ;
16- import org .apache .http .HttpHost ;
17- import org .apache .http .client .config .RequestConfig ;
18- import org .apache .http .client .methods .CloseableHttpResponse ;
19- import org .apache .http .client .methods .HttpPost ;
20- import org .apache .http .entity .StringEntity ;
21- import org .slf4j .Logger ;
22- import org .slf4j .LoggerFactory ;
23- import org .slf4j .helpers .MessageFormatter ;
24-
25- import java .io .IOException ;
26- import java .util .HashMap ;
27- import java .util .Map ;
28- import java .util .SortedMap ;
29- import java .util .TreeMap ;
36+ import me .chanjar .weixin .mp .bean .pay .WxSendRedpackRequest ;
37+ import me .chanjar .weixin .mp .bean .result .WxMpPrepayIdResult ;
3038
3139/**
3240 * Created by Binary Wang on 2016/7/28.
@@ -77,7 +85,7 @@ public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
7785 packageParams .put ("nonce_str" , System .currentTimeMillis () + "" );
7886 checkParameters (packageParams );
7987
80- String sign = WxCryptUtil .createSign (packageParams ,
88+ String sign = WxCryptUtil .createSignForPay (packageParams ,
8189 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
8290 packageParams .put ("sign" , sign );
8391
@@ -211,7 +219,7 @@ public Map<String, String> getPayInfo(Map<String, String> parameters)
211219 payInfo .put ("codeUrl" , wxMpPrepayIdResult .getCode_url ());
212220 }
213221
214- String finalSign = WxCryptUtil .createSign (payInfo ,
222+ String finalSign = WxCryptUtil .createSignForPay (payInfo ,
215223 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
216224 payInfo .put ("paySign" , finalSign );
217225 return payInfo ;
@@ -238,7 +246,7 @@ public WxMpPayResult getJSSDKPayResult(String transactionId,
238246 }
239247
240248 packageParams .put ("nonce_str" , nonce_str );
241- packageParams .put ("sign" , WxCryptUtil .createSign (packageParams ,
249+ packageParams .put ("sign" , WxCryptUtil .createSignForPay (packageParams ,
242250 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ()));
243251
244252 StringBuilder request = new StringBuilder ("<xml>" );
@@ -295,7 +303,7 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
295303 refundParams .put ("nonce_str" , System .currentTimeMillis () + "" );
296304 refundParams .put ("op_user_id" ,
297305 this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
298- String sign = WxCryptUtil .createSign (refundParams ,
306+ String sign = WxCryptUtil .createSignForPay (refundParams ,
299307 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
300308 refundParams .put ("sign" , sign );
301309
@@ -354,11 +362,12 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
354362 @ Override
355363 public boolean checkJSSDKCallbackDataSignature (Map <String , String > kvm ,
356364 String signature ) {
357- return signature .equals (WxCryptUtil .createSign (kvm ,
365+ return signature .equals (WxCryptUtil .createSignForPay (kvm ,
358366 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ()));
359367 }
360368
361369 @ Override
370+ @ Deprecated
362371 public WxRedpackResult sendRedpack (Map <String , String > parameters )
363372 throws WxErrorException {
364373 SortedMap <String , String > packageParams = new TreeMap <>(parameters );
@@ -368,7 +377,7 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
368377 this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
369378 packageParams .put ("nonce_str" , System .currentTimeMillis () + "" );
370379
371- String sign = WxCryptUtil .createSign (packageParams ,
380+ String sign = WxCryptUtil .createSignForPay (packageParams ,
372381 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
373382 packageParams .put ("sign" , sign );
374383
@@ -409,4 +418,49 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
409418 }
410419 }
411420
421+ @ Override
422+ public WxRedpackResult sendRedpack (WxSendRedpackRequest request ) throws WxErrorException {
423+ XStream xstream = XStreamInitializer .getInstance ();
424+ xstream .processAnnotations (WxRedpackResult .class );
425+
426+ request .setWxAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
427+ request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
428+ request .setNonceStr (System .currentTimeMillis () + "" );
429+
430+ String sign = WxCryptUtil .createSignForPay (xmlBean2Map (request ),
431+ this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
432+ request .setSign (sign );
433+
434+ String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" ;
435+ if (request .getAmtType () != null ) {
436+ //裂变红包
437+ url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack" ;
438+ }
439+
440+ String responseContent = this .wxMpService .post (url , xstream .toXML (request ));
441+ return (WxRedpackResult ) xstream .fromXML (responseContent );
442+ }
443+
444+ private Map <String , String > xmlBean2Map (Object bean ) {
445+ Map <String , String > result = Maps .newHashMap ();
446+ for (Entry <String , Reflect > entry : Reflect .on (bean ).fields ().entrySet ()) {
447+ Reflect reflect = entry .getValue ();
448+ if (reflect .get () == null ) {
449+ continue ;
450+ }
451+
452+ try {
453+ Field field = WxSendRedpackRequest .class .getDeclaredField (entry .getKey ());
454+ if (field .isAnnotationPresent (XStreamAlias .class )) {
455+ result .put (reflect .get ().toString (), field .getAnnotation (XStreamAlias .class ).value ());
456+ }
457+ } catch (NoSuchFieldException | SecurityException e ) {
458+ e .printStackTrace ();
459+ }
460+
461+ }
462+
463+ return result ;
464+ }
465+
412466}
0 commit comments