Skip to content

Commit 245bec7

Browse files
committed
引入配置方法,使得微信支付能够支持仿真测试环境 binarywang#119
1 parent fa7360d commit 245bec7

File tree

7 files changed

+61
-18
lines changed

7 files changed

+61
-18
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,8 @@ public interface WxMpConfigStorage {
144144
*/
145145
boolean autoRefreshToken();
146146

147+
/**
148+
* 微信支付是否使用仿真测试环境
149+
*/
150+
boolean useSandboxForWxPay();
147151
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,9 @@ public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientB
344344
public boolean autoRefreshToken() {
345345
return true;
346346
}
347+
348+
@Override
349+
public boolean useSandboxForWxPay() {
350+
return false;
351+
}
347352
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ private WxMpConfigStorage getConfig() {
5252
return wxMpService.getWxMpConfigStorage();
5353
}
5454

55+
private String getPayBaseUrl(){
56+
if(this.getConfig().useSandboxForWxPay()){
57+
return PAY_BASE_URL + "/sandboxnew";
58+
}
59+
60+
return PAY_BASE_URL;
61+
}
62+
5563
@Override
5664
public WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorException {
5765
this.initRequest(request);
@@ -61,7 +69,7 @@ public WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorExcept
6169
this.checkParameters(request);
6270
request.setSign(this.createSign(request));
6371

64-
String url = PAY_BASE_URL + "/secapi/pay/refund";
72+
String url = this.getPayBaseUrl() + "/secapi/pay/refund";
6573
String responseContent = this.executeWithKey(url, request.toXML());
6674
WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent, WxPayRefundResult.class);
6775
this.checkResult(result);
@@ -83,7 +91,7 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
8391
request.setRefundId(StringUtils.trimToNull(refundId));
8492
request.setSign(this.createSign(request));
8593

86-
String url = PAY_BASE_URL + "/pay/refundquery";
94+
String url = this.getPayBaseUrl() + "/pay/refundquery";
8795
String responseContent = this.executeRequest(url, request.toXML());
8896
WxPayRefundQueryResult result = WxPayRefundQueryResult.fromXML(responseContent, WxPayRefundQueryResult.class);
8997
result.composeRefundRecords();
@@ -150,10 +158,10 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request)
150158
this.initRequest(request);
151159
request.setSign(this.createSign(request));
152160

153-
String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack";
161+
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendredpack";
154162
if (request.getAmtType() != null) {
155163
//裂变红包
156-
url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack";
164+
url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendgroupredpack";
157165
}
158166
String responseContent = this.executeWithKey(url, request.toXML());
159167
WxPaySendRedpackResult result = WxPaySendRedpackResult.fromXML(responseContent, WxPaySendRedpackResult.class);
@@ -169,7 +177,7 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxErrorExce
169177
initRequest(request);
170178
request.setSign(this.createSign(request));
171179

172-
String url = PAY_BASE_URL + "/mmpaymkttransfers/gethbinfo";
180+
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gethbinfo";
173181
String responseContent = this.executeWithKey(url, request.toXML());
174182
WxPayRedpackQueryResult result = WxPayRedpackQueryResult.fromXML(responseContent, WxPayRedpackQueryResult.class);
175183
this.checkResult(result);
@@ -189,7 +197,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
189197
initRequest(request);
190198
request.setSign(this.createSign(request));
191199

192-
String url = PAY_BASE_URL + "/pay/orderquery";
200+
String url = this.getPayBaseUrl() + "/pay/orderquery";
193201
String responseContent = this.executeRequest(url, request.toXML());
194202
WxPayOrderQueryResult result = WxPayOrderQueryResult.fromXML(responseContent, WxPayOrderQueryResult.class);
195203
result.composeCoupons();
@@ -208,7 +216,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
208216
initRequest(request);
209217
request.setSign(this.createSign(request));
210218

211-
String url = PAY_BASE_URL + "/pay/closeorder";
219+
String url = this.getPayBaseUrl() + "/pay/closeorder";
212220
String responseContent = this.executeRequest(url, request.toXML());
213221
WxPayOrderCloseResult result = WxPayOrderCloseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
214222
this.checkResult(result);
@@ -230,7 +238,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
230238
checkParameters(request);//校验参数
231239
request.setSign(this.createSign(request));
232240

233-
String url = PAY_BASE_URL + "/pay/unifiedorder";
241+
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
234242
String xmlParam = request.toXML();
235243
log.debug("微信统一下单接口,URL:{},参数:{}", url, xmlParam);
236244

