11package com .github .binarywang .wxpay .config ;
22
33import com .github .binarywang .wxpay .exception .WxPayException ;
4- import com .github .binarywang .wxpay .v3 .WechatPayHttpClientBuilder ;
4+ import com .github .binarywang .wxpay .v3 .WxPayV3HttpClientBuilder ;
55import com .github .binarywang .wxpay .v3 .auth .AutoUpdateCertificatesVerifier ;
66import com .github .binarywang .wxpay .v3 .auth .PrivateKeySigner ;
77import com .github .binarywang .wxpay .v3 .auth .WechatPay2Credentials ;
88import com .github .binarywang .wxpay .v3 .auth .WechatPay2Validator ;
99import com .github .binarywang .wxpay .v3 .util .PemUtils ;
10+ import jodd .util .ResourcesUtil ;
1011import lombok .Data ;
1112import org .apache .commons .io .IOUtils ;
1213import org .apache .commons .lang3 .RegExUtils ;
2021import java .nio .charset .StandardCharsets ;
2122import java .security .KeyStore ;
2223import java .security .PrivateKey ;
23- import java .security .cert .X509Certificate ;
24- import java .util .ArrayList ;
24+ import java .util .Collections ;
2525
2626/**
2727 * 微信支付配置
3131@ Data
3232public class WxPayConfig {
3333 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】不存在,请核实!" ;
3436
3537 /**
3638 * 微信支付接口请求地址域名部分.
@@ -184,17 +186,20 @@ public SSLContext initSSLContext() throws WxPayException {
184186 }
185187
186188 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 ()) ;
189191 if (this .getKeyPath ().startsWith (prefix )) {
190192 String path = RegExUtils .removeFirst (this .getKeyPath (), prefix );
191193 if (!path .startsWith ("/" )) {
192194 path = "/" + path ;
193195 }
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 );
198203 }
199204 } else if (this .getKeyPath ().startsWith ("http://" ) || this .getKeyPath ().startsWith ("https://" )) {
200205 try {
@@ -232,7 +237,6 @@ public SSLContext initSSLContext() throws WxPayException {
232237 }
233238 }
234239
235-
236240 /**
237241 * 初始化api v3请求头 自动签名验签
238242 * 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
@@ -266,9 +270,13 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
266270 if (!keypath .startsWith ("/" )) {
267271 keypath = "/" + keypath ;
268272 }
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 );
272280 }
273281 }
274282
@@ -277,32 +285,29 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
277285 if (!certpath .startsWith ("/" )) {
278286 certpath = "/" + certpath ;
279287 }
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 );
283295 }
284296 }
285297
286- CloseableHttpClient httpClient ;
287298 try {
288- WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder .create ();
289299 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 ();
300307 this .apiV3HttpClient = httpClient ;
308+ return httpClient ;
301309 } catch (Exception e ) {
302310 throw new WxPayException ("v3请求构造异常!" , e );
303311 }
304-
305- return httpClient ;
306-
307312 }
308313}
0 commit comments