11package com .egzosn .pay .wx .api ;
22
3+ import java .io .ByteArrayInputStream ;
4+ import java .io .ByteArrayOutputStream ;
5+ import java .io .File ;
6+ import java .io .FileOutputStream ;
7+ import java .io .IOException ;
8+ import java .io .InputStream ;
9+ import java .io .OutputStream ;
10+ import java .net .URLEncoder ;
11+ import java .security .GeneralSecurityException ;
12+ import java .util .Date ;
13+ import java .util .HashMap ;
14+ import java .util .Map ;
15+ import java .util .SortedMap ;
16+ import java .util .TreeMap ;
17+ import java .util .zip .GZIPInputStream ;
18+
19+ import static com .egzosn .pay .wx .api .WxConst .APPID ;
20+ import static com .egzosn .pay .wx .api .WxConst .CIPHER_ALGORITHM ;
21+ import static com .egzosn .pay .wx .api .WxConst .FAIL ;
22+ import static com .egzosn .pay .wx .api .WxConst .FAILURE ;
23+ import static com .egzosn .pay .wx .api .WxConst .HMACSHA256 ;
24+ import static com .egzosn .pay .wx .api .WxConst .HMAC_SHA256 ;
25+ import static com .egzosn .pay .wx .api .WxConst .MCH_ID ;
26+ import static com .egzosn .pay .wx .api .WxConst .NONCE_STR ;
27+ import static com .egzosn .pay .wx .api .WxConst .OUT_TRADE_NO ;
28+ import static com .egzosn .pay .wx .api .WxConst .RESULT_CODE ;
29+ import static com .egzosn .pay .wx .api .WxConst .RETURN_CODE ;
30+ import static com .egzosn .pay .wx .api .WxConst .RETURN_MSG_CODE ;
31+ import static com .egzosn .pay .wx .api .WxConst .SANDBOXNEW ;
32+ import static com .egzosn .pay .wx .api .WxConst .SIGN ;
33+ import static com .egzosn .pay .wx .api .WxConst .SUCCESS ;
34+ import static com .egzosn .pay .wx .api .WxConst .URI ;
35+ import static com .egzosn .pay .wx .bean .WxTransferType .GETTRANSFERINFO ;
36+ import static com .egzosn .pay .wx .bean .WxTransferType .QUERY_BANK ;
37+ import static com .egzosn .pay .wx .bean .WxTransferType .TRANSFERS ;
38+
339import com .alibaba .fastjson .JSONObject ;
440import com .egzosn .pay .common .api .BasePayService ;
5- import com .egzosn .pay .common .bean .*;
41+ import com .egzosn .pay .common .bean .MethodType ;
42+ import com .egzosn .pay .common .bean .PayMessage ;
43+ import com .egzosn .pay .common .bean .PayOrder ;
44+ import com .egzosn .pay .common .bean .PayOutMessage ;
45+ import com .egzosn .pay .common .bean .RefundOrder ;
46+ import com .egzosn .pay .common .bean .SignType ;
47+ import com .egzosn .pay .common .bean .TransactionType ;
48+ import com .egzosn .pay .common .bean .TransferOrder ;
649import com .egzosn .pay .common .bean .result .PayException ;
750import com .egzosn .pay .common .exception .PayErrorException ;
51+ import com .egzosn .pay .common .http .ClientHttpRequest ;
852import com .egzosn .pay .common .http .HttpConfigStorage ;
53+ import com .egzosn .pay .common .http .HttpStringEntity ;
954import com .egzosn .pay .common .util .DateUtils ;
1055import com .egzosn .pay .common .util .Util ;
1156import com .egzosn .pay .common .util .XML ;
1257import com .egzosn .pay .common .util .sign .SignUtils ;
1358import com .egzosn .pay .common .util .sign .encrypt .RSA2 ;
1459import com .egzosn .pay .common .util .str .StringUtils ;
15- import com .egzosn .pay .wx .bean .*;
16-
17- import java .io .*;
18- import java .net .URLEncoder ;
19- import java .security .GeneralSecurityException ;
20- import java .util .*;
21- import java .util .zip .GZIPInputStream ;
22- import java .util .zip .GZIPOutputStream ;
23- import java .util .zip .ZipEntry ;
24- import java .util .zip .ZipInputStream ;
25-
26- import static com .egzosn .pay .wx .api .WxConst .*;
27- import static com .egzosn .pay .wx .bean .WxTransferType .*;
60+ import com .egzosn .pay .wx .bean .RedpackOrder ;
61+ import com .egzosn .pay .wx .bean .WxPayError ;
62+ import com .egzosn .pay .wx .bean .WxPayMessage ;
63+ import com .egzosn .pay .wx .bean .WxRefundResult ;
64+ import com .egzosn .pay .wx .bean .WxSendredpackType ;
65+ import com .egzosn .pay .wx .bean .WxTransactionType ;
66+ import com .egzosn .pay .wx .bean .WxTransferType ;
2867
2968/**
3069 * 微信支付服务
@@ -93,20 +132,13 @@ public String getReqUrl(TransactionType transactionType) {
93132 @ Override
94133 public boolean verify (Map <String , Object > params ) {
95134
96- if (!(SUCCESS .equals (params .get (RETURN_CODE )) && SUCCESS .equals (params .get (RESULT_CODE )))) {
135+ if (null == params . get ( SIGN ) || !(SUCCESS .equals (params .get (RETURN_CODE )) && SUCCESS .equals (params .get (RESULT_CODE ))) ) {
97136 if (LOG .isErrorEnabled ()) {
98137 LOG .error (String .format ("微信支付异常:return_code=%s,参数集=%s" , params .get (RETURN_CODE ), params ));
99138 }
100139 return false ;
101140 }
102141
103- if (null == params .get (SIGN )) {
104- if (LOG .isDebugEnabled ()) {
105- LOG .debug (String .format ("微信支付异常:签名为空!%s=%s" , OUT_TRADE_NO , params .get (OUT_TRADE_NO )));
106- }
107- return false ;
108- }
109-
110142 try {
111143 return signVerify (params , (String ) params .get (SIGN )) && verifySource ((String ) params .get (OUT_TRADE_NO ));
112144 } catch (PayErrorException e ) {
@@ -191,6 +223,7 @@ public JSONObject unifiedOrder(PayOrder order) {
191223 parameters .put ("total_fee" , Util .conversionCentAmount (order .getPrice ()));
192224 setParameters (parameters , "attach" , order .getAddition ());
193225 parameters .put ("notify_url" , payConfigStorage .getNotifyUrl ());
226+ setParameters (parameters , "notify_url" , order );
194227 parameters .put ("trade_type" , order .getTransactionType ().getType ());
195228 if (null != order .getExpirationTime ()) {
196229 parameters .put ("time_start" , DateUtils .formatDate (new Date (), DateUtils .YYYYMMDDHHMMSS ));
@@ -217,8 +250,10 @@ public JSONObject unifiedOrder(PayOrder order) {
217250 LOG .debug ("requestXML:" + requestXML );
218251 }
219252
253+ HttpStringEntity entity = new HttpStringEntity (requestXML , ClientHttpRequest .APPLICATION_XML_UTF_8 );
254+
220255 //调起支付的参数列表
221- JSONObject result = requestTemplate .postForObject (getReqUrl (order .getTransactionType ()), requestXML , JSONObject .class );
256+ JSONObject result = requestTemplate .postForObject (getReqUrl (order .getTransactionType ()), entity , JSONObject .class );
222257
223258 if (!SUCCESS .equals (result .get (RETURN_CODE )) || !SUCCESS .equals (result .get (RESULT_CODE ))) {
224259 throw new PayErrorException (new WxPayError (result .getString (RESULT_CODE ), result .getString (RETURN_MSG_CODE ), result .toJSONString ()));
@@ -305,7 +340,8 @@ private String getKeyPrivate() {
305340 String sign = createSign (SignUtils .parameterText (parameters , "&" , SIGN , "appId" ), payConfigStorage .getInputCharset (), false );
306341 parameters .put (SIGN , sign );
307342
308- JSONObject result = requestTemplate .postForObject (getReqUrl (WxTransactionType .GETSIGNKEY ), XML .getMap2Xml (parameters ), JSONObject .class );
343+ HttpStringEntity entity = new HttpStringEntity (XML .getMap2Xml (parameters ), ClientHttpRequest .APPLICATION_XML_UTF_8 );
344+ JSONObject result = requestTemplate .postForObject (getReqUrl (WxTransactionType .GETSIGNKEY ), entity , JSONObject .class );
309345 if (SUCCESS .equals (result .get (RETURN_CODE ))) {
310346 return result .getString ("sandbox_signkey" );
311347 }
0 commit comments