11
11
import java .util .HashMap ;
12
12
import java .util .List ;
13
13
import java .util .Map ;
14
+ import java .util .Map .Entry ;
14
15
import java .util .SortedMap ;
15
16
import java .util .TreeMap ;
16
17
import java .util .UUID ;
@@ -634,8 +635,9 @@ public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws
634
635
param .addProperty ("end_date" , SIMPLE_DATE_FORMAT .format (endDate ));
635
636
String responseContent = post (url , param .toString ());
636
637
JsonElement tmpJsonElement = Streams .parse (new JsonReader (new StringReader (responseContent )));
637
- return WxMpGsonBuilder .INSTANCE .create ().fromJson (tmpJsonElement .getAsJsonObject ().get ("list" ), new TypeToken <List <WxMpUserSummary >>() {
638
- }.getType ());
638
+ return WxMpGsonBuilder .INSTANCE .create ().fromJson (tmpJsonElement .getAsJsonObject ().get ("list" ),
639
+ new TypeToken <List <WxMpUserSummary >>() {
640
+ }.getType ());
639
641
}
640
642
641
643
@ Override
@@ -695,7 +697,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
695
697
throw new RuntimeException ("微信服务端异常,超出重试次数" );
696
698
}
697
699
698
- protected <T , E > T executeInternal (RequestExecutor <T , E > executor , String uri , E data ) throws WxErrorException {
700
+ protected synchronized <T , E > T executeInternal (RequestExecutor <T , E > executor , String uri , E data ) throws WxErrorException {
699
701
if (uri .indexOf ("access_token=" ) != -1 ) {
700
702
throw new IllegalArgumentException ("uri参数中不允许有access_token: " + uri );
701
703
}
@@ -776,43 +778,45 @@ public void setMaxRetryTimes(int maxRetryTimes) {
776
778
777
779
@ Override
778
780
public WxMpPrepayIdResult getPrepayId (String openId , String outTradeNo , double amt , String body , String tradeType , String ip , String callbackUrl ) {
779
- String nonce_str = System .currentTimeMillis () + "" ;
780
-
781
- SortedMap <String , String > packageParams = new TreeMap <String , String >();
781
+ Map <String , String > packageParams = new HashMap <String , String >();
782
782
packageParams .put ("appid" , wxMpConfigStorage .getAppId ());
783
783
packageParams .put ("mch_id" , wxMpConfigStorage .getPartnerId ());
784
- packageParams .put ("nonce_str" , nonce_str );
785
784
packageParams .put ("body" , body );
786
785
packageParams .put ("out_trade_no" , outTradeNo );
787
-
788
786
packageParams .put ("total_fee" , (int ) (amt * 100 ) + "" );
789
787
packageParams .put ("spbill_create_ip" , ip );
790
788
packageParams .put ("notify_url" , callbackUrl );
791
789
packageParams .put ("trade_type" , tradeType );
792
790
packageParams .put ("openid" , openId );
793
791
792
+ return getPrepayId (packageParams );
793
+ }
794
+
795
+ public WxMpPrepayIdResult getPrepayId (final Map <String , String > parameters ) {
796
+ String nonce_str = System .currentTimeMillis () + "" ;
797
+
798
+ final SortedMap <String , String > packageParams = new TreeMap <String , String >(parameters );
799
+ packageParams .put ("appid" , wxMpConfigStorage .getAppId ());
800
+ packageParams .put ("mch_id" , wxMpConfigStorage .getPartnerId ());
801
+ packageParams .put ("nonce_str" , nonce_str );
802
+ checkParameters (packageParams );
803
+
794
804
String sign = WxCryptUtil .createSign (packageParams , wxMpConfigStorage .getPartnerKey ());
795
- String xml = "<xml>" +
796
- "<appid>" + wxMpConfigStorage .getAppId () + "</appid>" +
797
- "<mch_id>" + wxMpConfigStorage .getPartnerId () + "</mch_id>" +
798
- "<nonce_str>" + nonce_str + "</nonce_str>" +
799
- "<sign>" + sign + "</sign>" +
800
- "<body><![CDATA[" + body + "]]></body>" +
801
- "<out_trade_no>" + outTradeNo + "</out_trade_no>" +
802
- "<total_fee>" + packageParams .get ("total_fee" ) + "</total_fee>" +
803
- "<spbill_create_ip>" + ip + "</spbill_create_ip>" +
804
- "<notify_url>" + callbackUrl + "</notify_url>" +
805
- "<trade_type>" + tradeType + "</trade_type>" +
806
- "<openid>" + openId + "</openid>" +
807
- "</xml>" ;
805
+ packageParams .put ("sign" , sign );
806
+
807
+ StringBuilder request = new StringBuilder ("<xml>" );
808
+ for (Entry <String , String > para : packageParams .entrySet ()) {
809
+ request .append (String .format ("<%s>%s</%s>" , para .getKey (), para .getValue (), para .getKey ()));
810
+ }
811
+ request .append ("</xml>" );
808
812
809
813
HttpPost httpPost = new HttpPost ("https://api.mch.weixin.qq.com/pay/unifiedorder" );
810
814
if (httpProxy != null ) {
811
815
RequestConfig config = RequestConfig .custom ().setProxy (httpProxy ).build ();
812
816
httpPost .setConfig (config );
813
817
}
814
818
815
- StringEntity entity = new StringEntity (xml , Consts .UTF_8 );
819
+ StringEntity entity = new StringEntity (request . toString () , Consts .UTF_8 );
816
820
httpPost .setEntity (entity );
817
821
try {
818
822
CloseableHttpResponse response = getHttpclient ().execute (httpPost );
@@ -827,9 +831,39 @@ public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double a
827
831
return new WxMpPrepayIdResult ();
828
832
}
829
833
834
+ final String [] REQUIRED_ORDER_PARAMETERS = new String [] { "appid" , "mch_id" , "body" , "out_trade_no" , "total_fee" , "spbill_create_ip" , "notify_url" ,
835
+ "trade_type" , };
836
+
837
+ private void checkParameters (Map <String , String > parameters ) {
838
+ for (String para : REQUIRED_ORDER_PARAMETERS ) {
839
+ if (!parameters .containsKey (para ))
840
+ throw new IllegalArgumentException ("Reqiured argument '" + para + "' is missing." );
841
+ }
842
+ if ("JSAPI" .equals (parameters .get ("trade_type" )) && !parameters .containsKey ("openid" ))
843
+ throw new IllegalArgumentException ("Reqiured argument 'openid' is missing when trade_type is 'JSAPI'." );
844
+ if ("NATIVE" .equals (parameters .get ("trade_type" )) && !parameters .containsKey ("product_id" ))
845
+ throw new IllegalArgumentException ("Reqiured argument 'product_id' is missing when trade_type is 'NATIVE'." );
846
+ }
847
+
830
848
@ Override
831
849
public Map <String , String > getJSSDKPayInfo (String openId , String outTradeNo , double amt , String body , String tradeType , String ip , String callbackUrl ) {
832
- WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId (openId , outTradeNo , amt , body , tradeType , ip , callbackUrl );
850
+ Map <String , String > packageParams = new HashMap <String , String >();
851
+ packageParams .put ("appid" , wxMpConfigStorage .getAppId ());
852
+ packageParams .put ("mch_id" , wxMpConfigStorage .getPartnerId ());
853
+ packageParams .put ("body" , body );
854
+ packageParams .put ("out_trade_no" , outTradeNo );
855
+ packageParams .put ("total_fee" , (int ) (amt * 100 ) + "" );
856
+ packageParams .put ("spbill_create_ip" , ip );
857
+ packageParams .put ("notify_url" , callbackUrl );
858
+ packageParams .put ("trade_type" , tradeType );
859
+ packageParams .put ("openid" , openId );
860
+
861
+ return getJSSDKPayInfo (packageParams );
862
+ }
863
+
864
+ @ Override
865
+ public Map <String , String > getJSSDKPayInfo (Map <String , String > parameters ) {
866
+ WxMpPrepayIdResult wxMpPrepayIdResult = getPrepayId (parameters );
833
867
String prepayId = wxMpPrepayIdResult .getPrepay_id ();
834
868
if (prepayId == null || prepayId .equals ("" )) {
835
869
throw new RuntimeException ("get prepayid error" );
0 commit comments