88import java .util .SortedMap ;
99import java .util .TreeMap ;
1010
11+ import org .apache .commons .codec .digest .DigestUtils ;
1112import org .apache .http .Consts ;
1213import org .apache .http .HttpHost ;
1314import org .apache .http .client .config .RequestConfig ;
2526
2627import me .chanjar .weixin .common .bean .result .WxError ;
2728import me .chanjar .weixin .common .exception .WxErrorException ;
28- import me .chanjar .weixin .common .util .crypto .WxCryptUtil ;
2929import me .chanjar .weixin .common .util .http .Utf8ResponseHandler ;
3030import me .chanjar .weixin .common .util .xml .XStreamInitializer ;
3131import me .chanjar .weixin .mp .api .WxMpPayService ;
@@ -85,7 +85,7 @@ public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
8585 packageParams .put ("nonce_str" , System .currentTimeMillis () + "" );
8686 checkParameters (packageParams );
8787
88- String sign = WxCryptUtil . createSignForPay (packageParams ,
88+ String sign = this . createSign (packageParams ,
8989 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
9090 packageParams .put ("sign" , sign );
9191
@@ -219,7 +219,7 @@ public Map<String, String> getPayInfo(Map<String, String> parameters)
219219 payInfo .put ("codeUrl" , wxMpPrepayIdResult .getCode_url ());
220220 }
221221
222- String finalSign = WxCryptUtil . createSignForPay (payInfo ,
222+ String finalSign = this . createSign (payInfo ,
223223 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
224224 payInfo .put ("paySign" , finalSign );
225225 return payInfo ;
@@ -246,7 +246,7 @@ public WxMpPayResult getJSSDKPayResult(String transactionId,
246246 }
247247
248248 packageParams .put ("nonce_str" , nonce_str );
249- packageParams .put ("sign" , WxCryptUtil . createSignForPay (packageParams ,
249+ packageParams .put ("sign" , this . createSign (packageParams ,
250250 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ()));
251251
252252 StringBuilder request = new StringBuilder ("<xml>" );
@@ -303,7 +303,7 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
303303 refundParams .put ("nonce_str" , System .currentTimeMillis () + "" );
304304 refundParams .put ("op_user_id" ,
305305 this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
306- String sign = WxCryptUtil . createSignForPay (refundParams ,
306+ String sign = this . createSign (refundParams ,
307307 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
308308 refundParams .put ("sign" , sign );
309309
@@ -362,7 +362,7 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
362362 @ Override
363363 public boolean checkJSSDKCallbackDataSignature (Map <String , String > kvm ,
364364 String signature ) {
365- return signature .equals (WxCryptUtil . createSignForPay (kvm ,
365+ return signature .equals (this . createSign (kvm ,
366366 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ()));
367367 }
368368
@@ -377,7 +377,7 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
377377 this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
378378 packageParams .put ("nonce_str" , System .currentTimeMillis () + "" );
379379
380- String sign = WxCryptUtil . createSignForPay (packageParams ,
380+ String sign = this . createSign (packageParams ,
381381 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
382382 packageParams .put ("sign" , sign );
383383
@@ -428,7 +428,7 @@ public WxRedpackResult sendRedpack(WxSendRedpackRequest request) throws WxErrorE
428428 request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
429429 request .setNonceStr (System .currentTimeMillis () + "" );
430430
431- String sign = WxCryptUtil . createSignForPay (xmlBean2Map (request ),
431+ String sign = this . createSign (xmlBean2Map (request ),
432432 this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
433433 request .setSign (sign );
434434
@@ -453,7 +453,7 @@ private Map<String, String> xmlBean2Map(Object bean) {
453453 try {
454454 Field field = WxSendRedpackRequest .class .getDeclaredField (entry .getKey ());
455455 if (field .isAnnotationPresent (XStreamAlias .class )) {
456- result .put (reflect . get (). toString (), field . getAnnotation ( XStreamAlias . class ). value ());
456+ result .put (field . getAnnotation ( XStreamAlias . class ). value (), reflect . get (). toString ());
457457 }
458458 } catch (NoSuchFieldException | SecurityException e ) {
459459 e .printStackTrace ();
@@ -464,4 +464,26 @@ private Map<String, String> xmlBean2Map(Object bean) {
464464 return result ;
465465 }
466466
467+ /**
468+ * 微信公众号支付签名算法(详见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3)
469+ * @param packageParams 原始参数
470+ * @param signKey 加密Key(即 商户Key)
471+ * @return 签名字符串
472+ */
473+ private String createSign (Map <String , String > packageParams , String signKey ) {
474+ SortedMap <String , String > sortedMap = new TreeMap <>(packageParams );
475+
476+ StringBuffer toSign = new StringBuffer ();
477+ for (String key : sortedMap .keySet ()) {
478+ String value = packageParams .get (key );
479+ if (null != value && !"" .equals (value ) && !"sign" .equals (key ) && !"key" .equals (key )) {
480+ toSign .append (key + "=" + value + "&" );
481+ }
482+ }
483+
484+ toSign .append ("key=" + signKey );
485+
486+ return DigestUtils .md5Hex (toSign .toString ()).toUpperCase ();
487+ }
488+
467489}
0 commit comments