1
0
mirror of synced 2026-10-07 23:39:43 +08:00

🎨 #4090 【微信支付】允许V2沙箱与V3配置共存

This commit is contained in:
Binary Wang
2026-08-12 13:34:26 +08:00
committed by GitHub
parent 1286cfd3f3
commit be7b151a7d
4 changed files with 68 additions and 3 deletions

View File

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

View File

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

View File

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

View File

@@ -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 目前不支持沙箱模式!");
}
}