撤销订单API,并重构相关代码,简化开发 #101

This commit is contained in:
Binary Wang
2017-03-24 11:11:02 +08:00
parent 6945e7e0f9
commit 3f4cdb7bf5
19 changed files with 607 additions and 342 deletions

View File

@@ -43,6 +43,11 @@ public class WxEntPayQueryRequest extends WxPayBaseRequest {
this.partnerTradeNo = partnerTradeNo;
}
@Override
protected void checkConstraints() {
//do nothing
}
@Override
public String toString() {
return ToStringUtils.toSimpleString(this);

View File

@@ -154,6 +154,11 @@ public class WxEntPayRequest extends WxPayBaseRequest {
private String spbillCreateIp;
@Override
protected void checkConstraints() {
}
@Override
public String getAppid() {
return this.mchAppid;

View File

@@ -1,9 +1,14 @@
package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.util.SignUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.BeanUtils;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
@@ -23,6 +28,22 @@ import java.math.BigDecimal;
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
*/
public abstract class WxPayBaseRequest {
/**
* 检查请求参数内容,包括必填参数以及特殊约束
*/
protected void checkFields() throws WxErrorException {
//check required fields
BeanUtils.checkRequiredFields(this);
//check other parameters
this.checkConstraints();
}
/**
* 检查约束情况
*/
protected abstract void checkConstraints();
/**
* <pre>
* 公众账号ID
@@ -180,4 +201,41 @@ public abstract class WxPayBaseRequest {
xstream.processAnnotations(this.getClass());
return xstream.toXML(this);
}
/**
* <pre>
* 检查参数,并设置签名
* 1、检查参数注意子类实现需要检查参数的而外功能时请在调用父类的方法前进行相应判断
* 2、补充系统参数如果未传入则从配置里读取
* 3、生成签名并设置进去
* </pre>
* @param config 支付配置对象,用于读取相应系统配置信息
*/
public void checkAndSign(WxPayConfig config) throws WxErrorException {
this.checkFields();
if (StringUtils.isBlank(getAppid())) {
this.setAppid(config.getAppId());
}
if (StringUtils.isBlank(getMchId())) {
this.setMchId(config.getMchId());
}
if (StringUtils.isBlank(getSubAppId())) {
this.setSubAppId(config.getSubAppId());
}
if (StringUtils.isBlank(getSubMchId())) {
this. setSubMchId(config.getSubMchId());
}
if (StringUtils.isBlank(getNonceStr())) {
this.setNonceStr(String.valueOf(System.currentTimeMillis()));
}
//设置签名字段的值
this.setSign(SignUtils.createSign(this, config.getMchKey()));
}
}

View File

@@ -2,6 +2,10 @@ package com.github.binarywang.wxpay.bean.request;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.annotation.Required;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
/**
* <pre>
@@ -12,6 +16,8 @@ import me.chanjar.weixin.common.annotation.Required;
*/
@XStreamAlias("xml")
public class WxPayDownloadBillRequest extends WxPayBaseRequest {
private static final String[] BILL_TYPE = new String[]{"ALL", "REFUND", "SUCCESS"};
/**
* <pre>
* 设备号
@@ -120,4 +126,16 @@ public class WxPayDownloadBillRequest extends WxPayBaseRequest {
public void setTarType(String tarType) {
this.tarType = tarType;
}
@Override
protected void checkConstraints() {
if (StringUtils.isNotBlank(this.getTarType()) && !"GZIP".equals(this.getTarType())) {
throw new IllegalArgumentException("tar_type值如果存在只能为GZIP");
}
if (!ArrayUtils.contains(BILL_TYPE, this.getBillType())) {
throw new IllegalArgumentException(String.format("bill_tpye目前必须为%s其中之一,实际值:%s",
Arrays.toString(BILL_TYPE), this.getBillType()));
}
}
}

View File

@@ -261,6 +261,11 @@ public class WxPayMicropayRequest extends WxPayBaseRequest {
this.authCode = authCode;
}
@Override
protected void checkConstraints() {
//do nothing
}
public static final class Builder {
private String signType;
private String body;

View File

@@ -33,4 +33,8 @@ public class WxPayOrderCloseRequest extends WxPayBaseRequest {
this.outTradeNo = outTradeNo;
}
@Override
protected void checkConstraints() {
}
}

View File

@@ -1,6 +1,7 @@
package com.github.binarywang.wxpay.bean.request;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils;
/**
* <pre>
@@ -61,4 +62,12 @@ public class WxPayOrderQueryRequest extends WxPayBaseRequest {
public void setOutTradeNo(String outTradeNo) {
this.outTradeNo = outTradeNo;
}
@Override
protected void checkConstraints() {
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) ||
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo))) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时存在或同时为空,必须二选一");
}
}
}

View File

@@ -0,0 +1,163 @@
package com.github.binarywang.wxpay.bean.request;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils;
/**
* <pre>
* 撤销订单请求类
* Created by Binary Wang on 2017-3-23.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
@XStreamAlias("xml")
public class WxPayOrderReverseRequest extends WxPayBaseRequest {
/**
* <pre>
* 微信订单号
* transaction_id
* String(28)
* 1217752501201400000000000000
* 微信的订单号,优先使用
* </pre>
*/
@XStreamAlias("transaction_id")
private String transactionId;
/**
* <pre>
* 商户订单号
* out_trade_no
* String(32)
* 1217752501201400000000000000
* 商户系统内部的订单号
* transaction_id、out_trade_no二选一如果同时存在优先级transaction_id> out_trade_no
* </pre>
*/
@XStreamAlias("out_trade_no")
private String outTradeNo;
/**
* <pre>
* 签名类型
* sign_type
* 否
* String(32)
* HMAC-SHA256
* 签名类型目前支持HMAC-SHA256和MD5默认为MD5
**/
@XStreamAlias("sign_type")
private String signType;
private WxPayOrderReverseRequest(Builder builder) {
setTransactionId(builder.transactionId);
setAppid(builder.appid);
setOutTradeNo(builder.outTradeNo);
setMchId(builder.mchId);
setSignType(builder.signType);
setSubAppId(builder.subAppId);
setSubMchId(builder.subMchId);
setNonceStr(builder.nonceStr);
setSign(builder.sign);
}
public static Builder newBuilder() {
return new Builder();
}
public String getTransactionId() {
return this.transactionId;
}
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}
public String getOutTradeNo() {
return this.outTradeNo;
}
public void setOutTradeNo(String outTradeNo) {
this.outTradeNo = outTradeNo;
}
public String getSignType() {
return this.signType;
}
public void setSignType(String signType) {
this.signType = signType;
}
@Override
protected void checkConstraints() {
if (StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no不能同时为空");
}
}
public static final class Builder {
private String transactionId;
private String appid;
private String outTradeNo;
private String mchId;
private String signType;
private String subAppId;
private String subMchId;
private String nonceStr;
private String sign;
private Builder() {
}
public Builder transactionId(String transactionId) {
this.transactionId = transactionId;
return this;
}
public Builder appid(String appid) {
this.appid = appid;
return this;
}
public Builder outTradeNo(String outTradeNo) {
this.outTradeNo = outTradeNo;
return this;
}
public Builder mchId(String mchId) {
this.mchId = mchId;
return this;
}
public Builder signType(String signType) {
this.signType = signType;
return this;
}
public Builder subAppId(String subAppId) {
this.subAppId = subAppId;
return this;
}
public Builder subMchId(String subMchId) {
this.subMchId = subMchId;
return this;
}
public Builder nonceStr(String nonceStr) {
this.nonceStr = nonceStr;
return this;
}
public Builder sign(String sign) {
this.sign = sign;
return this;
}
public WxPayOrderReverseRequest build() {
return new WxPayOrderReverseRequest(this);
}
}
}

View File

@@ -54,4 +54,9 @@ public class WxPayRedpackQueryRequest extends WxPayBaseRequest {
public void setMchBillNo(String mchBillNo) {
this.mchBillNo = mchBillNo;
}
@Override
protected void checkConstraints() {
}
}

View File

@@ -1,6 +1,7 @@
package com.github.binarywang.wxpay.bean.request;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang3.StringUtils;
/**
* <pre>
@@ -132,4 +133,15 @@ public class WxPayRefundQueryRequest extends WxPayBaseRequest {
public void setRefundId(String refundId) {
this.refundId = refundId;
}
@Override
protected void checkConstraints() {
if ((StringUtils.isBlank(transactionId) && StringUtils.isBlank(outTradeNo)
&& StringUtils.isBlank(outRefundNo) && StringUtils.isBlank(refundId)) ||
(StringUtils.isNotBlank(transactionId) && StringUtils.isNotBlank(outTradeNo)
&& StringUtils.isNotBlank(outRefundNo) && StringUtils.isNotBlank(refundId))) {
throw new IllegalArgumentException("transaction_idout_trade_noout_refund_norefund_id 必须四选一");
}
}
}

View File

@@ -1,7 +1,13 @@
package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.annotation.Required;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
/**
* <pre>
@@ -13,13 +19,25 @@ import me.chanjar.weixin.common.annotation.Required;
* <li>类型
* <li>示例值
* <li>描述
* Created by Binary Wang on 2016-10-08.
* </pre>
*
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* Created by Binary Wang on 2016-10-08.
*/
@XStreamAlias("xml")
public class WxPayRefundRequest extends WxPayBaseRequest {
private static final String[] REFUND_ACCOUNT = new String[]{"REFUND_SOURCE_RECHARGE_FUNDS",
"REFUND_SOURCE_UNSETTLED_FUNDS"};
@Override
public void checkAndSign(WxPayConfig config) throws WxErrorException {
if (StringUtils.isBlank(this.getOpUserId())) {
this.setOpUserId(config.getMchId());
}
super.checkAndSign(config);
}
/**
* <pre>
* 设备号
@@ -240,6 +258,20 @@ public class WxPayRefundRequest extends WxPayBaseRequest {
this.refundAccount = refundAccount;
}
@Override
protected void checkConstraints() {
if (StringUtils.isNotBlank(this.getRefundAccount())) {
if (!ArrayUtils.contains(REFUND_ACCOUNT, this.getRefundAccount())) {
throw new IllegalArgumentException(String.format("refund_account目前必须为%s其中之一,实际值:%s",
Arrays.toString(REFUND_ACCOUNT), this.getRefundAccount()));
}
}
if (StringUtils.isBlank(this.getOutTradeNo()) && StringUtils.isBlank(this.getTransactionId())) {
throw new IllegalArgumentException("transaction_id 和 out_trade_no 不能同时为空,必须提供一个");
}
}
public static final class Builder {
private String deviceInfo;
private String appid;

View File

@@ -270,4 +270,9 @@ public class WxPayReportRequest extends WxPayBaseRequest {
public void setTime(String time) {
this.time = time;
}
@Override
protected void checkConstraints() {
//do nothing
}
}

View File

@@ -217,6 +217,11 @@ public class WxPaySendRedpackRequest extends WxPayBaseRequest {
this.remark = remark;
}
@Override
protected void checkConstraints() {
}
@Override
public String getAppid() {
return this.wxAppid;

View File

@@ -1,7 +1,13 @@
package com.github.binarywang.wxpay.bean.request;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.annotation.Required;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
/**
* <pre>
@@ -21,6 +27,7 @@ import me.chanjar.weixin.common.annotation.Required;
*/
@XStreamAlias("xml")
public class WxPayUnifiedOrderRequest extends WxPayBaseRequest {
private static final String[] TRADE_TYPES = new String[]{"JSAPI", "NATIVE", "APP"};
/**
* <pre>
@@ -409,6 +416,35 @@ public class WxPayUnifiedOrderRequest extends WxPayBaseRequest {
this.openid = openid;
}
@Override
protected void checkConstraints() {
if (!ArrayUtils.contains(TRADE_TYPES, this.getTradeType())) {
throw new IllegalArgumentException(String.format("trade_type目前必须为%s其中之一,实际值:%s",
Arrays.toString(TRADE_TYPES), this.getTradeType()));
}
if ("JSAPI".equals(this.getTradeType()) && this.getOpenid() == null) {
throw new IllegalArgumentException("当 trade_type是'JSAPI'时未指定openid");
}
if ("NATIVE".equals(this.getTradeType()) && this.getProductId() == null) {
throw new IllegalArgumentException("当 trade_type是'NATIVE'时未指定product_id");
}
}
@Override
public void checkAndSign(WxPayConfig config) throws WxErrorException {
if (StringUtils.isBlank(this.getNotifyURL())) {
this.setNotifyURL(config.getNotifyUrl());
}
if (StringUtils.isBlank(this.getTradeType())) {
this.setTradeType(config.getTradeType());
}
super.checkAndSign(config);
}
public static class WxUnifiedOrderRequestBuilder {
private String appid;
private String mchId;

View File

@@ -1,9 +1,13 @@
package com.github.binarywang.wxpay.bean.result;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.github.binarywang.wxpay.util.SignUtils;
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import org.apache.commons.lang3.StringUtils;
@@ -300,4 +304,44 @@ public abstract class WxPayBaseResult {
return Integer.valueOf(result);
}
/**
* 校验返回结果签名
*/
public void checkResult(WxPayServiceImpl wxPayService) throws WxErrorException {
//校验返回结果签名
Map<String, String> map = toMap();
if (getSign() != null && !SignUtils.checkSign(map, wxPayService.getConfig().getMchKey())) {
this.getLogger().debug("校验结果签名失败,参数:{}", map);
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg("参数格式校验错误!").build());
}
//校验结果是否成功
if (!"SUCCESS".equalsIgnoreCase(getReturnCode())
|| !"SUCCESS".equalsIgnoreCase(getResultCode())) {
StringBuilder errorMsg = new StringBuilder();
if (getReturnCode() != null) {
errorMsg.append("返回代码:").append(getReturnCode());
}
if (getReturnMsg() != null) {
errorMsg.append(",返回信息:").append(getReturnMsg());
}
if (getResultCode() != null) {
errorMsg.append(",结果代码:").append(getResultCode());
}
if (getErrCode() != null) {
errorMsg.append(",错误代码:").append(getErrCode());
}
if (getErrCodeDes() != null) {
errorMsg.append(",错误详情:").append(getErrCodeDes());
}
WxError error = WxError.newBuilder()
.setErrorCode(-1)
.setErrorMsg(errorMsg.toString())
.build();
this.getLogger().error("\n结果业务代码异常返回結果{},\n{}", map, error);
throw new WxErrorException(error);
}
}
}

View File

@@ -0,0 +1,34 @@
package com.github.binarywang.wxpay.bean.result;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* <pre>
* 撤销订单响应结果类
* Created by Binary Wang on 2017-3-23.
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
* </pre>
*/
public class WxPayOrderReverseResult extends WxPayBaseResult {
/**
* <pre>
* 是否重调
* recall
* 是
* String(1)
* Y
* 是否需要继续调用撤销Y-需要N-不需要
* </pre>
**/
@XStreamAlias("recall")
private String isRecall;
public String getIsRecall() {
return this.isRecall;
}
public void setIsRecall(String isRecall) {
this.isRecall = isRecall;
}
}