@@ -13,11 +13,6 @@ | |||||
<name>WxJava - PAY Java SDK</name> | <name>WxJava - PAY Java SDK</name> | ||||
<description>微信支付 Java SDK</description> | <description>微信支付 Java SDK</description> | ||||
<properties> | |||||
<maven.compiler.source>1.8</maven.compiler.source> | |||||
<maven.compiler.target>1.8</maven.compiler.target> | |||||
</properties> | |||||
<dependencies> | <dependencies> | ||||
<dependency> | <dependency> | ||||
<groupId>com.github.binarywang</groupId> | <groupId>com.github.binarywang</groupId> | ||||
@@ -84,6 +79,11 @@ | |||||
<groupId>com.google.code.gson</groupId> | <groupId>com.google.code.gson</groupId> | ||||
<artifactId>gson</artifactId> | <artifactId>gson</artifactId> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>joda-time</groupId> | |||||
<artifactId>joda-time</artifactId> | |||||
<scope>compile</scope> | |||||
</dependency> | |||||
</dependencies> | </dependencies> | ||||
<profiles> | <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.WxPayV3HttpClientBuilder; | ||||
import com.github.binarywang.wxpay.v3.auth.AutoUpdateCertificatesVerifier; | import com.github.binarywang.wxpay.v3.auth.AutoUpdateCertificatesVerifier; | ||||
import com.github.binarywang.wxpay.v3.auth.PrivateKeySigner; | 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 com.github.binarywang.wxpay.v3.util.PemUtils; | ||||
import jodd.util.ResourcesUtil; | import jodd.util.ResourcesUtil; | ||||
import lombok.Data; | import lombok.Data; | ||||
@@ -300,8 +300,8 @@ public class WxPayConfig { | |||||
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create() | CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create() | ||||
.withMerchant(mchId, certSerialNo, merchantPrivateKey) | .withMerchant(mchId, certSerialNo, merchantPrivateKey) | ||||
.withWechatpay(Collections.singletonList(PemUtils.loadCertificate(certInputStream))) | .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)))) | apiV3Key.getBytes(StandardCharsets.UTF_8)))) | ||||
.build(); | .build(); | ||||
this.apiV3HttpClient = httpClient; | 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.CertificatesVerifier; | ||||
import com.github.binarywang.wxpay.v3.auth.PrivateKeySigner; | 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.CloseableHttpClient; | ||||
import org.apache.http.impl.client.HttpClientBuilder; | import org.apache.http.impl.client.HttpClientBuilder; | ||||
import org.apache.http.impl.execchain.ClientExecChain; | 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) { | public WxPayV3HttpClientBuilder withMerchant(String merchantId, String serialNo, PrivateKey privateKey) { | ||||
this.credentials = | this.credentials = | ||||
new WechatPay2Credentials(merchantId, new PrivateKeySigner(serialNo, privateKey)); | |||||
new WxPayCredentials(merchantId, new PrivateKeySigner(serialNo, privateKey)); | |||||
return this; | return this; | ||||
} | } | ||||
@@ -47,7 +47,7 @@ public class WxPayV3HttpClientBuilder extends HttpClientBuilder { | |||||
} | } | ||||
public WxPayV3HttpClientBuilder withWechatpay(List<X509Certificate> certificates) { | public WxPayV3HttpClientBuilder withWechatpay(List<X509Certificate> certificates) { | ||||
this.validator = new WechatPay2Validator(new CertificatesVerifier(certificates)); | |||||
this.validator = new WxPayValidator(new CertificatesVerifier(certificates)); | |||||
return this; | return this; | ||||
} | } | ||||
@@ -3,6 +3,7 @@ package com.github.binarywang.wxpay.v3.auth; | |||||
import com.fasterxml.jackson.databind.JsonNode; | import com.fasterxml.jackson.databind.JsonNode; | ||||
import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||||
import com.github.binarywang.wxpay.v3.Credentials; | 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.WxPayV3HttpClientBuilder; | ||||
import com.github.binarywang.wxpay.v3.util.AesUtils; | import com.github.binarywang.wxpay.v3.util.AesUtils; | ||||
import com.github.binarywang.wxpay.v3.util.PemUtils; | 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.client.methods.HttpGet; | ||||
import org.apache.http.impl.client.CloseableHttpClient; | import org.apache.http.impl.client.CloseableHttpClient; | ||||
import org.apache.http.util.EntityUtils; | import org.apache.http.util.EntityUtils; | ||||
import org.joda.time.Instant; | |||||
import org.joda.time.Minutes; | |||||
import java.io.ByteArrayInputStream; | import java.io.ByteArrayInputStream; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
@@ -21,8 +24,6 @@ import java.security.GeneralSecurityException; | |||||
import java.security.cert.CertificateExpiredException; | import java.security.cert.CertificateExpiredException; | ||||
import java.security.cert.CertificateNotYetValidException; | import java.security.cert.CertificateNotYetValidException; | ||||
import java.security.cert.X509Certificate; | import java.security.cert.X509Certificate; | ||||
import java.time.Duration; | |||||
import java.time.Instant; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.concurrent.locks.ReentrantLock; | import java.util.concurrent.locks.ReentrantLock; | ||||
@@ -98,7 +99,7 @@ public class AutoUpdateCertificatesVerifier implements Verifier { | |||||
@Override | @Override | ||||
public boolean verify(String serialNumber, byte[] message, String signature) { | 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()) { | if (lock.tryLock()) { | ||||
try { | try { | ||||
autoUpdateCert(); | autoUpdateCert(); | ||||
@@ -117,7 +118,12 @@ public class AutoUpdateCertificatesVerifier implements Verifier { | |||||
private void autoUpdateCert() throws IOException, GeneralSecurityException { | private void autoUpdateCert() throws IOException, GeneralSecurityException { | ||||
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create() | CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create() | ||||
.withCredentials(credentials) | .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(); | .build(); | ||||
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH); | HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH); | ||||
@@ -6,22 +6,22 @@ import java.net.URI; | |||||
import java.security.SecureRandom; | import java.security.SecureRandom; | ||||
import com.github.binarywang.wxpay.v3.Credentials; | 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.HttpEntityEnclosingRequestBase; | ||||
import org.apache.http.client.methods.HttpUriRequest; | import org.apache.http.client.methods.HttpUriRequest; | ||||
import org.apache.http.util.EntityUtils; | import org.apache.http.util.EntityUtils; | ||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | 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 = | private static final String SYMBOLS = | ||||
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||||
private static final SecureRandom RANDOM = new SecureRandom(); | private static final SecureRandom RANDOM = new SecureRandom(); | ||||
protected String merchantId; | protected String merchantId; | ||||
protected Signer signer; | protected Signer signer; | ||||
public WechatPay2Credentials(String merchantId, Signer signer) { | |||||
public WxPayCredentials(String merchantId, Signer signer) { | |||||
this.merchantId = merchantId; | this.merchantId = merchantId; | ||||
this.signer = signer; | this.signer = signer; | ||||
} | } |
@@ -4,20 +4,18 @@ package com.github.binarywang.wxpay.v3.auth; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import com.github.binarywang.wxpay.v3.Validator; | import com.github.binarywang.wxpay.v3.Validator; | ||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.apache.http.Header; | import org.apache.http.Header; | ||||
import org.apache.http.HttpEntity; | import org.apache.http.HttpEntity; | ||||
import org.apache.http.client.methods.CloseableHttpResponse; | import org.apache.http.client.methods.CloseableHttpResponse; | ||||
import org.apache.http.util.EntityUtils; | import org.apache.http.util.EntityUtils; | ||||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
import org.slf4j.LoggerFactory; | 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; | private Verifier verifier; | ||||
public WechatPay2Validator(Verifier verifier) { | |||||
public WxPayValidator(Verifier verifier) { | |||||
this.verifier = verifier; | this.verifier = verifier; | ||||
} | } | ||||