Parcourir la source

🎨 优化微信支付v3代码,兼容java7

dev1
Binary Wang il y a 5 ans
Parent
révision
3b46faeb89
6 fichiers modifiés avec 31 ajouts et 27 suppressions
  1. +5
    -5
      weixin-java-pay/pom.xml
  2. +4
    -4
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java
  3. +4
    -4
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3HttpClientBuilder.java
  4. +10
    -4
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java
  5. +4
    -4
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java
  6. +4
    -6
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java

+ 5
- 5
weixin-java-pay/pom.xml Voir le fichier

@@ -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
- 4
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java Voir le fichier

@@ -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;


+ 4
- 4
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3HttpClientBuilder.java Voir le fichier

@@ -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;
} }




+ 10
- 4
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java Voir le fichier

@@ -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);


weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WechatPay2Credentials.java → weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayCredentials.java Voir le fichier

@@ -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;
} }

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WechatPay2Validator.java → weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java Voir le fichier

@@ -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;
} }



Chargement…
Annuler
Enregistrer