瀏覽代碼

微信退款优化

lighter 1 年之前
父節點
當前提交
a658d1e5d0

+ 2 - 1
src/main/java/thyyxxk/webserver/controller/outpatient/wxapi/WxApiController.java

@@ -7,6 +7,7 @@ import thyyxxk.webserver.entity.ResultVo;
 import thyyxxk.webserver.entity.outpatient.wxapi.QueryWxOrderParam;
 import thyyxxk.webserver.entity.outpatient.wxapi.RefundParam;
 import thyyxxk.webserver.service.outpatient.wxapi.WxApiService;
+import thyyxxk.webserver.utils.ResultVoUtil;
 
 import javax.servlet.http.HttpServletResponse;
 import java.util.Map;
@@ -32,7 +33,7 @@ public class WxApiController {
     @PassToken
     @PostMapping("/refundOrder")
     public ResultVo<String> refundOrder(@RequestBody @Validated RefundParam param) throws Exception {
-        return service.refundOrder(param).get();
+        return ResultVoUtil.success(service.refundOrder(param));
     }
 
     @PostMapping("/exportExcel")

+ 7 - 1
src/main/java/thyyxxk/webserver/entity/outpatient/wxapi/RefundParam.java

@@ -19,8 +19,10 @@ public class RefundParam {
     // 总金额
     @DecimalMin(value = "0", message = "总金额不能小于等于零。")
     private BigDecimal totalFee;
-    @DecimalMin(value = "0", message = "金额不能小于等于零。")
+    @DecimalMin(value = "0", message = "退款金额不能小于等于零。")
     private BigDecimal cashpayAmt;
+    @DecimalMin(value = "0", message = "退款金额不能小于等于零。")
+    private BigDecimal refundAmt;
     // 交易订单号
     @NotBlank(message = "交易订单号不能为空。")
     private String tradeNo;
@@ -32,4 +34,8 @@ public class RefundParam {
     private String refundReason;
     // 微信退款id
     private String refundId;
+
+    public @DecimalMin(value = "0", message = "退款金额不能小于等于零。") BigDecimal getRefundAmt() {
+        return null == refundAmt ? cashpayAmt : refundAmt;
+    }
 }

+ 11 - 10
src/main/java/thyyxxk/webserver/service/outpatient/wxapi/WxApiService.java

@@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import thyyxxk.webserver.config.exception.BizException;
 import thyyxxk.webserver.config.exception.ExceptionEnum;
 import thyyxxk.webserver.constants.Capacity;
 import thyyxxk.webserver.dao.his.outpatient.WxApiDao;
@@ -79,21 +80,21 @@ public class WxApiService {
         return ResultVoUtil.success(map);
     }
 
-    @Async
     @Transactional(rollbackFor = Exception.class)
-    public CompletableFuture<ResultVo<String>> refundOrder(RefundParam param) throws Exception {
+    public String refundOrder(RefundParam param) throws Exception {
         log.info("退款申请:{}", JSON.toJSONStringWithDateFormat(param, "yyyy-MM-dd HH:mm:ss"));
         String nonceStr = SnowFlakeId.instance().nextId();
         String outRefundNo = SnowFlakeId.instance().nextId();
-        String cashpayAmt = DecimalUtil.moneyYuanToFen(param.getCashpayAmt());
+        String refundAmt = DecimalUtil.moneyYuanToFen(param.getRefundAmt());
+        String totalFee = DecimalUtil.moneyYuanToFen(param.getTotalFee());
         TreeMap<String, String> map = new TreeMap<>();
         map.put("appid", WxCertUtil.APP_ID);
         map.put("mch_id", WxCertUtil.MERCHANT_ID);
         map.put("nonce_str", nonceStr);
         map.put("out_refund_no", outRefundNo);
         map.put("out_trade_no", param.getTradeNo());
-        map.put("refund_fee", cashpayAmt);
-        map.put("total_fee", cashpayAmt);
+        map.put("refund_fee", refundAmt);
+        map.put("total_fee", totalFee);
         String refundSign = Md5Util.createWxPaySign(map);
         String xml = "<xml>" +
                 "<appid>" + WxCertUtil.APP_ID + "</appid>" +
@@ -101,8 +102,8 @@ public class WxApiService {
                 "<nonce_str>" + nonceStr + "</nonce_str>" +
                 "<out_refund_no>" + outRefundNo + "</out_refund_no>" +
                 "<out_trade_no>" + param.getTradeNo() + "</out_trade_no>" +
-                "<refund_fee>" + cashpayAmt + "</refund_fee>" +
-                "<total_fee>" + cashpayAmt + "</total_fee>" +
+                "<refund_fee>" + refundAmt + "</refund_fee>" +
+                "<total_fee>" + totalFee + "</total_fee>" +
                 "<sign>" + refundSign + "</sign>" +
                 "</xml>";
         String str = requestWithSsl(xml);
@@ -116,16 +117,16 @@ public class WxApiService {
                 if ("订单已全额退款".equals(msg)) {
                     dao.alreadyRefund(param.getTradeNo(), msg);
                 }
-                return CompletableFuture.completedFuture(ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, msg));
+                throw new BizException(msg);
             }
             param.setRefundId(refundIdEle.getStringValue());
             dao.updateRefundId(param);
             log.info("微信退款成功:{}", param);
-            return CompletableFuture.completedFuture(ResultVoUtil.success(param.getRefundId()));
+            return param.getRefundId();
         }
         final String message = root.element("return_msg").getStringValue();
         log.info("微信退款失败:{}", message);
-        return CompletableFuture.completedFuture(ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, message));
+        throw new BizException(message);
     }
 
     private String requestWithSsl(String xml) throws Exception {