From 10414d997179448fec71a7ab70d49d7a6d6f0e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E9=95=87=E6=B6=9B?= Date: Sun, 23 Oct 2022 14:38:13 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E6=9C=AA=E6=AD=A3=E7=A1=AE=E9=85=8D=E7=BD=AE=20apiV3K?= =?UTF-8?q?ey=20=E6=97=B6=EF=BC=8C173=20=E8=A1=8C=E8=AF=81=E4=B9=A6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=A4=B1=E8=B4=A5=EF=BC=8C=E6=9C=AA=E5=81=9A?= =?UTF-8?q?=20throw=20=E7=AD=89=E9=98=BB=E6=96=AD=E5=8A=A8=E4=BD=9C?= =?UTF-8?q?=EF=BC=8C=E5=AF=BC=E8=87=B4=20verifier=20=E6=9C=AA=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=EF=BC=8C=E5=90=8E=E7=BB=AD=E5=85=B6=E5=AE=83=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=BD=BF=E7=94=A8=E5=B0=86=E5=87=BA=E7=8E=B0=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/AutoUpdateCertificatesVerifier.java | 28 ++++++---- .../AutoUpdateCertificatesVerifierTest.java | 55 +++++++++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifierTest.java diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java index f5219e500..7b39aad36 100755 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java @@ -3,23 +3,22 @@ package com.github.binarywang.wxpay.v3.auth; import com.github.binarywang.wxpay.config.WxPayHttpProxy; import com.github.binarywang.wxpay.util.HttpProxyUtils; 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; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxRuntimeException; import me.chanjar.weixin.common.util.json.GsonParser; +import org.apache.http.HttpStatus; 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 java.time.Instant; -import java.time.temporal.ChronoUnit; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -28,9 +27,12 @@ import java.security.GeneralSecurityException; import java.security.cert.CertificateExpiredException; import java.security.cert.CertificateNotYetValidException; import java.security.cert.X509Certificate; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.concurrent.locks.ReentrantLock; /** @@ -141,12 +143,7 @@ public class AutoUpdateCertificatesVerifier implements Verifier { private void autoUpdateCert() throws IOException, GeneralSecurityException { WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create() .withCredentials(credentials) - .withValidator(verifier == null ? new Validator() { - @Override - public boolean validate(CloseableHttpResponse response) throws IOException { - return true; - } - } : new WxPayValidator(verifier)); + .withValidator(verifier == null ? response -> true : new WxPayValidator(verifier)); //调用自定义扩展设置设置HTTP PROXY对象 HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,this.wxPayHttpProxy); @@ -162,15 +159,15 @@ public class AutoUpdateCertificatesVerifier implements Verifier { CloseableHttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); String body = EntityUtils.toString(response.getEntity()); - if (statusCode == 200) { + if (statusCode == HttpStatus.SC_OK) { List newCertList = deserializeToCerts(apiV3Key, body); if (newCertList.isEmpty()) { - log.warn("Cert list is empty"); - return; + throw new WxRuntimeException("Cert list is empty"); } this.verifier = new CertificatesVerifier(newCertList); } else { log.warn("Auto update cert failed, statusCode = " + statusCode + ",body = " + body); + throw new WxRuntimeException(this.getErrorMsg(body)); } } @@ -223,4 +220,11 @@ public class AutoUpdateCertificatesVerifier implements Verifier { return verifier.getValidCertificate(); } + private String getErrorMsg(String body) { + return Optional + .ofNullable(GsonParser.parse(body).getAsJsonObject()) + .map(resp -> resp.get("message")) + .map(JsonElement::getAsString) + .orElse("update cert failed"); + } } diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifierTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifierTest.java new file mode 100644 index 000000000..e04ecf28e --- /dev/null +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifierTest.java @@ -0,0 +1,55 @@ +package com.github.binarywang.wxpay.v3.auth; + +import com.github.binarywang.wxpay.bean.merchanttransfer.TransferCreateRequest; +import com.github.binarywang.wxpay.bean.merchanttransfer.TransferCreateRequest.TransferDetailList; +import com.github.binarywang.wxpay.bean.merchanttransfer.TransferCreateResult; +import com.github.binarywang.wxpay.exception.WxPayException; +import com.github.binarywang.wxpay.service.WxPayService; +import com.github.binarywang.wxpay.testbase.ApiTestModule; +import com.google.inject.Inject; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.util.Asserts; +import org.assertj.core.util.Lists; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +/** + * 商家转账到零钱(直连商户)- 商户号配置信息错误时健壮性判断单元测试 + * @author imyzt + * created on 2022/10/23 + */ +@Slf4j +@Test +@Guice(modules = ApiTestModule.class) +public class AutoUpdateCertificatesVerifierTest { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Inject + private WxPayService payService; + + @Test + public void testVerify() throws WxPayException { + TransferDetailList transferDetailList = new TransferDetailList(); + transferDetailList.setOutDetailNo("test") + .setOpenid("test") + .setTransferAmount(1) + .setOutDetailNo("test") + .setUserName("test"); + TransferCreateRequest req = TransferCreateRequest.builder() + .appid("wxd930ea5d5a258f4f") + .batchName("test") + .outBatchNo("") + .totalAmount(1) + .totalNum(1) + .transferDetailList(Lists.newArrayList(transferDetailList)) + .build(); + TransferCreateResult transfer = payService.getMerchantTransferService().createTransfer(req); + Asserts.notNull(transfer, "transfer"); + + // 商户未申请过证书。请到商户平台上申请证书授权机构颁发的证书。详情可参考:http://kf.qq.com/faq/180824JvUZ3i180824YvMNJj.html + + } +}