From 245bec709a21f6889bb3743843d6c543c0188b26 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Mon, 9 Jan 2017 13:47:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E9=85=8D=E7=BD=AE=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E4=BD=BF=E5=BE=97=E5=BE=AE=E4=BF=A1=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E8=83=BD=E5=A4=9F=E6=94=AF=E6=8C=81=E4=BB=BF=E7=9C=9F?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=8E=AF=E5=A2=83=20#119?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/mp/api/WxMpConfigStorage.java | 4 ++ .../mp/api/WxMpInMemoryConfigStorage.java | 5 +++ .../mp/api/impl/WxMpPayServiceImpl.java | 37 +++++++++++++------ .../mp/bean/pay/result/WxPayBaseResult.java | 3 +- .../mp/bean/pay/result/WxPayCommonResult.java | 15 ++++++++ .../mp/api/WxXmlMpInMemoryConfigStorage.java | 5 +++ .../mp/api/impl/WxMpPayServiceImplTest.java | 10 +++-- 7 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayCommonResult.java diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java index cff903bf..79ae9318 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java @@ -144,4 +144,8 @@ public interface WxMpConfigStorage { */ boolean autoRefreshToken(); + /** + * 微信支付是否使用仿真测试环境 + */ + boolean useSandboxForWxPay(); } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java index 29fbed63..f2292684 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java @@ -344,4 +344,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage { public boolean autoRefreshToken() { return true; } + + @Override + public boolean useSandboxForWxPay() { + return false; + } } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java index a9ef983b..dde6431a 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java @@ -52,6 +52,14 @@ public class WxMpPayServiceImpl implements WxMpPayService { return wxMpService.getWxMpConfigStorage(); } + private String getPayBaseUrl(){ + if(this.getConfig().useSandboxForWxPay()){ + return PAY_BASE_URL + "/sandboxnew"; + } + + return PAY_BASE_URL; + } + @Override public WxPayRefundResult refund(WxPayRefundRequest request) throws WxErrorException { this.initRequest(request); @@ -61,7 +69,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { this.checkParameters(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/secapi/pay/refund"; + String url = this.getPayBaseUrl() + "/secapi/pay/refund"; String responseContent = this.executeWithKey(url, request.toXML()); WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent, WxPayRefundResult.class); this.checkResult(result); @@ -83,7 +91,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { request.setRefundId(StringUtils.trimToNull(refundId)); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/pay/refundquery"; + String url = this.getPayBaseUrl() + "/pay/refundquery"; String responseContent = this.executeRequest(url, request.toXML()); WxPayRefundQueryResult result = WxPayRefundQueryResult.fromXML(responseContent, WxPayRefundQueryResult.class); result.composeRefundRecords(); @@ -150,10 +158,10 @@ public class WxMpPayServiceImpl implements WxMpPayService { this.initRequest(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack"; + String url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendredpack"; if (request.getAmtType() != null) { //裂变红包 - url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack"; + url = this.getPayBaseUrl() + "/mmpaymkttransfers/sendgroupredpack"; } String responseContent = this.executeWithKey(url, request.toXML()); WxPaySendRedpackResult result = WxPaySendRedpackResult.fromXML(responseContent, WxPaySendRedpackResult.class); @@ -169,7 +177,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { initRequest(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/mmpaymkttransfers/gethbinfo"; + String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gethbinfo"; String responseContent = this.executeWithKey(url, request.toXML()); WxPayRedpackQueryResult result = WxPayRedpackQueryResult.fromXML(responseContent, WxPayRedpackQueryResult.class); this.checkResult(result); @@ -189,7 +197,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { initRequest(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/pay/orderquery"; + String url = this.getPayBaseUrl() + "/pay/orderquery"; String responseContent = this.executeRequest(url, request.toXML()); WxPayOrderQueryResult result = WxPayOrderQueryResult.fromXML(responseContent, WxPayOrderQueryResult.class); result.composeCoupons(); @@ -208,7 +216,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { initRequest(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/pay/closeorder"; + String url = this.getPayBaseUrl() + "/pay/closeorder"; String responseContent = this.executeRequest(url, request.toXML()); WxPayOrderCloseResult result = WxPayOrderCloseResult.fromXML(responseContent, WxPayOrderCloseResult.class); this.checkResult(result); @@ -230,7 +238,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { checkParameters(request);//校验参数 request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/pay/unifiedorder"; + String url = this.getPayBaseUrl() + "/pay/unifiedorder"; String xmlParam = request.toXML(); log.debug("微信统一下单接口,URL:{},参数:{}", url, xmlParam); @@ -298,7 +306,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { BeanUtils.checkRequiredFields(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/mmpaymkttransfers/promotion/transfers"; + String url = this.getPayBaseUrl() + "/mmpaymkttransfers/promotion/transfers"; String responseContent = this.executeWithKey(url, request.toXML()); WxEntPayResult result = WxEntPayResult.fromXML(responseContent, WxEntPayResult.class); @@ -312,7 +320,7 @@ public class WxMpPayServiceImpl implements WxMpPayService { this.initRequest(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/mmpaymkttransfers/gettransferinfo"; + String url = this.getPayBaseUrl() + "/mmpaymkttransfers/gettransferinfo"; String responseContent = this.executeWithKey(url, request.toXML()); WxEntPayQueryResult result = WxEntPayQueryResult.fromXML(responseContent, WxEntPayQueryResult.class); this.checkResult(result); @@ -359,9 +367,9 @@ public class WxMpPayServiceImpl implements WxMpPayService { this.initRequest(request); request.setSign(this.createSign(request)); - String url = PAY_BASE_URL + "/payitil/report"; + String url = this.getPayBaseUrl() + "/payitil/report"; String responseContent = this.wxMpService.post(url, request.toXML()); - WxPayBaseResult result = WxPayBaseResult.fromXML(responseContent, WxPayBaseResult.class); + WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class); this.checkResult(result); } @@ -435,6 +443,11 @@ public class WxMpPayServiceImpl implements WxMpPayService { @Override public String createSign(Map params, String signKey) { + if(this.getConfig().useSandboxForWxPay()){ + //使用仿真测试环境 + return "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"; + } + SortedMap sortedMap = new TreeMap<>(params); StringBuilder toSign = new StringBuilder(); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java index 88334aa7..4dd133ab 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayBaseResult.java @@ -24,8 +24,7 @@ import java.util.Map; * @author binarywang(Binary Wang) * */ -@XStreamAlias("xml") -public class WxPayBaseResult { +public abstract class WxPayBaseResult { /** * 返回状态码 */ diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayCommonResult.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayCommonResult.java new file mode 100644 index 00000000..a6e7de5f --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/pay/result/WxPayCommonResult.java @@ -0,0 +1,15 @@ +package me.chanjar.weixin.mp.bean.pay.result; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +/** + *
+ * 微信支付结果仅包含有return 和result等相关信息的的属性类
+ * Created by Binary Wang on 2017-01-09.
+ * @author binarywang(Binary Wang)
+ * 
+ */ + +@XStreamAlias("xml") +public class WxPayCommonResult extends WxPayBaseResult { +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxXmlMpInMemoryConfigStorage.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxXmlMpInMemoryConfigStorage.java index 1ab04cf1..f2a54d49 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxXmlMpInMemoryConfigStorage.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxXmlMpInMemoryConfigStorage.java @@ -64,4 +64,9 @@ public class WxXmlMpInMemoryConfigStorage public void setAccessTokenLock(Lock lock){ super.accessTokenLock = lock; } + + @Override + public boolean useSandboxForWxPay() { + return true; + } } diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java index 7e28cb43..abdfb63a 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java @@ -8,13 +8,14 @@ import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage; import me.chanjar.weixin.mp.bean.pay.request.*; import me.chanjar.weixin.mp.bean.pay.result.*; -import org.testng.Assert; import org.testng.annotations.Guice; import org.testng.annotations.Test; import java.nio.file.Files; import java.nio.file.Path; +import static org.testng.Assert.*; + /** * 测试支付相关接口 * Created by Binary Wang on 2016/7/28. @@ -52,6 +53,7 @@ public class WxMpPayServiceImplTest { WxXmlMpInMemoryConfigStorage config = (WxXmlMpInMemoryConfigStorage) this.wxService.getWxMpConfigStorage(); config.setSslContextFilePath(config.getKeyPath()); } + /** * Test method for {@link me.chanjar.weixin.mp.api.impl.WxMpPayServiceImpl#refund(WxPayRefundRequest)} . */ @@ -163,8 +165,8 @@ public class WxMpPayServiceImplTest { String qrcodeContent = QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()); System.out.println(qrcodeContent); - Assert.assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?")); - Assert.assertTrue(qrcodeContent.contains("product_id=" + productId)); + assertTrue(qrcodeContent.startsWith("weixin://wxpay/bizpayurl?")); + assertTrue(qrcodeContent.contains("product_id=" + productId)); } @Test @@ -174,7 +176,7 @@ public class WxMpPayServiceImplTest { Path qrcodeFilePath = Files.createTempFile("qrcode_", ".jpg"); Files.write(qrcodeFilePath, bytes); - Assert.assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent); + assertEquals(QrcodeUtils.decodeQrcode(qrcodeFilePath.toFile()), qrcodeContent); }