diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDefaultRequest.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDefaultRequest.java new file mode 100644 index 00000000..ff36bfaa --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDefaultRequest.java @@ -0,0 +1,19 @@ +package com.github.binarywang.wxpay.bean.request; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +/** + *
+ * 支付请求默认对象类 + * Created by BinaryWang on 2017/6/18. + *+ * + * @author Binary Wang + */ +@XStreamAlias("xml") +public class WxPayDefaultRequest extends WxPayBaseRequest { + @Override + protected void checkConstraints() { + //do nothing + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPaySandboxSignKeyResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPaySandboxSignKeyResult.java new file mode 100644 index 00000000..6326f694 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPaySandboxSignKeyResult.java @@ -0,0 +1,35 @@ +package com.github.binarywang.wxpay.bean.result; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +/** + *
+ * Created by BinaryWang on 2017/6/18. + *+ * + * @author Binary Wang + */ +@XStreamAlias("xml") +public class WxPaySandboxSignKeyResult extends WxPayBaseResult { + + /** + *
+ * 沙箱密钥 + * sandbox_signkey + * 否 + * 013467007045764 + * String(32) + * 返回的沙箱密钥 + *+ */ + @XStreamAlias("sandbox_signkey") + private String sandboxSignKey; + + public String getSandboxSignKey() { + return sandboxSignKey; + } + + public void setSandboxSignKey(String sandboxSignKey) { + this.sandboxSignKey = sandboxSignKey; + } +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java index f3102a3a..495ecb5e 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java @@ -328,4 +328,15 @@ public interface WxPayService { * @see WxPayService#authcode2Openid(WxPayAuthcode2OpenidRequest) */ String authcode2Openid(String authCode) throws WxPayException; + + /** + *
+ * 获取仿真测试系统的验签密钥 + * 请求Url: https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey + * 是否需要证书: 否 + * 请求方式: POST + * 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1 + *+ */ + String getSandboxSignKey() throws WxPayException; } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java index f9973f0f..a7228774 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java @@ -76,7 +76,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/refundquery"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); WxPayRefundQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayRefundQueryResult.class); result.composeRefundRecords(); result.checkResult(this); @@ -139,7 +139,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/orderquery"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); if (StringUtils.isBlank(responseContent)) { throw new WxPayException("无响应结果"); } @@ -161,7 +161,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/closeorder"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); WxPayOrderCloseResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderCloseResult.class); result.checkResult(this); @@ -173,7 +173,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/unifiedorder"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); WxPayUnifiedOrderResult result = WxPayBaseResult.fromXML(responseContent, WxPayUnifiedOrderResult.class); result.checkResult(this); return result; @@ -294,7 +294,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/payitil/report"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class); result.checkResult(this); } @@ -310,7 +310,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/downloadbill"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); if (responseContent.startsWith("<")) { WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class); result.checkResult(this); @@ -397,7 +397,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/pay/micropay"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); WxPayMicropayResult result = WxPayBaseResult.fromXML(responseContent, WxPayMicropayResult.class); result.checkResult(this); return result; @@ -419,7 +419,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/tools/shorturl"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); WxPayShorturlResult result = WxPayBaseResult.fromXML(responseContent, WxPayShorturlResult.class); result.checkResult(this); return result.getShortUrl(); @@ -435,7 +435,7 @@ public class WxPayServiceImpl implements WxPayService { request.checkAndSign(this.getConfig()); String url = this.getPayBaseUrl() + "/tools/authcodetoopenid"; - String responseContent = this.post(url, request.toXML()); + String responseContent = this.post(url, request.toXML(), true); WxPayAuthcode2OpenidResult result = WxPayBaseResult.fromXML(responseContent, WxPayAuthcode2OpenidResult.class); result.checkResult(this); return result.getOpenid(); @@ -446,7 +446,25 @@ public class WxPayServiceImpl implements WxPayService { return this.authcode2Openid(new WxPayAuthcode2OpenidRequest(authCode)); } - private String post(String url, String xmlParam) { + @Override + public String getSandboxSignKey() throws WxPayException { + WxPayDefaultRequest request = new WxPayDefaultRequest(); + request.checkAndSign(this.getConfig()); + + String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey"; + String responseContent = this.post(url, request.toXML(), false); + WxPaySandboxSignKeyResult result = WxPayBaseResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class); + result.checkResult(this); + return result.getSandboxSignKey(); + } + + /** + * @param url 请求地址 + * @param xmlParam 请求字符串 + * @param needTransferEncoding 是否需要对结果进行重编码 + * @return 返回请求结果 + */ + private String post(String url, String xmlParam, boolean needTransferEncoding) { String requestString = xmlParam; try { requestString = new String(xmlParam.getBytes(CharEncoding.UTF_8), CharEncoding.ISO_8859_1); @@ -457,11 +475,14 @@ public class WxPayServiceImpl implements WxPayService { HttpRequest request = HttpRequest.post(url).body(requestString); HttpResponse response = request.send(); - String responseString = null; - try { - responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); + String responseString = response.bodyText(); + + if (needTransferEncoding) { + try { + responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } } this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, xmlParam, responseString); diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java index ac599adc..995aa827 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java @@ -264,5 +264,11 @@ public class WxPayServiceImplTest { this.logger.info(result); } + @Test + public void testGetSandboxSignKey() throws Exception { + final String signKey = this.payService.getSandboxSignKey(); + assertNotNull(signKey); + this.logger.info(signKey); + } }