11package me .chanjar .weixin .mp .api .impl ;
22
3+ import java .io .File ;
4+ import java .io .FileInputStream ;
35import java .lang .reflect .Field ;
6+ import java .security .KeyStore ;
47import java .util .HashMap ;
58import java .util .List ;
69import java .util .Map ;
710import java .util .Map .Entry ;
811import java .util .SortedMap ;
912import java .util .TreeMap ;
1013
14+ import javax .net .ssl .SSLContext ;
15+
1116import org .apache .commons .codec .digest .DigestUtils ;
1217import org .apache .commons .lang3 .StringUtils ;
18+ import org .apache .http .client .methods .CloseableHttpResponse ;
19+ import org .apache .http .client .methods .HttpPost ;
20+ import org .apache .http .conn .ssl .DefaultHostnameVerifier ;
21+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
22+ import org .apache .http .entity .StringEntity ;
23+ import org .apache .http .impl .client .CloseableHttpClient ;
24+ import org .apache .http .impl .client .HttpClients ;
25+ import org .apache .http .ssl .SSLContexts ;
26+ import org .apache .http .util .EntityUtils ;
1327import org .joor .Reflect ;
1428
1529import com .google .common .collect .Lists ;
2337import me .chanjar .weixin .common .util .xml .XStreamInitializer ;
2438import me .chanjar .weixin .mp .api .WxMpPayService ;
2539import me .chanjar .weixin .mp .api .WxMpService ;
40+ import me .chanjar .weixin .mp .bean .pay .WxEntPayRequest ;
41+ import me .chanjar .weixin .mp .bean .pay .WxEntPayResult ;
2642import me .chanjar .weixin .mp .bean .pay .WxMpPayCallback ;
2743import me .chanjar .weixin .mp .bean .pay .WxMpPayRefundResult ;
2844import me .chanjar .weixin .mp .bean .pay .WxMpPayResult ;
@@ -263,6 +279,23 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
263279
264280 private void checkParameters (WxUnifiedOrderRequest request ) {
265281
282+ checkNotNullParams (request );
283+
284+ if (!TRADE_TYPES .contains (request .getTradeType ())) {
285+ throw new IllegalArgumentException ("trade_type目前必须为" + TRADE_TYPES + "其中之一" );
286+
287+ }
288+
289+ if ("JSAPI" .equals (request .getTradeType ()) && request .getOpenid () == null ) {
290+ throw new IllegalArgumentException ("当 trade_type是'JSAPI'时未指定openid" );
291+ }
292+
293+ if ("NATIVE" .equals (request .getTradeType ()) && request .getProductId () == null ) {
294+ throw new IllegalArgumentException ("当 trade_type是'NATIVE'时未指定product_id" );
295+ }
296+ }
297+
298+ private void checkNotNullParams (Object request ) {
266299 List <String > nullFields = Lists .newArrayList ();
267300 for (Entry <String , Reflect > entry : Reflect .on (request ).fields ()
268301 .entrySet ()) {
@@ -281,20 +314,6 @@ private void checkParameters(WxUnifiedOrderRequest request) {
281314 if (!nullFields .isEmpty ()) {
282315 throw new IllegalArgumentException ("必填字段[" + nullFields + "]必须提供值" );
283316 }
284-
285- if (!TRADE_TYPES .contains (request .getTradeType ())) {
286- throw new IllegalArgumentException ("trade_type目前必须为" + TRADE_TYPES + "其中之一" );
287-
288- }
289-
290- if ("JSAPI" .equals (request .getTradeType ()) && request .getOpenid () == null ) {
291- throw new IllegalArgumentException ("当 trade_type是'JSAPI'时未指定openid" );
292- }
293-
294- if ("NATIVE" .equals (request .getTradeType ())
295- && request .getProductId () == null ) {
296- throw new IllegalArgumentException ("当 trade_type是'NATIVE'时未指定product_id" );
297- }
298317 }
299318
300319 @ Override
@@ -332,4 +351,50 @@ public Map<String, String> getPayInfo(WxUnifiedOrderRequest request) throws WxEr
332351 return payInfo ;
333352 }
334353
354+ @ Override
355+ public WxEntPayResult entPay (WxEntPayRequest request , File keyFile ) throws WxErrorException {
356+ checkNotNullParams (request );
357+
358+ XStream xstream = XStreamInitializer .getInstance ();
359+ xstream .processAnnotations (WxEntPayRequest .class );
360+ xstream .processAnnotations (WxEntPayResult .class );
361+
362+ request .setMchAppid (this .wxMpService .getWxMpConfigStorage ().getAppId ());
363+ request .setMchId (this .wxMpService .getWxMpConfigStorage ().getPartnerId ());
364+ request .setNonceStr (System .currentTimeMillis () + "" );
365+
366+ String sign = this .createSign (xmlBean2Map (request ), this .wxMpService .getWxMpConfigStorage ().getPartnerKey ());
367+ request .setSign (sign );
368+
369+ String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers" ;
370+
371+ try (FileInputStream instream = new FileInputStream (keyFile )) {
372+ String mchId = request .getMchId ();
373+ KeyStore keyStore = KeyStore .getInstance ("PKCS12" );
374+ keyStore .load (instream , mchId .toCharArray ());
375+
376+ SSLContext sslcontext = SSLContexts .custom ().loadKeyMaterial (keyStore , mchId .toCharArray ()).build ();
377+ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (sslcontext , new String [] { "TLSv1" }, null ,
378+ new DefaultHostnameVerifier ());
379+
380+ try (CloseableHttpClient httpclient = HttpClients .custom ().setSSLSocketFactory (sslsf ).build ()) {
381+ HttpPost httpPost = new HttpPost (url );
382+ httpPost .setEntity (new StringEntity (new String (xstream .toXML (request ).getBytes ("UTF-8" ), "ISO-8859-1" )));
383+
384+ try (CloseableHttpResponse response = httpclient .execute (httpPost )) {
385+ String responseContent = EntityUtils .toString (response .getEntity ());
386+ WxEntPayResult result = (WxEntPayResult ) xstream .fromXML (responseContent );
387+ if ("FAIL" .equals (result .getResultCode ())) {
388+ throw new WxErrorException (
389+ WxError .newBuilder ().setErrorMsg (result .getErrCode () + ":" + result .getErrCodeDes ()).build ());
390+ }
391+
392+ return result ;
393+ }
394+ }
395+ } catch (Exception e ) {
396+ throw new WxErrorException (WxError .newBuilder ().setErrorMsg (e .getMessage ()).build (), e );
397+ }
398+ }
399+
335400}
0 commit comments