From 8b30615cd7833a6513361bdf96fb2a4c8a95b51c Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 22 Apr 2018 00:33:33 +0800 Subject: [PATCH] =?UTF-8?q?#530=20=E5=BE=AE=E4=BF=A1=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E7=94=B3=E8=AF=B7=E9=80=80=E6=AC=BE=E6=8E=A5=E5=8F=A3=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E7=B1=BB=E5=A2=9E=E5=8A=A0=E5=8D=95=E4=B8=AA=E4=BB=A3?= =?UTF-8?q?=E9=87=91=E5=88=B8=E7=9B=B8=E5=85=B3=E5=8F=82=E6=95=B0=20?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=A0=B9=E6=8D=AE=E5=AE=98=E6=96=B9=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E6=95=B4=E7=90=86=E5=85=B6=E4=BB=96=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bean/result/WxPayRefundCouponInfo.java | 61 ++++++++++++ .../bean/result/WxPayRefundQueryResult.java | 54 +---------- .../wxpay/bean/result/WxPayRefundResult.java | 97 +++++++++++++++---- .../service/impl/BaseWxPayServiceImpl.java | 1 + .../bean/result/WxPayRefundResultTest.java | 51 ++++++++++ 5 files changed, 192 insertions(+), 72 deletions(-) create mode 100644 weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundCouponInfo.java create mode 100644 weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundCouponInfo.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundCouponInfo.java new file mode 100644 index 00000000..290e4723 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundCouponInfo.java @@ -0,0 +1,61 @@ +package com.github.binarywang.wxpay.bean.result; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + *
+ *  退款代金券信息.
+ *  Created by BinaryWang on 2018/4/21.
+ * 
+ * + * @author Binary Wang + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class WxPayRefundCouponInfo { + /** + *
+   * 字段名:退款代金券ID.
+   * 变量名:coupon_refund_id_$n_$m
+   * 是否必填:否
+   * 类型:String(20)
+   * 示例值:10000
+   * 描述:退款代金券ID, $n为下标,$m为下标,从0开始编号
+   * 
+ */ + @XStreamAlias("coupon_refund_id") + private String couponRefundId; + + /** + *
+   * 字段名:单个退款代金券支付金额.
+   * 变量名:coupon_refund_fee_$n_$m
+   * 是否必填:否
+   * 类型:Int
+   * 示例值:100
+   * 描述:单个退款代金券支付金额, $n为下标,$m为下标,从0开始编号
+   * 
+ */ + @XStreamAlias("coupon_refund_fee") + private Integer couponRefundFee; + + /** + *
+   * 字段名:代金券类型.
+   * 变量名:coupon_type_$n_$m
+   * 是否必填:否
+   * 类型:String(8)
+   * 示例值:CASH
+   * 描述:CASH--充值代金券 , NO_CASH---非充值代金券。
+   * 开通免充值券功能,并且订单使用了优惠券后有返回(取值:CASH、NO_CASH)。
+   * $n为下标,$m为下标,从0开始编号,举例:coupon_type_$0_$1
+   * 
+ */ + @XStreamAlias("coupon_type") + private String couponType; + +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundQueryResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundQueryResult.java index 65514a72..662e30bb 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundQueryResult.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundQueryResult.java @@ -151,10 +151,10 @@ public class WxPayRefundQueryResult extends BaseWxPayResult { continue; } - List coupons = Lists.newArrayList(); + List coupons = Lists.newArrayList(); for (int j = 0; j < refundRecord.getCouponRefundCount(); j++) { coupons.add( - new RefundRecord.RefundCoupon( + new WxPayRefundCouponInfo( this.getXmlValue("xml/coupon_refund_id_" + i + "_" + j), this.getXmlValueAsInt("xml/coupon_refund_fee_" + i + "_" + j), this.getXmlValue("xml/coupon_type_" + i + "_" + j) @@ -277,7 +277,7 @@ public class WxPayRefundQueryResult extends BaseWxPayResult { @XStreamAlias("coupon_refund_count") private Integer couponRefundCount; - private List refundCoupons; + private List refundCoupons; /** *
@@ -323,54 +323,6 @@ public class WxPayRefundQueryResult extends BaseWxPayResult {
     @XStreamAlias("refund_success_time")
     private String refundSuccessTime;
 
-    @Data
-    @NoArgsConstructor
-    @AllArgsConstructor
-    public static class RefundCoupon {
-      /**
-       * 
-       * 字段名:退款代金券ID.
-       * 变量名:coupon_refund_id_$n_$m
-       * 是否必填:否
-       * 类型:String(20)
-       * 示例值:10000
-       * 描述:退款代金券ID, $n为下标,$m为下标,从0开始编号
-       * 
- */ - @XStreamAlias("coupon_refund_id") - private String couponRefundId; - - /** - *
-       * 字段名:单个退款代金券支付金额.
-       * 变量名:coupon_refund_fee_$n_$m
-       * 是否必填:否
-       * 类型:Int
-       * 示例值:100
-       * 描述:单个退款代金券支付金额, $n为下标,$m为下标,从0开始编号
-       * 
- */ - @XStreamAlias("coupon_refund_fee") - private Integer couponRefundFee; - - /** - *
-       * 字段名:代金券类型.
-       * 变量名:coupon_type_$n_$m
-       * 是否必填:否
-       * 类型:String(8)
-       * 示例值:CASH
-       * 描述:CASH--充值代金券 , NO_CASH---非充值代金券。
-       * 开通免充值券功能,并且订单使用了优惠券后有返回(取值:CASH、NO_CASH)。
-       * $n为下标,$m为下标,从0开始编号,举例:coupon_type_$0_$1
-       * 
- */ - @XStreamAlias("coupon_type") - private String couponType; - - - } - } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java index 5dde2c90..9c7e0ad0 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java @@ -1,67 +1,122 @@ package com.github.binarywang.wxpay.bean.result; +import com.google.common.collect.Lists; import com.thoughtworks.xstream.annotations.XStreamAlias; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import java.io.Serializable; +import java.util.List; /** *
- * 微信支付-申请退款返回结果
+ * 微信支付-申请退款返回结果.
  * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
  * 
* - * @author liukaitj + * @author liukaitj & Binary Wang */ @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor @XStreamAlias("xml") public class WxPayRefundResult extends BaseWxPayResult implements Serializable { - private static final long serialVersionUID = 1L; - - @XStreamAlias("device_info") - private String deviceInfo; - + private static final long serialVersionUID = -3392333879907788033L; + /** + * 微信订单号. + */ @XStreamAlias("transaction_id") private String transactionId; + /** + * 商户订单号. + */ @XStreamAlias("out_trade_no") private String outTradeNo; + /** + * 商户退款单号. + */ @XStreamAlias("out_refund_no") private String outRefundNo; + /** + * 微信退款单号. + */ @XStreamAlias("refund_id") private String refundId; - @XStreamAlias("refund_channel") - private String refundChannel; - + /** + * 退款金额. + */ @XStreamAlias("refund_fee") - private String refundFee; + private Integer refundFee; + /** + * 应结退款金额. + */ + @XStreamAlias("settlement_refund_fee") + private Integer settlementRefundFee; + + /** + * 标价金额. + */ @XStreamAlias("total_fee") - private String totalFee; + private Integer totalFee; + + /** + * 应结订单金额. + */ + @XStreamAlias("settlement_total_fee") + private Integer settlementTotalFee; + /** + * 标价币种. + */ @XStreamAlias("fee_type") private String feeType; + /** + * 现金支付金额. + */ @XStreamAlias("cash_fee") - private String cashFee; + private Integer cashFee; + /** + * 现金支付币种. + */ + @XStreamAlias("cash_fee_type") + private String cashFeeType; + + /** + * 现金退款金额. + */ @XStreamAlias("cash_refund_fee") private String cashRefundFee; - @XStreamAlias("coupon_refund_fee") - private String couponRefundFee; - + /** + * 退款代金券使用数量. + */ @XStreamAlias("coupon_refund_count") - private String couponRefundCount; - - @XStreamAlias("coupon_refund_id") - private String couponRefundId; - + private Integer couponRefundCount; + + private List refundCoupons; + + /** + * 组装生成退款代金券信息. + */ + public void composeRefundCoupons() { + List coupons = Lists.newArrayList(); + for (int i = 0; i < this.getCouponRefundCount(); i++) { + coupons.add( + new WxPayRefundCouponInfo( + this.getXmlValue("xml/coupon_refund_id_" + i), + this.getXmlValueAsInt("xml/coupon_refund_fee_" + i), + this.getXmlValue("xml/coupon_type_" + i) + ) + ); + } + this.setRefundCoupons(coupons); + } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java index 43f60535..ef81f31b 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java @@ -131,6 +131,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { String url = this.getPayBaseUrl() + "/secapi/pay/refund"; String responseContent = this.post(url, request.toXML(), true); WxPayRefundResult result = BaseWxPayResult.fromXML(responseContent, WxPayRefundResult.class); + result.composeRefundCoupons(); result.checkResult(this, request.getSignType(), true); return result; } diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java new file mode 100644 index 00000000..7c9b34fb --- /dev/null +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java @@ -0,0 +1,51 @@ +package com.github.binarywang.wxpay.bean.result; + +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + *
+ *  Created by BinaryWang on 2018/4/22.
+ * 
+ * + * @author Binary Wang + */ +public class WxPayRefundResultTest { + + @Test + public void testComposeRefundCoupons() { + /* + 该xml字符串来自于官方文档示例,稍加改造,加上代金卷 + refund_channel 是个什么鬼,官方文档只字不提 + */ + String xmlString = "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " 1\n" + + " 1\n" + + " 123\n" + + " 1\n" + + " \n" + + " 2 \n" + + ""; + + WxPayRefundResult result = WxPayRefundResult.fromXML(xmlString, WxPayRefundResult.class); + result.composeRefundCoupons(); + + assertThat(result.getRefundCoupons()).isNotEmpty(); + assertThat(result.getRefundCoupons().get(0).getCouponRefundId()).isEqualTo("123"); + assertThat(result.getRefundCoupons().get(0).getCouponType()).isEqualTo("CASH"); + assertThat(result.getRefundCoupons().get(0).getCouponRefundFee()).isEqualTo(1); + } +}