1
1
package com .github .binarywang .wxpay .config ;
2
2
3
3
import com .github .binarywang .wxpay .exception .WxPayException ;
4
- import com .github .binarywang .wxpay .v3 .WechatPayHttpClientBuilder ;
4
+ import com .github .binarywang .wxpay .v3 .WxPayV3HttpClientBuilder ;
5
5
import com .github .binarywang .wxpay .v3 .auth .AutoUpdateCertificatesVerifier ;
6
6
import com .github .binarywang .wxpay .v3 .auth .PrivateKeySigner ;
7
7
import com .github .binarywang .wxpay .v3 .auth .WechatPay2Credentials ;
8
8
import com .github .binarywang .wxpay .v3 .auth .WechatPay2Validator ;
9
9
import com .github .binarywang .wxpay .v3 .util .PemUtils ;
10
+ import jodd .util .ResourcesUtil ;
10
11
import lombok .Data ;
11
12
import org .apache .commons .io .IOUtils ;
12
13
import org .apache .commons .lang3 .RegExUtils ;
20
21
import java .nio .charset .StandardCharsets ;
21
22
import java .security .KeyStore ;
22
23
import java .security .PrivateKey ;
23
- import java .security .cert .X509Certificate ;
24
- import java .util .ArrayList ;
24
+ import java .util .Collections ;
25
25
26
26
/**
27
27
* 微信支付配置
31
31
@ Data
32
32
public class WxPayConfig {
33
33
private static final String DEFAULT_PAY_BASE_URL = "https://api.mch.weixin.qq.com" ;
34
+ private static final String PROBLEM_MSG = "证书文件【%s】有问题,请核实!" ;
35
+ private static final String NOT_FOUND_MSG = "证书文件【%s】不存在,请核实!" ;
34
36
35
37
/**
36
38
* 微信支付接口请求地址域名部分.
@@ -184,17 +186,20 @@ public SSLContext initSSLContext() throws WxPayException {
184
186
}
185
187
186
188
final String prefix = "classpath:" ;
187
- String fileHasProblemMsg = "证书文件【" + this .getKeyPath () + "】有问题,请核实!" ;
188
- String fileNotFoundMsg = "证书文件【" + this .getKeyPath () + "】不存在,请核实!" ;
189
+ String fileHasProblemMsg = String . format ( PROBLEM_MSG , this .getKeyPath ()) ;
190
+ String fileNotFoundMsg = String . format ( NOT_FOUND_MSG , this .getKeyPath ()) ;
189
191
if (this .getKeyPath ().startsWith (prefix )) {
190
192
String path = RegExUtils .removeFirst (this .getKeyPath (), prefix );
191
193
if (!path .startsWith ("/" )) {
192
194
path = "/" + path ;
193
195
}
194
-
195
- inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream (path );
196
- if (inputStream == null ) {
197
- throw new WxPayException (fileNotFoundMsg );
196
+ try {
197
+ inputStream = ResourcesUtil .getResourceAsStream (path );
198
+ if (inputStream == null ) {
199
+ throw new WxPayException (fileNotFoundMsg );
200
+ }
201
+ } catch (Exception e ) {
202
+ throw new WxPayException (fileNotFoundMsg , e );
198
203
}
199
204
} else if (this .getKeyPath ().startsWith ("http://" ) || this .getKeyPath ().startsWith ("https://" )) {
200
205
try {
@@ -232,7 +237,6 @@ public SSLContext initSSLContext() throws WxPayException {
232
237
}
233
238
}
234
239
235
-
236
240
/**
237
241
* 初始化api v3请求头 自动签名验签
238
242
* 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
@@ -266,9 +270,13 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
266
270
if (!keypath .startsWith ("/" )) {
267
271
keypath = "/" + keypath ;
268
272
}
269
- keyInputStream = WxPayConfig .class .getResourceAsStream (keypath );
270
- if (keyInputStream == null ) {
271
- throw new WxPayException ("证书文件【" + this .getPrivateKeyPath () + "】不存在,请核实!" );
273
+ try {
274
+ keyInputStream = ResourcesUtil .getResourceAsStream (keypath );
275
+ if (keyInputStream == null ) {
276
+ throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateKeyPath ()));
277
+ }
278
+ } catch (Exception e ) {
279
+ throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateKeyPath ()), e );
272
280
}
273
281
}
274
282
@@ -277,32 +285,29 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
277
285
if (!certpath .startsWith ("/" )) {
278
286
certpath = "/" + certpath ;
279
287
}
280
- certInputStream = WxPayConfig .class .getResourceAsStream (certpath );
281
- if (certInputStream == null ) {
282
- throw new WxPayException ("证书文件【" + this .getPrivateCertPath () + "】不存在,请核实!" );
288
+ try {
289
+ certInputStream = ResourcesUtil .getResourceAsStream (certpath );
290
+ if (certInputStream == null ) {
291
+ throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateCertPath ()));
292
+ }
293
+ } catch (Exception e ) {
294
+ throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateCertPath ()), e );
283
295
}
284
296
}
285
297
286
- CloseableHttpClient httpClient ;
287
298
try {
288
- WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder .create ();
289
299
PrivateKey merchantPrivateKey = PemUtils .loadPrivateKey (keyInputStream );
290
- X509Certificate x509Certificate = PemUtils .loadCertificate (certInputStream );
291
- ArrayList <X509Certificate > certificates = new ArrayList <>();
292
- certificates .add (x509Certificate );
293
- builder .withMerchant (mchId , certSerialNo , merchantPrivateKey );
294
- builder .withWechatpay (certificates );
295
- AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier (
296
- new WechatPay2Credentials (mchId , new PrivateKeySigner (certSerialNo , merchantPrivateKey )),
297
- apiV3Key .getBytes (StandardCharsets .UTF_8 ));
298
- builder .withValidator (new WechatPay2Validator (verifier ));
299
- httpClient = builder .build ();
300
+ CloseableHttpClient httpClient = WxPayV3HttpClientBuilder .create ()
301
+ .withMerchant (mchId , certSerialNo , merchantPrivateKey )
302
+ .withWechatpay (Collections .singletonList (PemUtils .loadCertificate (certInputStream )))
303
+ .withValidator (new WechatPay2Validator (new AutoUpdateCertificatesVerifier (
304
+ new WechatPay2Credentials (mchId , new PrivateKeySigner (certSerialNo , merchantPrivateKey )),
305
+ apiV3Key .getBytes (StandardCharsets .UTF_8 ))))
306
+ .build ();
300
307
this .apiV3HttpClient = httpClient ;
308
+ return httpClient ;
301
309
} catch (Exception e ) {
302
310
throw new WxPayException ("v3请求构造异常!" , e );
303
311
}
304
-
305
- return httpClient ;
306
-
307
312
}
308
313
}
0 commit comments