Skip to content

Commit 22ec3f0

Browse files
committed
🎨 增加注释防止误解,同时优化重构代码
1 parent 410cc9d commit 22ec3f0

File tree

1 file changed

+39
-27
lines changed
  • weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config

1 file changed

+39
-27
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
77
import com.github.binarywang.wxpay.v3.auth.*;
88
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;
920
import java.io.*;
1021
import java.net.URL;
1122
import java.nio.charset.StandardCharsets;
@@ -16,16 +27,6 @@
1627
import java.security.cert.X509Certificate;
1728
import java.util.Base64;
1829
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;
2930

3031
/**
3132
* 微信支付配置
@@ -310,8 +311,8 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
310311
PublicKey publicKey = null;
311312
if (this.getPublicKeyString() != null || this.getPublicKeyPath() != null || this.publicKeyContent != null) {
312313
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")) {
315316
publicKey = PemUtils.loadPublicKey(pubInputStream);
316317
}
317318
}
@@ -322,10 +323,10 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
322323
Verifier certificatesVerifier;
323324
if (publicKey == null) {
324325
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);
329330
} else {
330331
certificatesVerifier = new PublicCertificateVerifier(publicKey, publicKeyId);
331332
}
@@ -366,21 +367,32 @@ private WxPayHttpProxy getWxPayHttpProxy() {
366367
return null;
367368
}
368369

370+
/**
371+
* 从指定参数加载输入流
372+
*
373+
* @param configString 证书内容进行Base64加密后的字符串
374+
* @param configPath 证书路径
375+
* @param configContent 证书内容的字节数组
376+
* @param certName 证书的标识
377+
* @return 输入流
378+
* @throws WxPayException 异常
379+
*/
369380
private InputStream loadConfigInputStream(String configString, String configPath, byte[] configContent,
370-
String fileName) throws WxPayException {
371-
InputStream inputStream;
381+
String certName) throws WxPayException {
372382
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)) {
375387
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);
382389
}
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);
384396
}
385397

386398

0 commit comments

Comments
 (0)