Skip to content

Commit 87687b3

Browse files
committed
实现获取仿真测试系统的验签密钥的API #206
1 parent 9eb4ccf commit 87687b3

File tree

5 files changed

+107
-15
lines changed

5 files changed

+107
-15
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.binarywang.wxpay.bean.request;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
5+
/**
6+
* <pre>
7+
* 支付请求默认对象类
8+
* Created by BinaryWang on 2017/6/18.
9+
* </pre>
10+
*
11+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
12+
*/
13+
@XStreamAlias("xml")
14+
public class WxPayDefaultRequest extends WxPayBaseRequest {
15+
@Override
16+
protected void checkConstraints() {
17+
//do nothing
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.binarywang.wxpay.bean.result;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
5+
/**
6+
* <pre>
7+
* Created by BinaryWang on 2017/6/18.
8+
* </pre>
9+
*
10+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
11+
*/
12+
@XStreamAlias("xml")
13+
public class WxPaySandboxSignKeyResult extends WxPayBaseResult {
14+
15+
/**
16+
* <pre>
17+
* 沙箱密钥
18+
* sandbox_signkey
19+
* 否
20+
* 013467007045764
21+
* String(32)
22+
* 返回的沙箱密钥
23+
* </pre>
24+
*/
25+
@XStreamAlias("sandbox_signkey")
26+
private String sandboxSignKey;
27+
28+
public String getSandboxSignKey() {
29+
return sandboxSignKey;
30+
}
31+
32+
public void setSandboxSignKey(String sandboxSignKey) {
33+
this.sandboxSignKey = sandboxSignKey;
34+
}
35+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,15 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
328328
* @see WxPayService#authcode2Openid(WxPayAuthcode2OpenidRequest)
329329
*/
330330
String authcode2Openid(String authCode) throws WxPayException;
331+
332+
/**
333+
* <pre>
334+
* 获取仿真测试系统的验签密钥
335+
* 请求Url: https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey
336+
* 是否需要证书: 否
337+
* 请求方式: POST
338+
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1
339+
* </pre>
340+
*/
341+
String getSandboxSignKey() throws WxPayException;
331342
}

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
7676
request.checkAndSign(this.getConfig());
7777

7878
String url = this.getPayBaseUrl() + "/pay/refundquery";
79-
String responseContent = this.post(url, request.toXML());
79+
String responseContent = this.post(url, request.toXML(), true);
8080
WxPayRefundQueryResult result = WxPayBaseResult.fromXML(responseContent, WxPayRefundQueryResult.class);
8181
result.composeRefundRecords();
8282
result.checkResult(this);
@@ -139,7 +139,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
139139
request.checkAndSign(this.getConfig());
140140

141141
String url = this.getPayBaseUrl() + "/pay/orderquery";
142-
String responseContent = this.post(url, request.toXML());
142+
String responseContent = this.post(url, request.toXML(), true);
143143
if (StringUtils.isBlank(responseContent)) {
144144
throw new WxPayException("无响应结果");
145145
}
@@ -161,7 +161,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
161161
request.checkAndSign(this.getConfig());
162162

163163
String url = this.getPayBaseUrl() + "/pay/closeorder";
164-
String responseContent = this.post(url, request.toXML());
164+
String responseContent = this.post(url, request.toXML(), true);
165165
WxPayOrderCloseResult result = WxPayBaseResult.fromXML(responseContent, WxPayOrderCloseResult.class);
166166
result.checkResult(this);
167167

@@ -173,7 +173,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) th
173173
request.checkAndSign(this.getConfig());
174174

175175
String url = this.getPayBaseUrl() + "/pay/unifiedorder";
176-
String responseContent = this.post(url, request.toXML());
176+
String responseContent = this.post(url, request.toXML(), true);
177177
WxPayUnifiedOrderResult result = WxPayBaseResult.fromXML(responseContent, WxPayUnifiedOrderResult.class);
178178
result.checkResult(this);
179179
return result;
@@ -294,7 +294,7 @@ public void report(WxPayReportRequest request) throws WxPayException {
294294
request.checkAndSign(this.getConfig());
295295

296296
String url = this.getPayBaseUrl() + "/payitil/report";
297-
String responseContent = this.post(url, request.toXML());
297+
String responseContent = this.post(url, request.toXML(), true);
298298
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
299299
result.checkResult(this);
300300
}
@@ -310,7 +310,7 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
310310
request.checkAndSign(this.getConfig());
311311

312312
String url = this.getPayBaseUrl() + "/pay/downloadbill";
313-
String responseContent = this.post(url, request.toXML());
313+
String responseContent = this.post(url, request.toXML(), true);
314314
if (responseContent.startsWith("<")) {
315315
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
316316
result.checkResult(this);
@@ -397,7 +397,7 @@ public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayEx
397397
request.checkAndSign(this.getConfig());
398398

399399
String url = this.getPayBaseUrl() + "/pay/micropay";
400-
String responseContent = this.post(url, request.toXML());
400+
String responseContent = this.post(url, request.toXML(), true);
401401
WxPayMicropayResult result = WxPayBaseResult.fromXML(responseContent, WxPayMicropayResult.class);
402402
result.checkResult(this);
403403
return result;
@@ -419,7 +419,7 @@ public String shorturl(WxPayShorturlRequest request) throws WxPayException {
419419
request.checkAndSign(this.getConfig());
420420

421421
String url = this.getPayBaseUrl() + "/tools/shorturl";
422-
String responseContent = this.post(url, request.toXML());
422+
String responseContent = this.post(url, request.toXML(), true);
423423
WxPayShorturlResult result = WxPayBaseResult.fromXML(responseContent, WxPayShorturlResult.class);
424424
result.checkResult(this);
425425
return result.getShortUrl();
@@ -435,7 +435,7 @@ public String authcode2Openid(WxPayAuthcode2OpenidRequest request) throws WxPayE
435435
request.checkAndSign(this.getConfig());
436436

437437
String url = this.getPayBaseUrl() + "/tools/authcodetoopenid";
438-
String responseContent = this.post(url, request.toXML());
438+
String responseContent = this.post(url, request.toXML(), true);
439439
WxPayAuthcode2OpenidResult result = WxPayBaseResult.fromXML(responseContent, WxPayAuthcode2OpenidResult.class);
440440
result.checkResult(this);
441441
return result.getOpenid();
@@ -446,7 +446,25 @@ public String authcode2Openid(String authCode) throws WxPayException {
446446
return this.authcode2Openid(new WxPayAuthcode2OpenidRequest(authCode));
447447
}
448448

449-
private String post(String url, String xmlParam) {
449+
@Override
450+
public String getSandboxSignKey() throws WxPayException {
451+
WxPayDefaultRequest request = new WxPayDefaultRequest();
452+
request.checkAndSign(this.getConfig());
453+
454+
String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
455+
String responseContent = this.post(url, request.toXML(), false);
456+
WxPaySandboxSignKeyResult result = WxPayBaseResult.fromXML(responseContent, WxPaySandboxSignKeyResult.class);
457+
result.checkResult(this);
458+
return result.getSandboxSignKey();
459+
}
460+
461+
/**
462+
* @param url 请求地址
463+
* @param xmlParam 请求字符串
464+
* @param needTransferEncoding 是否需要对结果进行重编码
465+
* @return 返回请求结果
466+
*/
467+
private String post(String url, String xmlParam, boolean needTransferEncoding) {
450468
String requestString = xmlParam;
451469
try {
452470
requestString = new String(xmlParam.getBytes(CharEncoding.UTF_8), CharEncoding.ISO_8859_1);
@@ -457,11 +475,14 @@ private String post(String url, String xmlParam) {
457475

458476
HttpRequest request = HttpRequest.post(url).body(requestString);
459477
HttpResponse response = request.send();
460-
String responseString = null;
461-
try {
462-
responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8);
463-
} catch (UnsupportedEncodingException e) {
464-
e.printStackTrace();
478+
String responseString = response.bodyText();
479+
480+
if (needTransferEncoding) {
481+
try {
482+
responseString = new String(response.bodyText().getBytes(CharEncoding.ISO_8859_1), CharEncoding.UTF_8);
483+
} catch (UnsupportedEncodingException e) {
484+
e.printStackTrace();
485+
}
465486
}
466487

467488
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, xmlParam, responseString);

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,11 @@ public void testAuthcode2Openid() throws Exception {
264264
this.logger.info(result);
265265
}
266266

267+
@Test
268+
public void testGetSandboxSignKey() throws Exception {
269+
final String signKey = this.payService.getSandboxSignKey();
270+
assertNotNull(signKey);
271+
this.logger.info(signKey);
272+
}
267273

268274
}

0 commit comments

Comments
 (0)