Skip to content

Commit 3b46fae

Browse files
committed
🎨 优化微信支付v3代码,兼容java7
1 parent 9d2f90e commit 3b46fae

File tree

6 files changed

+31
-27
lines changed

6 files changed

+31
-27
lines changed

weixin-java-pay/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
<name>WxJava - PAY Java SDK</name>
1414
<description>微信支付 Java SDK</description>
1515

16-
<properties>
17-
<maven.compiler.source>1.8</maven.compiler.source>
18-
<maven.compiler.target>1.8</maven.compiler.target>
19-
</properties>
20-
2116
<dependencies>
2217
<dependency>
2318
<groupId>com.github.binarywang</groupId>
@@ -84,6 +79,11 @@
8479
<groupId>com.google.code.gson</groupId>
8580
<artifactId>gson</artifactId>
8681
</dependency>
82+
<dependency>
83+
<groupId>joda-time</groupId>
84+
<artifactId>joda-time</artifactId>
85+
<scope>compile</scope>
86+
</dependency>
8787
</dependencies>
8888

8989
<profiles>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
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;
7-
import com.github.binarywang.wxpay.v3.auth.WechatPay2Credentials;
8-
import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator;
7+
import com.github.binarywang.wxpay.v3.auth.WxPayCredentials;
8+
import com.github.binarywang.wxpay.v3.auth.WxPayValidator;
99
import com.github.binarywang.wxpay.v3.util.PemUtils;
1010
import jodd.util.ResourcesUtil;
1111
import lombok.Data;
@@ -300,8 +300,8 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
300300
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
301301
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
302302
.withWechatpay(Collections.singletonList(PemUtils.loadCertificate(certInputStream)))
303-
.withValidator(new WechatPay2Validator(new AutoUpdateCertificatesVerifier(
304-
new WechatPay2Credentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
303+
.withValidator(new WxPayValidator(new AutoUpdateCertificatesVerifier(
304+
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
305305
apiV3Key.getBytes(StandardCharsets.UTF_8))))
306306
.build();
307307
this.apiV3HttpClient = httpClient;

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

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

88
import com.github.binarywang.wxpay.v3.auth.CertificatesVerifier;
99
import com.github.binarywang.wxpay.v3.auth.PrivateKeySigner;
10-
import com.github.binarywang.wxpay.v3.auth.WechatPay2Credentials;
11-
import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator;
10+
import com.github.binarywang.wxpay.v3.auth.WxPayCredentials;
11+
import com.github.binarywang.wxpay.v3.auth.WxPayValidator;
1212
import org.apache.http.impl.client.CloseableHttpClient;
1313
import org.apache.http.impl.client.HttpClientBuilder;
1414
import org.apache.http.impl.execchain.ClientExecChain;
@@ -37,7 +37,7 @@ public static WxPayV3HttpClientBuilder create() {
3737

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

@@ -47,7 +47,7 @@ public WxPayV3HttpClientBuilder withCredentials(Credentials credentials) {
4747
}
4848

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

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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.Validator;
67
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
78
import com.github.binarywang.wxpay.v3.util.AesUtils;
89
import com.github.binarywang.wxpay.v3.util.PemUtils;
@@ -13,6 +14,8 @@
1314
import org.apache.http.client.methods.HttpGet;
1415
import org.apache.http.impl.client.CloseableHttpClient;
1516
import org.apache.http.util.EntityUtils;
17+
import org.joda.time.Instant;
18+
import org.joda.time.Minutes;
1619

1720
import java.io.ByteArrayInputStream;
1821
import java.io.IOException;
@@ -21,8 +24,6 @@
2124
import java.security.cert.CertificateExpiredException;
2225
import java.security.cert.CertificateNotYetValidException;
2326
import java.security.cert.X509Certificate;
24-
import java.time.Duration;
25-
import java.time.Instant;
2627
import java.util.ArrayList;
2728
import java.util.List;
2829
import java.util.concurrent.locks.ReentrantLock;
@@ -98,7 +99,7 @@ public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key,
9899

99100
@Override
100101
public boolean verify(String serialNumber, byte[] message, String signature) {
101-
if (instant == null || Duration.between(instant, Instant.now()).toMinutes() >= minutesInterval) {
102+
if (instant == null || Minutes.minutesBetween(instant, Instant.now()).getMinutes() >= minutesInterval) {
102103
if (lock.tryLock()) {
103104
try {
104105
autoUpdateCert();
@@ -117,7 +118,12 @@ public boolean verify(String serialNumber, byte[] message, String signature) {
117118
private void autoUpdateCert() throws IOException, GeneralSecurityException {
118119
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
119120
.withCredentials(credentials)
120-
.withValidator(verifier == null ? (response) -> true : new WechatPay2Validator(verifier))
121+
.withValidator(verifier == null ? new Validator() {
122+
@Override
123+
public boolean validate(CloseableHttpResponse response) throws IOException {
124+
return true;
125+
}
126+
} : new WxPayValidator(verifier))
121127
.build();
122128

123129
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WechatPay2Credentials.java renamed to weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
import java.security.SecureRandom;
77

88
import com.github.binarywang.wxpay.v3.Credentials;
9+
import lombok.extern.slf4j.Slf4j;
910
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
1011
import org.apache.http.client.methods.HttpUriRequest;
1112
import org.apache.http.util.EntityUtils;
1213
import org.slf4j.Logger;
1314
import org.slf4j.LoggerFactory;
1415

15-
public class WechatPay2Credentials implements Credentials {
16-
private static final Logger log = LoggerFactory.getLogger(WechatPay2Credentials.class);
17-
16+
@Slf4j
17+
public class WxPayCredentials implements Credentials {
1818
private static final String SYMBOLS =
1919
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
2020
private static final SecureRandom RANDOM = new SecureRandom();
2121
protected String merchantId;
2222
protected Signer signer;
2323

24-
public WechatPay2Credentials(String merchantId, Signer signer) {
24+
public WxPayCredentials(String merchantId, Signer signer) {
2525
this.merchantId = merchantId;
2626
this.signer = signer;
2727
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WechatPay2Validator.java renamed to weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
import java.io.IOException;
55

66
import com.github.binarywang.wxpay.v3.Validator;
7+
import lombok.extern.slf4j.Slf4j;
78
import org.apache.http.Header;
89
import org.apache.http.HttpEntity;
910
import org.apache.http.client.methods.CloseableHttpResponse;
1011
import org.apache.http.util.EntityUtils;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
13-
14-
public class WechatPay2Validator implements Validator {
15-
16-
private static final Logger log = LoggerFactory.getLogger(WechatPay2Validator.class);
17-
14+
@Slf4j
15+
public class WxPayValidator implements Validator {
1816
private Verifier verifier;
1917

20-
public WechatPay2Validator(Verifier verifier) {
18+
public WxPayValidator(Verifier verifier) {
2119
this.verifier = verifier;
2220
}
2321

0 commit comments

Comments
 (0)