6
6
import com .github .binarywang .wxpay .v3 .WxPayV3DownloadHttpGet ;
7
7
import com .google .gson .JsonElement ;
8
8
import com .google .gson .JsonObject ;
9
+ import lombok .extern .slf4j .Slf4j ;
9
10
import me .chanjar .weixin .common .util .json .GsonParser ;
10
11
import org .apache .commons .lang3 .StringUtils ;
11
12
import org .apache .http .*;
39
40
*
40
41
* @author <a href="https://github.com/binarywang">Binary Wang</a>
41
42
*/
43
+ @ Slf4j
42
44
public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
43
45
44
46
private static final String ACCEPT = "Accept" ;
45
47
private static final String CONTENT_TYPE = "Content-Type" ;
46
48
private static final String APPLICATION_JSON = "application/json" ;
47
- private static final String WECHATPAY_SERIAL = "Wechatpay-Serial" ;
49
+ private static final String WECHAT_PAY_SERIAL = "Wechatpay-Serial" ;
48
50
49
51
@ Override
50
52
public byte [] postForBytes (String url , String requestStr , boolean useKey ) throws WxPayException {
@@ -55,15 +57,15 @@ public byte[] postForBytes(String url, String requestStr, boolean useKey) throws
55
57
try (CloseableHttpResponse response = httpClient .execute (httpPost )) {
56
58
final byte [] bytes = EntityUtils .toByteArray (response .getEntity ());
57
59
final String responseData = Base64 .getEncoder ().encodeToString (bytes );
58
- this .log . info ( " \n 【请求地址】:{} \n 【请求数据】:{} \n 【响应数据(Base64编码后)】:{}" , url , requestStr , responseData );
60
+ this .logRequestAndResponse ( url , requestStr , responseData );
59
61
wxApiData .set (new WxPayApiData (url , requestStr , responseData , null ));
60
62
return bytes ;
61
63
}
62
64
} finally {
63
65
httpPost .releaseConnection ();
64
66
}
65
67
} catch (Exception e ) {
66
- this .log . error ( " \n 【请求地址】:{} \n 【请求数据】:{} \n 【异常信息】:{}" , url , requestStr , e . getMessage () );
68
+ this .logError ( url , requestStr , e );
67
69
wxApiData .set (new WxPayApiData (url , requestStr , null , e .getMessage ()));
68
70
throw new WxPayException (e .getMessage (), e );
69
71
}
@@ -77,7 +79,7 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
77
79
try (CloseableHttpClient httpClient = httpClientBuilder .build ()) {
78
80
try (CloseableHttpResponse response = httpClient .execute (httpPost )) {
79
81
String responseString = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
80
- this .log . info ( " \n 【请求地址】:{} \n 【请求数据】:{} \n 【响应数据】:{}" , url , requestStr , responseString );
82
+ this .logRequestAndResponse ( url , requestStr , responseString );
81
83
if (this .getConfig ().isIfSaveApiData ()) {
82
84
wxApiData .set (new WxPayApiData (url , requestStr , responseString , null ));
83
85
}
@@ -87,7 +89,7 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
87
89
httpPost .releaseConnection ();
88
90
}
89
91
} catch (Exception e ) {
90
- this .log . error ( " \n 【请求地址】:{} \n 【请求数据】:{} \n 【异常信息】:{}" , url , requestStr , e . getMessage () );
92
+ this .logError ( url , requestStr , e );
91
93
if (this .getConfig ().isIfSaveApiData ()) {
92
94
wxApiData .set (new WxPayApiData (url , requestStr , null , e .getMessage ()));
93
95
}
@@ -97,13 +99,14 @@ public String post(String url, String requestStr, boolean useKey) throws WxPayEx
97
99
98
100
@ Override
99
101
public String postV3 (String url , String requestStr ) throws WxPayException {
100
- CloseableHttpClient httpClient = this .createApiV3HttpClient ();
101
102
HttpPost httpPost = this .createHttpPost (url , requestStr );
102
- httpPost .addHeader (ACCEPT , APPLICATION_JSON );
103
- httpPost .addHeader (CONTENT_TYPE , APPLICATION_JSON );
104
- String serialNumber = getWechatpaySerial (getConfig ());
105
- httpPost .addHeader (WECHATPAY_SERIAL , serialNumber );
106
- try (CloseableHttpResponse response = httpClient .execute (httpPost )) {
103
+ this .configureRequest (httpPost );
104
+ return this .requestV3 (url , requestStr , httpPost );
105
+ }
106
+
107
+ private String requestV3 (String url , String requestStr , HttpRequestBase httpRequestBase ) throws WxPayException {
108
+ try (CloseableHttpClient httpClient = this .createApiV3HttpClient ();
109
+ CloseableHttpResponse response = httpClient .execute (httpRequestBase )) {
107
110
//v3已经改为通过状态码判断200 204 成功
108
111
int statusCode = response .getStatusLine ().getStatusCode ();
109
112
//post方法有可能会没有返回值的情况
@@ -113,73 +116,34 @@ public String postV3(String url, String requestStr) throws WxPayException {
113
116
}
114
117
115
118
if (HttpStatus .SC_OK == statusCode || HttpStatus .SC_NO_CONTENT == statusCode ) {
116
- this .log . info ( " \n 【请求地址】:{} \n 【请求数据】:{} \n 【响应数据】:{}" , url , requestStr , responseString );
119
+ this .logRequestAndResponse ( url , requestStr , responseString );
117
120
return responseString ;
118
121
}
119
122
120
123
//有错误提示信息返回
121
124
JsonObject jsonObject = GsonParser .parse (responseString );
122
125
throw convertException (jsonObject );
123
126
} catch (Exception e ) {
124
- this .log . error ( " \n 【请求地址】:{} \n 【请求数据】:{} \n 【异常信息】:{}" , url , requestStr , e . getMessage () );
127
+ this .logError ( url , requestStr , e );
125
128
throw (e instanceof WxPayException ) ? (WxPayException ) e : new WxPayException (e .getMessage (), e );
126
129
} finally {
127
- httpPost .releaseConnection ();
130
+ httpRequestBase .releaseConnection ();
128
131
}
129
-
130
-
131
132
}
132
133
133
134
@ Override
134
135
public String patchV3 (String url , String requestStr ) throws WxPayException {
135
- CloseableHttpClient httpClient = this .createApiV3HttpClient ();
136
136
HttpPatch httpPatch = new HttpPatch (url );
137
- String serialNumber = getWechatpaySerial (getConfig ());
138
- httpPatch .addHeader (WECHATPAY_SERIAL , serialNumber );
139
137
httpPatch .setEntity (this .createEntry (requestStr ));
140
-
141
- httpPatch .setConfig (RequestConfig .custom ()
142
- .setConnectionRequestTimeout (this .getConfig ().getHttpConnectionTimeout ())
143
- .setConnectTimeout (this .getConfig ().getHttpConnectionTimeout ())
144
- .setSocketTimeout (this .getConfig ().getHttpTimeout ())
145
- .build ());
146
-
147
- httpPatch .addHeader (ACCEPT , APPLICATION_JSON );
148
- httpPatch .addHeader (CONTENT_TYPE , APPLICATION_JSON );
149
- try (CloseableHttpResponse response = httpClient .execute (httpPatch )) {
150
- //v3已经改为通过状态码判断200 204 成功
151
- int statusCode = response .getStatusLine ().getStatusCode ();
152
- //post方法有可能会没有返回值的情况
153
- String responseString = null ;
154
- if (response .getEntity () != null ) {
155
- responseString = EntityUtils .toString (response .getEntity (), StandardCharsets .UTF_8 );
156
- }
157
-
158
- if (HttpStatus .SC_OK == statusCode || HttpStatus .SC_NO_CONTENT == statusCode ) {
159
- this .log .info ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【响应数据】:{}" , url , requestStr , responseString );
160
- return responseString ;
161
- }
162
-
163
- //有错误提示信息返回
164
- JsonObject jsonObject = GsonParser .parse (responseString );
165
- throw convertException (jsonObject );
166
- } catch (Exception e ) {
167
- this .log .error ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【异常信息】:{}" , url , requestStr , e .getMessage ());
168
- throw (e instanceof WxPayException ) ? (WxPayException ) e : new WxPayException (e .getMessage (), e );
169
- } finally {
170
- httpPatch .releaseConnection ();
171
- }
138
+ return this .requestV3 (url , requestStr , httpPatch );
172
139
}
173
140
174
141
@ Override
175
142
public String postV3WithWechatpaySerial (String url , String requestStr ) throws WxPayException {
176
- CloseableHttpClient httpClient = this .createApiV3HttpClient ();
177
143
HttpPost httpPost = this .createHttpPost (url , requestStr );
178
- httpPost .addHeader (ACCEPT , APPLICATION_JSON );
179
- httpPost .addHeader (CONTENT_TYPE , APPLICATION_JSON );
180
- String serialNumber = getWechatpaySerial (getConfig ());
181
- httpPost .addHeader ("Wechatpay-Serial" , serialNumber );
182
- try (CloseableHttpResponse response = httpClient .execute (httpPost )) {
144
+ this .configureRequest (httpPost );
145
+ try (CloseableHttpClient httpClient = this .createApiV3HttpClient ();
146
+ CloseableHttpResponse response = httpClient .execute (httpPost )) {
183
147
//v3已经改为通过状态码判断200 204 成功
184
148
int statusCode = response .getStatusLine ().getStatusCode ();
185
149
String responseString = "{}" ;
@@ -189,16 +153,15 @@ public String postV3WithWechatpaySerial(String url, String requestStr) throws Wx
189
153
}
190
154
191
155
if (HttpStatus .SC_OK == statusCode || HttpStatus .SC_NO_CONTENT == statusCode ) {
192
- this .log . info ( " \n 【请求地址】:{} \n 【请求数据】:{} \n 【响应数据】:{}" , url , requestStr , responseString );
156
+ this .logRequestAndResponse ( url , requestStr , responseString );
193
157
return responseString ;
194
158
}
195
159
196
160
//有错误提示信息返回
197
161
JsonObject jsonObject = GsonParser .parse (responseString );
198
162
throw convertException (jsonObject );
199
163
} catch (Exception e ) {
200
- this .log .error ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【异常信息】:{}" , url , requestStr , e .getMessage ());
201
- e .printStackTrace ();
164
+ this .logError (url , requestStr , e );
202
165
throw (e instanceof WxPayException ) ? (WxPayException ) e : new WxPayException (e .getMessage (), e );
203
166
} finally {
204
167
httpPost .releaseConnection ();
@@ -207,21 +170,17 @@ public String postV3WithWechatpaySerial(String url, String requestStr) throws Wx
207
170
208
171
@ Override
209
172
public String postV3 (String url , HttpPost httpPost ) throws WxPayException {
210
- String serialNumber = getWechatpaySerial (getConfig ());
211
- httpPost .addHeader (WECHATPAY_SERIAL , serialNumber );
173
+ String serialNumber = getWechatPaySerial (getConfig ());
174
+ httpPost .addHeader (WECHAT_PAY_SERIAL , serialNumber );
212
175
return this .requestV3 (url , httpPost );
213
176
}
214
177
215
178
@ Override
216
179
public String requestV3 (String url , HttpRequestBase httpRequest ) throws WxPayException {
217
- httpRequest .setConfig (RequestConfig .custom ()
218
- .setConnectionRequestTimeout (this .getConfig ().getHttpConnectionTimeout ())
219
- .setConnectTimeout (this .getConfig ().getHttpConnectionTimeout ())
220
- .setSocketTimeout (this .getConfig ().getHttpTimeout ())
221
- .build ());
180
+ this .configureRequest (httpRequest );
222
181
223
- CloseableHttpClient httpClient = this .createApiV3HttpClient ();
224
- try ( CloseableHttpResponse response = httpClient .execute (httpRequest )) {
182
+ try ( CloseableHttpClient httpClient = this .createApiV3HttpClient ();
183
+ CloseableHttpResponse response = httpClient .execute (httpRequest )) {
225
184
//v3已经改为通过状态码判断200 204 成功
226
185
int statusCode = response .getStatusLine ().getStatusCode ();
227
186
//post方法有可能会没有返回值的情况
@@ -252,38 +211,30 @@ public String getV3(String url) throws WxPayException {
252
211
return getV3WithWechatPaySerial (url );
253
212
}
254
213
HttpGet httpGet = new HttpGet (url );
255
- httpGet .addHeader (ACCEPT , APPLICATION_JSON );
256
- httpGet .addHeader (CONTENT_TYPE , APPLICATION_JSON );
257
- String serialNumber = getWechatpaySerial (getConfig ());
258
- httpGet .addHeader (WECHATPAY_SERIAL , serialNumber );
259
214
return this .requestV3 (url , httpGet );
260
215
}
261
216
262
217
@ Override
263
218
public String getV3WithWechatPaySerial (String url ) throws WxPayException {
264
219
HttpGet httpGet = new HttpGet (url );
265
- httpGet .addHeader (ACCEPT , APPLICATION_JSON );
266
- httpGet .addHeader (CONTENT_TYPE , APPLICATION_JSON );
267
- String serialNumber = getWechatpaySerial (getConfig ());
268
- httpGet .addHeader (WECHATPAY_SERIAL , serialNumber );
269
220
return this .requestV3 (url , httpGet );
270
221
}
271
222
272
223
@ Override
273
224
public InputStream downloadV3 (String url ) throws WxPayException {
274
- CloseableHttpClient httpClient = this .createApiV3HttpClient ();
275
225
HttpGet httpGet = new WxPayV3DownloadHttpGet (url );
276
226
httpGet .addHeader (ACCEPT , ContentType .WILDCARD .getMimeType ());
277
- String serialNumber = getWechatpaySerial (getConfig ());
278
- httpGet .addHeader (WECHATPAY_SERIAL , serialNumber );
279
- try (CloseableHttpResponse response = httpClient .execute (httpGet )) {
227
+ String serialNumber = getWechatPaySerial (getConfig ());
228
+ httpGet .addHeader (WECHAT_PAY_SERIAL , serialNumber );
229
+ try (CloseableHttpClient httpClient = this .createApiV3HttpClient ();
230
+ CloseableHttpResponse response = httpClient .execute (httpGet )) {
280
231
//v3已经改为通过状态码判断200 204 成功
281
232
int statusCode = response .getStatusLine ().getStatusCode ();
282
233
Header contentType = response .getFirstHeader (HttpHeaders .CONTENT_TYPE );
283
234
boolean isJsonContentType = Objects .nonNull (contentType ) && ContentType .APPLICATION_JSON .getMimeType ()
284
235
.equals (ContentType .parse (String .valueOf (contentType .getValue ())).getMimeType ());
285
236
if ((HttpStatus .SC_OK == statusCode || HttpStatus .SC_NO_CONTENT == statusCode ) && !isJsonContentType ) {
286
- this . log .info ("\n 【请求地址】:{}\n " , url );
237
+ log .info ("\n 【请求地址】:{}\n " , url );
287
238
return response .getEntity ().getContent ();
288
239
}
289
240
@@ -293,7 +244,7 @@ public InputStream downloadV3(String url) throws WxPayException {
293
244
JsonObject jsonObject = GsonParser .parse (responseString );
294
245
throw convertException (jsonObject );
295
246
} catch (Exception e ) {
296
- this . log .error ("\n 【请求地址】:{}\n 【异常信息】:{}" , url , e .getMessage ());
247
+ log .error ("\n 【请求地址】:{}\n 【异常信息】:{}" , url , e .getMessage ());
297
248
throw (e instanceof WxPayException ) ? (WxPayException ) e : new WxPayException (e .getMessage (), e );
298
249
} finally {
299
250
httpGet .releaseConnection ();
@@ -305,23 +256,28 @@ public String putV3(String url, String requestStr) throws WxPayException {
305
256
HttpPut httpPut = new HttpPut (url );
306
257
StringEntity entity = this .createEntry (requestStr );
307
258
httpPut .setEntity (entity );
308
- httpPut .addHeader (ACCEPT , APPLICATION_JSON );
309
- httpPut .addHeader (CONTENT_TYPE , APPLICATION_JSON );
310
- String serialNumber = getWechatpaySerial (getConfig ());
311
- httpPut .addHeader (WECHATPAY_SERIAL , serialNumber );
312
259
return requestV3 (url , httpPut );
313
260
}
314
261
315
262
@ Override
316
263
public String deleteV3 (String url ) throws WxPayException {
317
264
HttpDelete httpDelete = new HttpDelete (url );
318
- httpDelete .addHeader (ACCEPT , APPLICATION_JSON );
319
- httpDelete .addHeader (CONTENT_TYPE , APPLICATION_JSON );
320
- String serialNumber = getWechatpaySerial (getConfig ());
321
- httpDelete .addHeader (WECHATPAY_SERIAL , serialNumber );
322
265
return requestV3 (url , httpDelete );
323
266
}
324
267
268
+ private void configureRequest (HttpRequestBase request ) {
269
+ String serialNumber = getWechatPaySerial (getConfig ());
270
+ request .addHeader (ACCEPT , APPLICATION_JSON );
271
+ request .addHeader (CONTENT_TYPE , APPLICATION_JSON );
272
+ request .addHeader (WECHAT_PAY_SERIAL , serialNumber );
273
+
274
+ request .setConfig (RequestConfig .custom ()
275
+ .setConnectionRequestTimeout (this .getConfig ().getHttpConnectionTimeout ())
276
+ .setConnectTimeout (this .getConfig ().getHttpConnectionTimeout ())
277
+ .setSocketTimeout (this .getConfig ().getHttpTimeout ())
278
+ .build ());
279
+ }
280
+
325
281
private CloseableHttpClient createApiV3HttpClient () throws WxPayException {
326
282
CloseableHttpClient apiV3HttpClient = this .getConfig ().getApiV3HttpClient ();
327
283
if (null == apiV3HttpClient ) {
@@ -387,7 +343,6 @@ private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayExc
387
343
new DefaultHostnameVerifier ()));
388
344
}
389
345
390
-
391
346
private WxPayException convertException (JsonObject jsonObject ) {
392
347
//todo 这里考虑使用新的适用于V3的异常
393
348
JsonElement codeElement = jsonObject .get ("code" );
@@ -401,13 +356,20 @@ private WxPayException convertException(JsonObject jsonObject) {
401
356
402
357
/**
403
358
* 兼容微信支付公钥模式
404
- * @param wxPayConfig
405
- * @return
406
359
*/
407
- private String getWechatpaySerial (WxPayConfig wxPayConfig ) {
360
+ private String getWechatPaySerial (WxPayConfig wxPayConfig ) {
408
361
if (StringUtils .isNotBlank (wxPayConfig .getPublicKeyId ())) {
409
362
return wxPayConfig .getPublicKeyId ();
410
363
}
364
+
411
365
return wxPayConfig .getVerifier ().getValidCertificate ().getSerialNumber ().toString (16 ).toUpperCase ();
412
366
}
367
+
368
+ private void logRequestAndResponse (String url , String requestStr , String responseStr ) {
369
+ log .info ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【响应数据】:{}" , url , requestStr , responseStr );
370
+ }
371
+
372
+ private void logError (String url , String requestStr , Exception e ) {
373
+ log .error ("\n 【请求地址】:{}\n 【请求数据】:{}\n 【异常信息】:{}" , url , requestStr , e .getMessage ());
374
+ }
413
375
}
0 commit comments