@@ -298,7 +306,7 @@ public WxEntPayResult entPay(WxEntPayRequest request) throws WxErrorException {
298306
BeanUtils.checkRequiredFields(request);
299307
request.setSign(this.createSign(request));
300308

301-
String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers";
309+
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers";
302310

303311
String responseContent = this.executeWithKey(url, request.toXML());
304312
WxEntPayResult result = WxEntPayResult.fromXML(responseContent, WxEntPayResult.class);
@@ -312,7 +320,7 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo) throws WxErrorExce
312320
this.initRequest(request);
313321
request.setSign(this.createSign(request));
314322

315-
String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo";
323+
String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo";
316324
String responseContent = this.executeWithKey(url, request.toXML());
317325
WxEntPayQueryResult result = WxEntPayQueryResult.fromXML(responseContent, WxEntPayQueryResult.class);
318326
this.checkResult(result);
@@ -359,9 +367,9 @@ public void report(WxPayReportRequest request) throws WxErrorException {
359367
this.initRequest(request);
360368
request.setSign(this.createSign(request));
361369

362-
String url = PAY_BASE_URL + "/payitil/report";
370+
String url = this.getPayBaseUrl() + "/payitil/report";
363371
String responseContent = this.wxMpService.post(url, request.toXML());
364-
WxPayBaseResult result = WxPayBaseResult.fromXML(responseContent, WxPayBaseResult.class);
372+
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
365373
this.checkResult(result);
366374
}
367375

@@ -435,6 +443,11 @@ public String createSign(Map<String, String> params) {
435443

436444
@Override
437445
public String createSign(Map<String, String> params, String signKey) {
446+
if(this.getConfig().useSandboxForWxPay()){
447+
//使用仿真测试环境
448+
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456";
449+
}
450+
438451
SortedMap<String, String> sortedMap = new TreeMap<>(params);
439452

440453
StringBuilder toSign = new StringBuilder();

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
2525
* </pre>
2626
*/
27-
@XStreamAlias("xml")
28-
public class WxPayBaseResult {
27+
public abstract class WxPayBaseResult {
2928
/**
3029
* 返回状态码
3130
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package me.chanjar.weixin.mp.bean.pay.result;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
5+
/**
6+
* <pre>
7+
* 微信支付结果仅包含有return 和result等相关信息的的属性类
8+
* Created by Binary Wang on 2017-01-09.
9+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
10+
* </pre>
11+
*/
12+
13+
@XStreamAlias("xml")
14+
public class WxPayCommonResult extends WxPayBaseResult {
15+
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxXmlMpInMemoryConfigStorage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ public void setTemplateId(String templateId) {
6464
public void setAccessTokenLock(Lock lock){
6565
super.accessTokenLock = lock;
6666
}
67+
68+
@Override
69+
public boolean useSandboxForWxPay() {
70+
return true;
71+
}
6772
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
99
import me.chanjar.weixin.mp.bean.pay.request.*;
1010
import me.chanjar.weixin.mp.bean.pay.result.*;
11-
import org.testng.Assert;
1211
import org.testng.annotations.Guice;
1312
import org.testng.annotations.Test;
1413

1514
import java.nio.file.Files;
1615
import java.nio.file.Path;
1716

17+
import static org.testng.Assert.*;
18+
1819
/**
1920
* 测试支付相关接口
2021
* Created by Binary Wang on 2016/7/28.
@@ -52,6 +53,7 @@ public void setSSLKey(){
5253
WxXmlMpInMemoryConfigStorage config = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage();
5354
config.setSslContextFilePath(config.getKeyPath());
5455
}
56+
5557
/**
5658
* Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#refund(WxPayRefundRequest)} .
5759
*/
@@ -163,8 +165,8 @@ public void testCreateScanPayQrcodeMode1() throws Exception {
163165
String qrcodeContent = QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile());
164166
System.out.println(qrcodeContent);
165167

166-
Assert.assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?"));
167-
Assert.assertTrue(qrcodeContent.contains("product_id=" + productId));
168+
assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?"));
169+
assertTrue(qrcodeContent.contains("product_id=" + productId));
168170
}
169171

170172
@Test
@@ -174,7 +176,7 @@ public void testCreateScanPayQrcodeMode2() throws Exception {
174176
Path qrcodeFilePath = Files.createTempFile("qrcode_", ".jpg");
175177
Files.write(qrcodeFilePath, bytes);
176178

177-
Assert.assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
179+
assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent);
178180
}
179181

180182

0 commit comments

Comments
 (0)