Skip to content

Commit be7b151

Browse files
authored
🎨 #4090 【微信支付】允许V2沙箱与V3配置共存
1 parent 1286cfd commit be7b151

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,17 @@ 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();
377374
}
378375

376+
protected void checkV3SandboxNotSupported() {
377+
if (this.getConfig().isUseSandboxEnv()) {
378+
throw new WxRuntimeException("微信支付V3 目前不支持沙箱模式!");
379+
}
380+
}
381+
379382
@Override
380383
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayException {
381384
request.checkAndSign(this.getConfig());

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public String postV3(String url, String requestStr) throws WxPayException {
128128
}
129129

130130
private String requestV3(String url, String requestStr, HttpRequestBase httpRequestBase) throws WxPayException {
131+
this.checkV3SandboxNotSupported();
131132
CloseableHttpClient httpClient = this.createApiV3HttpClient();
132133
try (CloseableHttpResponse response = httpClient.execute(httpRequestBase)) {
133134
//v3已经改为通过状态码判断200 204 成功
@@ -163,6 +164,7 @@ public String patchV3(String url, String requestStr) throws WxPayException {
163164

164165
@Override
165166
public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException {
167+
this.checkV3SandboxNotSupported();
166168
HttpPost httpPost = this.createHttpPost(url, requestStr);
167169
this.configureRequest(httpPost);
168170
CloseableHttpClient httpClient = this.createApiV3HttpClient();
@@ -199,6 +201,7 @@ public String postV3(String url, HttpPost httpPost) throws WxPayException {
199201

200202
@Override
201203
public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayException {
204+
this.checkV3SandboxNotSupported();
202205
this.configureRequest(httpRequest);
203206
CloseableHttpClient httpClient = this.createApiV3HttpClient();
204207
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
@@ -243,6 +246,7 @@ public String getV3WithWechatPaySerial(String url) throws WxPayException {
243246

244247
@Override
245248
public InputStream downloadV3(String url) throws WxPayException {
249+
this.checkV3SandboxNotSupported();
246250
HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
247251
this.configureRequest(httpGet);
248252
CloseableHttpClient httpClient = this.createApiV3HttpClient();
@@ -285,6 +289,7 @@ public String deleteV3(String url) throws WxPayException {
285289
}
286290

287291
private void configureRequest(HttpRequestBase request) {
292+
this.checkV3SandboxNotSupported();
288293
String serialNumber = getWechatPaySerial(getConfig());
289294
String method = request.getMethod();
290295
request.addHeader(ACCEPT, APPLICATION_JSON);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public String postV3(String url, String requestStr) throws WxPayException {
125125
}
126126

127127
private String requestV3(String url, String requestStr, HttpRequestBase httpRequestBase) throws WxPayException {
128+
this.checkV3SandboxNotSupported();
128129
CloseableHttpClient httpClient = this.createApiV3HttpClient();
129130
try (CloseableHttpResponse response = httpClient.execute(httpRequestBase)) {
130131
//v3已经改为通过状态码判断200 204 成功
@@ -160,6 +161,7 @@ public String patchV3(String url, String requestStr) throws WxPayException {
160161

161162
@Override
162163
public String postV3WithWechatpaySerial(String url, String requestStr) throws WxPayException {
164+
this.checkV3SandboxNotSupported();
163165
HttpPost httpPost = this.createHttpPost(url, requestStr);
164166
this.configureRequest(httpPost);
165167
CloseableHttpClient httpClient = this.createApiV3HttpClient();
@@ -196,6 +198,7 @@ public String postV3(String url, HttpPost httpPost) throws WxPayException {
196198

197199
@Override
198200
public String requestV3(String url, HttpRequestBase httpRequest) throws WxPayException {
201+
this.checkV3SandboxNotSupported();
199202
this.configureRequest(httpRequest);
200203
CloseableHttpClient httpClient = this.createApiV3HttpClient();
201204
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
@@ -240,6 +243,7 @@ public String getV3WithWechatPaySerial(String url) throws WxPayException {
240243

241244
@Override
242245
public InputStream downloadV3(String url) throws WxPayException {
246+
this.checkV3SandboxNotSupported();
243247
HttpGet httpGet = new WxPayV3DownloadHttpGet(url);
244248
this.configureRequest(httpGet);
245249
CloseableHttpClient httpClient = this.createApiV3HttpClient();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
40+
@Test
41+
public void shouldRejectHttpComponentsV3RequestWhenSandboxIsEnabled() {
42+
WxPayConfig config = new WxPayConfig();
43+
config.setUseSandboxEnv(true);
44+
45+
WxPayServiceHttpComponentsImpl service = new WxPayServiceHttpComponentsImpl();
46+
service.setConfig(config);
47+
48+
WxRuntimeException exception = expectThrows(WxRuntimeException.class,
49+
() -> service.postV3("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi", "{}"));
50+
51+
assertEquals(exception.getMessage(), "微信支付V3 目前不支持沙箱模式!");
52+
}
53+
}

0 commit comments

Comments
 (0)