From be7b151a7da0fdfd98f082cf89d567e8a363186f Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Wed, 12 Aug 2026 13:34:26 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#4090=20=E3=80=90=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E3=80=91=E5=85=81=E8=AE=B8V2=E6=B2=99?= =?UTF-8?q?=E7=AE=B1=E4=B8=8EV3=E9=85=8D=E7=BD=AE=E5=85=B1=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/BaseWxPayServiceImpl.java | 9 ++-- .../impl/WxPayServiceApacheHttpImpl.java | 5 ++ .../impl/WxPayServiceHttpComponentsImpl.java | 4 ++ .../service/impl/WxPayServiceSandboxTest.java | 53 +++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceSandboxTest.java diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java index d17c3ae69..8489f8062 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java @@ -368,14 +368,17 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { @Override public String getPayBaseUrl() { if (this.getConfig().isUseSandboxEnv()) { - if (StringUtils.isNotBlank(this.getConfig().getApiV3Key())) { - throw new WxRuntimeException("微信支付V3 目前不支持沙箱模式!"); - } return this.getConfig().getApiHostWithPathPrefix() + "/xdc/apiv2sandbox"; } return this.getConfig().getApiHostWithPathPrefix(); } + protected void checkV3SandboxNotSupported() { + if (this.getConfig().isUseSandboxEnv()) { + throw new WxRuntimeException("微信支付V3 目前不支持沙箱模式!"); + } + } + @Override public WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayException { request.checkAndSign(this.getConfig()); diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java index 12b85670f..5926bbacf 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java @@ -128,6 +128,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { } private String requestV3(String url, String requestStr, HttpRequestBase httpRequestBase) throws WxPayException { + this.checkV3SandboxNotSupported(); CloseableHttpClient httpClient = this.createApiV3HttpClient(); try (CloseableHttpResponse response = httpClient.execute(httpRequestBase)) { //v3已经改为通过状态码判断200 204 成功 @@ -163,6 +164,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { @Override public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException { + this.checkV3SandboxNotSupported(); HttpPost httpPost = this.createHttpPost(url, requestStr); this.configureRequest(httpPost); CloseableHttpClient httpClient = this.createApiV3HttpClient(); @@ -199,6 +201,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { @Override public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayException { + this.checkV3SandboxNotSupported(); this.configureRequest(httpRequest); CloseableHttpClient httpClient = this.createApiV3HttpClient(); try (CloseableHttpResponse response = httpClient.execute(httpRequest)) { @@ -243,6 +246,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { @Override public InputStream downloadV3(String url) throws WxPayException { + this.checkV3SandboxNotSupported(); HttpGet httpGet = new WxPayV3DownloadHttpGet(url); this.configureRequest(httpGet); CloseableHttpClient httpClient = this.createApiV3HttpClient(); @@ -285,6 +289,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { } private void configureRequest(HttpRequestBase request) { + this.checkV3SandboxNotSupported(); String serialNumber = getWechatPaySerial(getConfig()); String method = request.getMethod(); request.addHeader(ACCEPT, APPLICATION_JSON); diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceHttpComponentsImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceHttpComponentsImpl.java index cc5423302..837587e76 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceHttpComponentsImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceHttpComponentsImpl.java @@ -125,6 +125,7 @@ public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl { } private String requestV3(String url, String requestStr, HttpRequestBase httpRequestBase) throws WxPayException { + this.checkV3SandboxNotSupported(); CloseableHttpClient httpClient = this.createApiV3HttpClient(); try (CloseableHttpResponse response = httpClient.execute(httpRequestBase)) { //v3已经改为通过状态码判断200 204 成功 @@ -160,6 +161,7 @@ public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl { @Override public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException { + this.checkV3SandboxNotSupported(); HttpPost httpPost = this.createHttpPost(url, requestStr); this.configureRequest(httpPost); CloseableHttpClient httpClient = this.createApiV3HttpClient(); @@ -196,6 +198,7 @@ public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl { @Override public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayException { + this.checkV3SandboxNotSupported(); this.configureRequest(httpRequest); CloseableHttpClient httpClient = this.createApiV3HttpClient(); try (CloseableHttpResponse response = httpClient.execute(httpRequest)) { @@ -240,6 +243,7 @@ public class WxPayServiceHttpComponentsImpl extends BaseWxPayServiceImpl { @Override public InputStream downloadV3(String url) throws WxPayException { + this.checkV3SandboxNotSupported(); HttpGet httpGet = new WxPayV3DownloadHttpGet(url); this.configureRequest(httpGet); CloseableHttpClient httpClient = this.createApiV3HttpClient(); diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceSandboxTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceSandboxTest.java new file mode 100644 index 000000000..2daa9845a --- /dev/null +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceSandboxTest.java @@ -0,0 +1,53 @@ +package com.github.binarywang.wxpay.service.impl; + +import com.github.binarywang.wxpay.config.WxPayConfig; +import me.chanjar.weixin.common.error.WxRuntimeException; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.expectThrows; + +public class WxPayServiceSandboxTest { + + @Test + public void shouldUseV2SandboxUrlWhenV3KeyIsConfigured() { + WxPayConfig config = new WxPayConfig(); + config.setApiHostUrl("https://api.mch.weixin.qq.com"); + config.setApiHostUrlPath("/payment-proxy"); + config.setApiV3Key("v3-key"); + config.setUseSandboxEnv(true); + + WxPayServiceImpl service = new WxPayServiceImpl(); + service.setConfig(config); + + assertEquals(service.getPayBaseUrl(), "https://api.mch.weixin.qq.com/payment-proxy/xdc/apiv2sandbox"); + } + + @Test + public void shouldRejectV3RequestWhenSandboxIsEnabled() { + WxPayConfig config = new WxPayConfig(); + config.setUseSandboxEnv(true); + + WxPayServiceImpl service = new WxPayServiceImpl(); + service.setConfig(config); + + WxRuntimeException exception = expectThrows(WxRuntimeException.class, + () -> service.postV3("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi", "{}")); + + assertEquals(exception.getMessage(), "微信支付V3 目前不支持沙箱模式!"); + } + + @Test + public void shouldRejectHttpComponentsV3RequestWhenSandboxIsEnabled() { + WxPayConfig config = new WxPayConfig(); + config.setUseSandboxEnv(true); + + WxPayServiceHttpComponentsImpl service = new WxPayServiceHttpComponentsImpl(); + service.setConfig(config); + + WxRuntimeException exception = expectThrows(WxRuntimeException.class, + () -> service.postV3("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi", "{}")); + + assertEquals(exception.getMessage(), "微信支付V3 目前不支持沙箱模式!"); + } +}