6
6
import com .github .binarywang .wxpay .v3 .WxPayV3HttpClientBuilder ;
7
7
import com .github .binarywang .wxpay .v3 .auth .*;
8
8
import com .github .binarywang .wxpay .v3 .util .PemUtils ;
9
+ import lombok .Data ;
10
+ import lombok .EqualsAndHashCode ;
11
+ import lombok .SneakyThrows ;
12
+ import lombok .ToString ;
13
+ import lombok .extern .slf4j .Slf4j ;
14
+ import org .apache .commons .lang3 .RegExUtils ;
15
+ import org .apache .commons .lang3 .StringUtils ;
16
+ import org .apache .http .impl .client .CloseableHttpClient ;
17
+ import org .apache .http .ssl .SSLContexts ;
18
+
19
+ import javax .net .ssl .SSLContext ;
9
20
import java .io .*;
10
21
import java .net .URL ;
11
22
import java .nio .charset .StandardCharsets ;
16
27
import java .security .cert .X509Certificate ;
17
28
import java .util .Base64 ;
18
29
import java .util .Optional ;
19
- import javax .net .ssl .SSLContext ;
20
- import lombok .Data ;
21
- import lombok .EqualsAndHashCode ;
22
- import lombok .SneakyThrows ;
23
- import lombok .ToString ;
24
- import lombok .extern .slf4j .Slf4j ;
25
- import org .apache .commons .lang3 .RegExUtils ;
26
- import org .apache .commons .lang3 .StringUtils ;
27
- import org .apache .http .impl .client .CloseableHttpClient ;
28
- import org .apache .http .ssl .SSLContexts ;
29
30
30
31
/**
31
32
* 微信支付配置
@@ -310,8 +311,8 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
310
311
PublicKey publicKey = null ;
311
312
if (this .getPublicKeyString () != null || this .getPublicKeyPath () != null || this .publicKeyContent != null ) {
312
313
try (InputStream pubInputStream =
313
- this .loadConfigInputStream (this .getPublicKeyString (), this .getPublicKeyPath (),
314
- this .publicKeyContent , "publicKeyPath" )) {
314
+ this .loadConfigInputStream (this .getPublicKeyString (), this .getPublicKeyPath (),
315
+ this .publicKeyContent , "publicKeyPath" )) {
315
316
publicKey = PemUtils .loadPublicKey (pubInputStream );
316
317
}
317
318
}
@@ -322,10 +323,10 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
322
323
Verifier certificatesVerifier ;
323
324
if (publicKey == null ) {
324
325
certificatesVerifier =
325
- new AutoUpdateCertificatesVerifier (
326
- new WxPayCredentials (mchId , new PrivateKeySigner (certSerialNo , merchantPrivateKey )),
327
- this .getApiV3Key ().getBytes (StandardCharsets .UTF_8 ), this .getCertAutoUpdateTime (),
328
- this .getPayBaseUrl (), wxPayHttpProxy );
326
+ new AutoUpdateCertificatesVerifier (
327
+ new WxPayCredentials (mchId , new PrivateKeySigner (certSerialNo , merchantPrivateKey )),
328
+ this .getApiV3Key ().getBytes (StandardCharsets .UTF_8 ), this .getCertAutoUpdateTime (),
329
+ this .getPayBaseUrl (), wxPayHttpProxy );
329
330
} else {
330
331
certificatesVerifier = new PublicCertificateVerifier (publicKey , publicKeyId );
331
332
}
@@ -366,21 +367,32 @@ private WxPayHttpProxy getWxPayHttpProxy() {
366
367
return null ;
367
368
}
368
369
370
+ /**
371
+ * 从指定参数加载输入流
372
+ *
373
+ * @param configString 证书内容进行Base64加密后的字符串
374
+ * @param configPath 证书路径
375
+ * @param configContent 证书内容的字节数组
376
+ * @param certName 证书的标识
377
+ * @return 输入流
378
+ * @throws WxPayException 异常
379
+ */
369
380
private InputStream loadConfigInputStream (String configString , String configPath , byte [] configContent ,
370
- String fileName ) throws WxPayException {
371
- InputStream inputStream ;
381
+ String certName ) throws WxPayException {
372
382
if (configContent != null ) {
373
- inputStream = new ByteArrayInputStream (configContent );
374
- } else if (StringUtils .isNotEmpty (configString )) {
383
+ return new ByteArrayInputStream (configContent );
384
+ }
385
+
386
+ if (StringUtils .isNotEmpty (configString )) {
375
387
configContent = Base64 .getDecoder ().decode (configString );
376
- inputStream = new ByteArrayInputStream (configContent );
377
- } else {
378
- if (StringUtils .isBlank (configPath )) {
379
- throw new WxPayException ("请确保证书文件地址【" + fileName + "】或者内容已配置" );
380
- }
381
- inputStream = this .loadConfigInputStream (configPath );
388
+ return new ByteArrayInputStream (configContent );
382
389
}
383
- return inputStream ;
390
+
391
+ if (StringUtils .isBlank (configPath )) {
392
+ throw new WxPayException (String .format ("请确保【%s】的文件地址【%s】存在" , certName , configPath ));
393
+ }
394
+
395
+ return this .loadConfigInputStream (configPath );
384
396
}
385
397
386
398
0 commit comments