Skip to content

Commit 9d2f90e

Browse files
committed
🎨 优化代码
1 parent ebc8f0e commit 9d2f90e

File tree

6 files changed

+57
-58
lines changed

6 files changed

+57
-58
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayFaceAuthInfoResult.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
@NoArgsConstructor
2222
@XStreamAlias("xml")
2323
public class WxPayFaceAuthInfoResult extends BaseWxPayResult implements Serializable {
24-
2524
private static final long serialVersionUID = -65138145275211272L;
2625

2726
/**

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

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.github.binarywang.wxpay.config;
22

33
import com.github.binarywang.wxpay.exception.WxPayException;
4-
import com.github.binarywang.wxpay.v3.WechatPayHttpClientBuilder;
4+
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
55
import com.github.binarywang.wxpay.v3.auth.AutoUpdateCertificatesVerifier;
66
import com.github.binarywang.wxpay.v3.auth.PrivateKeySigner;
77
import com.github.binarywang.wxpay.v3.auth.WechatPay2Credentials;
88
import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator;
99
import com.github.binarywang.wxpay.v3.util.PemUtils;
10+
import jodd.util.ResourcesUtil;
1011
import lombok.Data;
1112
import org.apache.commons.io.IOUtils;
1213
import org.apache.commons.lang3.RegExUtils;
@@ -20,8 +21,7 @@
2021
import java.nio.charset.StandardCharsets;
2122
import java.security.KeyStore;
2223
import java.security.PrivateKey;
23-
import java.security.cert.X509Certificate;
24-
import java.util.ArrayList;
24+
import java.util.Collections;
2525

2626
/**
2727
* 微信支付配置
@@ -31,6 +31,8 @@
3131
@Data
3232
public 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
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import java.nio.charset.StandardCharsets;
55
import javax.net.ssl.SSLContext;
66

7-
import com.github.binarywang.wxpay.bean.request.WxPayQueryCommentRequest;
8-
import com.github.binarywang.wxpay.bean.request.WxPayRedpackQueryRequest;
9-
import com.github.binarywang.wxpay.bean.result.WxPayCommonResult;
10-
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
117
import org.apache.commons.lang3.StringUtils;
128

139
import com.github.binarywang.wxpay.bean.WxPayApiData;
@@ -20,10 +16,6 @@
2016
import jodd.http.net.SSLSocketHttpConnectionProvider;
2117
import jodd.http.net.SocketHttpConnectionProvider;
2218
import jodd.util.Base64;
23-
import org.apache.commons.lang3.StringUtils;
24-
25-
import javax.net.ssl.SSLContext;
26-
import java.nio.charset.StandardCharsets;
2719

2820
/**
2921
* 微信支付请求实现类,jodd-http实现.

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WechatPayHttpClientBuilder.java renamed to weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3HttpClientBuilder.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@
1313
import org.apache.http.impl.client.HttpClientBuilder;
1414
import org.apache.http.impl.execchain.ClientExecChain;
1515

16-
public class WechatPayHttpClientBuilder extends HttpClientBuilder {
16+
public class WxPayV3HttpClientBuilder extends HttpClientBuilder {
1717
private Credentials credentials;
1818
private Validator validator;
1919

20-
static final String os = System.getProperty("os.name") + "/" + System.getProperty("os.version");
21-
static final String version = System.getProperty("java.version");
20+
static final String OS = System.getProperty("os.name") + "/" + System.getProperty("os.version");
21+
static final String VERSION = System.getProperty("java.version");
2222

23-
private WechatPayHttpClientBuilder() {
23+
private WxPayV3HttpClientBuilder() {
2424
super();
2525

2626
String userAgent = String.format(
2727
"WechatPay-Apache-HttpClient/%s (%s) Java/%s",
2828
getClass().getPackage().getImplementationVersion(),
29-
os,
30-
version == null ? "Unknown" : version);
29+
OS,
30+
VERSION == null ? "Unknown" : VERSION);
3131
setUserAgent(userAgent);
3232
}
3333

34-
public static WechatPayHttpClientBuilder create() {
35-
return new WechatPayHttpClientBuilder();
34+
public static WxPayV3HttpClientBuilder create() {
35+
return new WxPayV3HttpClientBuilder();
3636
}
3737

38-
public WechatPayHttpClientBuilder withMerchant(String merchantId, String serialNo, PrivateKey privateKey) {
38+
public WxPayV3HttpClientBuilder withMerchant(String merchantId, String serialNo, PrivateKey privateKey) {
3939
this.credentials =
4040
new WechatPay2Credentials(merchantId, new PrivateKeySigner(serialNo, privateKey));
4141
return this;
4242
}
4343

44-
public WechatPayHttpClientBuilder withCredentials(Credentials credentials) {
44+
public WxPayV3HttpClientBuilder withCredentials(Credentials credentials) {
4545
this.credentials = credentials;
4646
return this;
4747
}
4848

49-
public WechatPayHttpClientBuilder withWechatpay(List<X509Certificate> certificates) {
49+
public WxPayV3HttpClientBuilder withWechatpay(List<X509Certificate> certificates) {
5050
this.validator = new WechatPay2Validator(new CertificatesVerifier(certificates));
5151
return this;
5252
}
5353

54-
public WechatPayHttpClientBuilder withValidator(Validator validator) {
54+
public WxPayV3HttpClientBuilder withValidator(Validator validator) {
5555
this.validator = validator;
5656
return this;
5757
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.github.binarywang.wxpay.v3.Credentials;
6-
import com.github.binarywang.wxpay.v3.WechatPayHttpClientBuilder;
6+
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
77
import com.github.binarywang.wxpay.v3.util.AesUtils;
88
import com.github.binarywang.wxpay.v3.util.PemUtils;
99
import lombok.Getter;
@@ -115,7 +115,7 @@ public boolean verify(String serialNumber, byte[] message, String signature) {
115115
}
116116

117117
private void autoUpdateCert() throws IOException, GeneralSecurityException {
118-
CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
118+
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
119119
.withCredentials(credentials)
120120
.withValidator(verifier == null ? (response) -> true : new WechatPay2Validator(verifier))
121121
.build();

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.testng.annotations.Test;
44

5-
import static org.testng.Assert.*;
6-
75
/**
86
* <pre>
97
* Created by BinaryWang on 2017/6/18.
@@ -12,12 +10,12 @@
1210
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1311
*/
1412
public class WxPayConfigTest {
15-
private WxPayConfig payConfig = new WxPayConfig();
13+
private final WxPayConfig payConfig = new WxPayConfig();
1614

1715
@Test
1816
public void testInitSSLContext_classpath() throws Exception {
1917
payConfig.setMchId("123");
20-
payConfig.setKeyPath("classpath:/abc.p12");
18+
payConfig.setKeyPath("classpath:/dlt.p12");
2119
payConfig.initSSLContext();
2220
}
2321

@@ -28,4 +26,9 @@ public void testInitSSLContext_http() throws Exception {
2826
payConfig.initSSLContext();
2927
}
3028

29+
@Test
30+
public void testInitSSLContext() throws Exception {
31+
this.testInitSSLContext_classpath();
32+
this.testInitSSLContext_http();
33+
}
3134
}

0 commit comments

Comments
 (0)