Skip to content

Commit 067cfd4

Browse files
committed
fix: 允许V2沙箱与V3配置共存
1 parent eb42a33 commit 067cfd4

3 files changed

Lines changed: 51 additions & 3 deletions

File tree

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,6 @@ public String getConfigKey(String mchId, String appId) {
368368
@Override
369369
public String getPayBaseUrl() {
370370
if (this.getConfig().isUseSandboxEnv()) {
371-
if (StringUtils.isNotBlank(this.getConfig().getApiV3Key())) {
372-
throw new WxRuntimeException("微信支付V3 目前不支持沙箱模式!");
373-
}
374371
return this.getConfig().getApiHostWithPathPrefix() + "/xdc/apiv2sandbox";
375372
}
376373
return this.getConfig().getApiHostWithPathPrefix();

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.google.gson.JsonElement;
88
import com.google.gson.JsonObject;
99
import lombok.extern.slf4j.Slf4j;
10+
import me.chanjar.weixin.common.error.WxRuntimeException;
1011
import me.chanjar.weixin.common.util.http.apache.ByteArrayResponseHandler;
1112
import me.chanjar.weixin.common.util.json.GsonParser;
1213
import org.apache.commons.lang3.StringUtils;
@@ -128,6 +129,7 @@ public String postV3(String url, String requestStr) throws WxPayException {
128129
}
129130

130131
private String requestV3(String url, String requestStr, HttpRequestBase httpRequestBase) throws WxPayException {
132+
this.checkV3SandboxNotSupported();
131133
CloseableHttpClient httpClient = this.createApiV3HttpClient();
132134
try (CloseableHttpResponse response = httpClient.execute(httpRequestBase)) {
133135
//v3已经改为通过状态码判断200 204 成功
@@ -163,6 +165,7 @@ public String patchV3(String url, String requestStr) throws WxPayException {
163165

164166
@Override
165167
public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException {
168+
this.checkV3SandboxNotSupported();
166169
HttpPost httpPost = this.createHttpPost(url, requestStr);
167170
this.configureRequest(httpPost);
168171
CloseableHttpClient httpClient = this.createApiV3HttpClient();
@@ -199,6 +202,7 @@ public String postV3(String url, HttpPost httpPost) throws WxPayException {
199202

200203
@Override
201204
public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayException {
205+
this.checkV3SandboxNotSupported();
202206
this.configureRequest(httpRequest);
203207
CloseableHttpClient httpClient = this.createApiV3HttpClient();
204208
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
@@ -243,6 +247,7 @@ public String getV3WithWechatPaySerial(String url) throws WxPayException {
243247

244248
@Override
245249
public InputStream downloadV3(String url) throws WxPayException {
250+
this.checkV3SandboxNotSupported();
246251
HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
247252
this.configureRequest(httpGet);
248253
CloseableHttpClient httpClient = this.createApiV3HttpClient();
@@ -285,6 +290,7 @@ public String deleteV3(String url) throws WxPayException {
285290
}
286291

287292
private void configureRequest(HttpRequestBase request) {
293+
this.checkV3SandboxNotSupported();
288294
String serialNumber = getWechatPaySerial(getConfig());
289295
String method = request.getMethod();
290296
request.addHeader(ACCEPT, APPLICATION_JSON);
@@ -300,6 +306,12 @@ private void configureRequest(HttpRequestBase request) {
300306
.build());
301307
}
302308

309+
private void checkV3SandboxNotSupported() {
310+
if (this.getConfig().isUseSandboxEnv()) {
311+
throw new WxRuntimeException("微信支付V3 目前不支持沙箱模式!");
312+
}
313+
}
314+
303315
private CloseableHttpClient createApiV3HttpClient() throws WxPayException {
304316
CloseableHttpClient apiV3HttpClient = this.getConfig().getApiV3HttpClient();
305317
if (null == apiV3HttpClient) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.binarywang.wxpay.service.impl;
2+
3+
import com.github.binarywang.wxpay.config.WxPayConfig;
4+
import me.chanjar.weixin.common.error.WxRuntimeException;
5+
import org.testng.annotations.Test;
6+
7+
import static org.testng.Assert.assertEquals;
8+
import static org.testng.Assert.expectThrows;
9+
10+
public class WxPayServiceSandboxTest {
11+
12+
@Test
13+
public void shouldUseV2SandboxUrlWhenV3KeyIsConfigured() {
14+
WxPayConfig config = new WxPayConfig();
15+
config.setApiHostUrl("https://api.mch.weixin.qq.com");
16+
config.setApiHostUrlPath("/payment-proxy");
17+
config.setApiV3Key("v3-key");
18+
config.setUseSandboxEnv(true);
19+
20+
WxPayServiceImpl service = new WxPayServiceImpl();
21+
service.setConfig(config);
22+
23+
assertEquals(service.getPayBaseUrl(), "https://api.mch.weixin.qq.com/payment-proxy/xdc/apiv2sandbox");
24+
}
25+
26+
@Test
27+
public void shouldRejectV3RequestWhenSandboxIsEnabled() {
28+
WxPayConfig config = new WxPayConfig();
29+
config.setUseSandboxEnv(true);
30+
31+
WxPayServiceImpl service = new WxPayServiceImpl();
32+
service.setConfig(config);
33+
34+
WxRuntimeException exception = expectThrows(WxRuntimeException.class,
35+
() -> service.postV3("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi", "{}"));
36+
37+
assertEquals(exception.getMessage(), "微信支付V3 目前不支持沙箱模式!");
38+
}
39+
}

0 commit comments

Comments
 (0)