瀏覽代碼

实现获取仿真测试系统的验签密钥的API #206

master
Binary Wang 7 年之前
父節點
當前提交
87687b3369
共有 5 個檔案被更改,包括 107 行新增15 行删除
  1. +19
    -0
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/request/WxPayDefaultRequest.java
  2. +35
    -0
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPaySandboxSignKeyResult.java
  3. +11
    -0
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java
  4. +36
    -15
      weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImpl.java
  5. +6
    -0
      weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/WxPayServiceImplTest.java

+ 19
- 0
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;

/**
* <pre>
* 支付请求默认对象类
* Created by BinaryWang on 2017/6/18.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@XStreamAlias("xml")
public class WxPayDefaultRequest extends WxPayBaseRequest {
@Override
protected void checkConstraints() {
//do nothing
}
}

+ 35
- 0
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;

/**
* <pre>
* Created by BinaryWang on 2017/6/18.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@XStreamAlias("xml")
public class WxPaySandboxSignKeyResult extends WxPayBaseResult {

/**
* <pre>
* 沙箱密钥
* sandbox_signkey
* 否
* 013467007045764
* String(32)
* 返回的沙箱密钥
* </pre>
*/
@XStreamAlias("sandbox_signkey")
private String sandboxSignKey;

public String getSandboxSignKey() {
return sandboxSignKey;
}

public void setSandboxSignKey(String sandboxSignKey) {
this.sandboxSignKey = sandboxSignKey;
}
}

+ 11
- 0
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;

/**
* <pre>
* 获取仿真测试系统的验签密钥
* 请求Url: https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey
* 是否需要证书: 否
* 请求方式: POST
* 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=23_1
* </pre>
*/
String getSandboxSignKey() throws WxPayException;
}

+ 36
- 15
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);


+ 6
- 0
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);
}

}

Loading…
取消
儲存