Skip to content

Commit

Permalink
🎨 优化微信支付v3代码,兼容java7
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed May 24, 2020
1 parent 9d2f90e commit 3b46fae
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
10 changes: 5 additions & 5 deletions weixin-java-pay/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
<name>WxJava - PAY Java SDK</name>
<description>微信支付 Java SDK</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.github.binarywang</groupId>
Expand Down Expand Up @@ -84,6 +79,11 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
import com.github.binarywang.wxpay.v3.auth.AutoUpdateCertificatesVerifier;
import com.github.binarywang.wxpay.v3.auth.PrivateKeySigner;
import com.github.binarywang.wxpay.v3.auth.WechatPay2Credentials;
import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator;
import com.github.binarywang.wxpay.v3.auth.WxPayCredentials;
import com.github.binarywang.wxpay.v3.auth.WxPayValidator;
import com.github.binarywang.wxpay.v3.util.PemUtils;
import jodd.util.ResourcesUtil;
import lombok.Data;
Expand Down Expand Up @@ -300,8 +300,8 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
.withWechatpay(Collections.singletonList(PemUtils.loadCertificate(certInputStream)))
.withValidator(new WechatPay2Validator(new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
.withValidator(new WxPayValidator(new AutoUpdateCertificatesVerifier(
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8))))
.build();
this.apiV3HttpClient = httpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import com.github.binarywang.wxpay.v3.auth.CertificatesVerifier;
import com.github.binarywang.wxpay.v3.auth.PrivateKeySigner;
import com.github.binarywang.wxpay.v3.auth.WechatPay2Credentials;
import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator;
import com.github.binarywang.wxpay.v3.auth.WxPayCredentials;
import com.github.binarywang.wxpay.v3.auth.WxPayValidator;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.execchain.ClientExecChain;
Expand Down Expand Up @@ -37,7 +37,7 @@ public static WxPayV3HttpClientBuilder create() {

public WxPayV3HttpClientBuilder withMerchant(String merchantId, String serialNo, PrivateKey privateKey) {
this.credentials =
new WechatPay2Credentials(merchantId, new PrivateKeySigner(serialNo, privateKey));
new WxPayCredentials(merchantId, new PrivateKeySigner(serialNo, privateKey));
return this;
}

Expand All @@ -47,7 +47,7 @@ public WxPayV3HttpClientBuilder withCredentials(Credentials credentials) {
}

public WxPayV3HttpClientBuilder withWechatpay(List<X509Certificate> certificates) {
this.validator = new WechatPay2Validator(new CertificatesVerifier(certificates));
this.validator = new WxPayValidator(new CertificatesVerifier(certificates));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.binarywang.wxpay.v3.Credentials;
import com.github.binarywang.wxpay.v3.Validator;
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
import com.github.binarywang.wxpay.v3.util.AesUtils;
import com.github.binarywang.wxpay.v3.util.PemUtils;
Expand All @@ -13,6 +14,8 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.joda.time.Instant;
import org.joda.time.Minutes;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -21,8 +24,6 @@
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -98,7 +99,7 @@ public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key,

@Override
public boolean verify(String serialNumber, byte[] message, String signature) {
if (instant == null || Duration.between(instant, Instant.now()).toMinutes() >= minutesInterval) {
if (instant == null || Minutes.minutesBetween(instant, Instant.now()).getMinutes() >= minutesInterval) {
if (lock.tryLock()) {
try {
autoUpdateCert();
Expand All @@ -117,7 +118,12 @@ public boolean verify(String serialNumber, byte[] message, String signature) {
private void autoUpdateCert() throws IOException, GeneralSecurityException {
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
.withCredentials(credentials)
.withValidator(verifier == null ? (response) -> true : new WechatPay2Validator(verifier))
.withValidator(verifier == null ? new Validator() {
@Override
public boolean validate(CloseableHttpResponse response) throws IOException {
return true;
}
} : new WxPayValidator(verifier))
.build();

HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
import java.security.SecureRandom;

import com.github.binarywang.wxpay.v3.Credentials;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WechatPay2Credentials implements Credentials {
private static final Logger log = LoggerFactory.getLogger(WechatPay2Credentials.class);

@Slf4j
public class WxPayCredentials implements Credentials {
private static final String SYMBOLS =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final SecureRandom RANDOM = new SecureRandom();
protected String merchantId;
protected Signer signer;

public WechatPay2Credentials(String merchantId, Signer signer) {
public WxPayCredentials(String merchantId, Signer signer) {
this.merchantId = merchantId;
this.signer = signer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import java.io.IOException;

import com.github.binarywang.wxpay.v3.Validator;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WechatPay2Validator implements Validator {

private static final Logger log = LoggerFactory.getLogger(WechatPay2Validator.class);

@Slf4j
public class WxPayValidator implements Validator {
private Verifier verifier;

public WechatPay2Validator(Verifier verifier) {
public WxPayValidator(Verifier verifier) {
this.verifier = verifier;
}

Expand Down

0 comments on commit 3b46fae

Please sign in to comment.