6666import me .chanjar .weixin .mp .bean .result .WxMpUserCumulate ;
6767import me .chanjar .weixin .mp .bean .result .WxMpUserList ;
6868import me .chanjar .weixin .mp .bean .result .WxMpUserSummary ;
69+ import me .chanjar .weixin .mp .bean .result .WxRedpackResult ;
6970import me .chanjar .weixin .mp .util .http .MaterialDeleteRequestExecutor ;
7071import me .chanjar .weixin .mp .util .http .MaterialNewsInfoRequestExecutor ;
7172import me .chanjar .weixin .mp .util .http .MaterialUploadRequestExecutor ;
8485import org .apache .http .client .methods .CloseableHttpResponse ;
8586import org .apache .http .client .methods .HttpGet ;
8687import org .apache .http .client .methods .HttpPost ;
88+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
8789import org .apache .http .entity .StringEntity ;
8890import org .apache .http .impl .client .BasicCredentialsProvider ;
8991import org .apache .http .impl .client .BasicResponseHandler ;
9092import org .apache .http .impl .client .CloseableHttpClient ;
93+ import org .apache .http .impl .client .HttpClientBuilder ;
9194import org .apache .http .impl .client .HttpClients ;
9295import org .slf4j .Logger ;
9396import org .slf4j .LoggerFactory ;
97+ import org .slf4j .helpers .MessageFormatter ;
9498
9599import com .google .gson .JsonArray ;
96100import com .google .gson .JsonElement ;
@@ -743,6 +747,7 @@ public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
743747 String http_proxy_username = wxMpConfigStorage .getHttp_proxy_username ();
744748 String http_proxy_password = wxMpConfigStorage .getHttp_proxy_password ();
745749
750+ final HttpClientBuilder builder = HttpClients .custom ();
746751 if (StringUtils .isNotBlank (http_proxy_host )) {
747752 // 使用代理服务器
748753 if (StringUtils .isNotBlank (http_proxy_username )) {
@@ -751,18 +756,22 @@ public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
751756 credsProvider .setCredentials (
752757 new AuthScope (http_proxy_host , http_proxy_port ),
753758 new UsernamePasswordCredentials (http_proxy_username , http_proxy_password ));
754- httpClient = HttpClients
755- .custom ()
756- .setDefaultCredentialsProvider (credsProvider )
757- .build ();
759+ builder
760+ .setDefaultCredentialsProvider (credsProvider );
758761 } else {
759762 // 无需用户认证的代理服务器
760- httpClient = HttpClients .createDefault ();
761763 }
762764 httpProxy = new HttpHost (http_proxy_host , http_proxy_port );
763- } else {
764- httpClient = HttpClients .createDefault ();
765765 }
766+ if (wxConfigProvider .getSSLContext () != null ){
767+ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (
768+ wxConfigProvider .getSSLContext (),
769+ new String [] { "TLSv1" },
770+ null ,
771+ SSLConnectionSocketFactory .BROWSER_COMPATIBLE_HOSTNAME_VERIFIER );
772+ builder .setSSLSocketFactory (sslsf );
773+ }
774+ httpClient = builder .build ();
766775 }
767776
768777 @ Override
@@ -941,4 +950,45 @@ public WxMpPayCallback getJSSDKCallbackData(String xmlData) {
941950 public boolean checkJSSDKCallbackDataSignature (Map <String , String > kvm , String signature ) {
942951 return signature .equals (WxCryptUtil .createSign (kvm , wxMpConfigStorage .getPartnerKey ()));
943952 }
953+
954+ @ Override
955+ public WxRedpackResult sendRedpack (Map <String , String > parameters ) throws WxErrorException {
956+ String nonce_str = System .currentTimeMillis () + "" ;
957+
958+ SortedMap <String , String > packageParams = new TreeMap <String , String >(parameters );
959+ packageParams .put ("wxappid" , wxMpConfigStorage .getAppId ());
960+ packageParams .put ("mch_id" , wxMpConfigStorage .getPartnerId ());
961+ packageParams .put ("nonce_str" , nonce_str );
962+
963+ String sign = WxCryptUtil .createSign (packageParams , wxMpConfigStorage .getPartnerKey ());
964+ packageParams .put ("sign" , sign );
965+
966+ StringBuilder request = new StringBuilder ("<xml>" );
967+ for (Entry <String , String > para : packageParams .entrySet ()) {
968+ request .append (String .format ("<%s>%s</%s>" , para .getKey (), para .getValue (), para .getKey ()));
969+ }
970+ request .append ("</xml>" );
971+
972+ HttpPost httpPost = new HttpPost ("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" );
973+ if (httpProxy != null ) {
974+ RequestConfig config = RequestConfig .custom ().setProxy (httpProxy ).build ();
975+ httpPost .setConfig (config );
976+ }
977+
978+ StringEntity entity = new StringEntity (request .toString (), Consts .UTF_8 );
979+ httpPost .setEntity (entity );
980+ try {
981+ CloseableHttpResponse response = getHttpclient ().execute (httpPost );
982+ String responseContent = Utf8ResponseHandler .INSTANCE .handleResponse (response );
983+ XStream xstream = XStreamInitializer .getInstance ();
984+ xstream .processAnnotations (WxRedpackResult .class );
985+ WxRedpackResult wxMpRedpackResult = (WxRedpackResult ) xstream .fromXML (responseContent );
986+ return wxMpRedpackResult ;
987+ } catch (IOException e ) {
988+ log .error (MessageFormatter .format ("The exception was happened when sending redpack '{}'." , request .toString ()).getMessage (), e );
989+ WxError error = new WxError ();
990+ error .setErrorCode (-1 );
991+ throw new WxErrorException (error );
992+ }
993+ }
944994}
0 commit comments