mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 07:23:01 +08:00
🎨 优化微信支付v3代码,兼容java7
This commit is contained in:
parent
9d2f90ec4f
commit
3b46faeb89
@ -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>
|
||||
@ -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>
|
||||
|
@ -4,8 +4,8 @@ import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
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;
|
||||
@ -300,8 +300,8 @@ public class WxPayConfig {
|
||||
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;
|
||||
|
@ -7,8 +7,8 @@ import java.util.List;
|
||||
|
||||
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;
|
||||
@ -37,7 +37,7 @@ public class WxPayV3HttpClientBuilder extends HttpClientBuilder {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -47,7 +47,7 @@ public class WxPayV3HttpClientBuilder extends HttpClientBuilder {
|
||||
}
|
||||
|
||||
public WxPayV3HttpClientBuilder withWechatpay(List<X509Certificate> certificates) {
|
||||
this.validator = new WechatPay2Validator(new CertificatesVerifier(certificates));
|
||||
this.validator = new WxPayValidator(new CertificatesVerifier(certificates));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package com.github.binarywang.wxpay.v3.auth;
|
||||
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;
|
||||
@ -13,6 +14,8 @@ import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
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;
|
||||
@ -21,8 +24,6 @@ import java.security.GeneralSecurityException;
|
||||
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;
|
||||
@ -98,7 +99,7 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
||||
|
||||
@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();
|
||||
@ -117,7 +118,12 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
||||
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);
|
||||
|
@ -6,22 +6,22 @@ import java.net.URI;
|
||||
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;
|
||||
}
|
@ -4,20 +4,18 @@ package com.github.binarywang.wxpay.v3.auth;
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user