Browse Source

feat[litemall-admin]: 支持微信退款

Junling Bu 7 years ago
parent
commit
97011e15a3

+ 3 - 0
deploy/litemall/application-core.yml

@@ -6,6 +6,9 @@ litemall:
     mch-id: 111111
     mch-key: xxxxxx
     notify-url: http://122.152.206.172:8080/wx/order/pay-notify
+    # 商户证书文件路径
+    # 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
+    key-path: xxxxx
 
   #通知相关配置
   notify:

+ 49 - 16
litemall-admin-api/src/main/java/org/linlinjava/litemall/admin/web/AdminOrderController.java

@@ -1,10 +1,15 @@
 package org.linlinjava.litemall.admin.web;
 
+import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
+import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
+import com.github.binarywang.wxpay.exception.WxPayException;
+import com.github.binarywang.wxpay.service.WxPayService;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.linlinjava.litemall.admin.annotation.LoginAdmin;
 import org.linlinjava.litemall.core.notify.NotifyService;
 import org.linlinjava.litemall.core.notify.NotifyType;
+import org.linlinjava.litemall.core.util.CharUtil;
 import org.linlinjava.litemall.core.util.JacksonUtil;
 import org.linlinjava.litemall.core.util.ResponseUtil;
 import org.linlinjava.litemall.core.validator.Order;
@@ -50,7 +55,8 @@ public class AdminOrderController {
     private LitemallUserService userService;
     @Autowired
     private LitemallCommentService commentService;
-
+    @Autowired
+    private WxPayService wxPayService;
     @Autowired
     private NotifyService notifyService;
 
@@ -93,16 +99,21 @@ public class AdminOrderController {
     }
 
     /**
-     * 订单退款确认
-     * 1. 检测当前订单是否能够退款确认
-     * 2. 设置订单退款确认状态
-     * 3. 订单商品恢复
+     * 订单退款
+     * <p>
+     * 1. 检测当前订单是否能够退款;
+     * 2. 微信退款操作;
+     * 3. 设置订单退款确认状态;
+     * 4. 订单商品库存回库。
+     * <p>
+     * TODO
+     * 虽然接入了微信退款API,但是从安全角度考虑,建议开发者删除这里微信退款代码,采用以下两步走步骤:
+     * 1. 管理员登录微信官方支付平台点击退款操作进行退款
+     * 2. 管理员登录litemall管理后台点击退款操作进行订单状态修改和商品库存回库
      *
      * @param adminId 管理员ID
      * @param body    订单信息,{ orderId:xxx }
-     * @return 订单操作结果
-     * 成功则 { errno: 0, errmsg: '成功' }
-     * 失败则 { errno: XXX, errmsg: XXX }
+     * @return 订单退款操作结果
      */
     @PostMapping("refund")
     public Object refund(@LoginAdmin Integer adminId, @RequestBody String body) {
@@ -114,6 +125,9 @@ public class AdminOrderController {
         if (orderId == null) {
             return ResponseUtil.badArgument();
         }
+        if(StringUtils.isEmpty(refundMoney)){
+            return ResponseUtil.badArgument();
+        }
 
         LitemallOrder order = orderService.findById(orderId);
         if (order == null) {
@@ -129,6 +143,31 @@ public class AdminOrderController {
             return ResponseUtil.fail(ORDER_CONFIRM_NOT_ALLOWED, "订单不能确认收货");
         }
 
+        // 微信退款
+        WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
+        wxPayRefundRequest.setOutTradeNo(order.getOrderSn());
+        wxPayRefundRequest.setOutRefundNo("refund_" + order.getOrderSn());
+        // 元转成分
+        Integer totalFee = order.getActualPrice().multiply(new BigDecimal(100)).intValue();
+        wxPayRefundRequest.setTotalFee(totalFee);
+        wxPayRefundRequest.setRefundFee(totalFee);
+
+        WxPayRefundResult wxPayRefundResult = null;
+        try {
+            wxPayRefundResult = wxPayService.refund(wxPayRefundRequest);
+        } catch (WxPayException e) {
+            e.printStackTrace();
+            return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
+        }
+        if(!wxPayRefundResult.getReturnCode().equals("SUCCESS")){
+            logger.warn("refund fail: " +  wxPayRefundResult.getReturnMsg());
+            return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
+        }
+        if(!wxPayRefundResult.getResultCode().equals("SUCCESS")){
+            logger.warn("refund fail: " +  wxPayRefundResult.getReturnMsg());
+            return ResponseUtil.fail(ORDER_REFUND_FAILED, "订单退款失败");
+        }
+
         // 开启事务管理
         DefaultTransactionDefinition def = new DefaultTransactionDefinition();
         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
@@ -156,16 +195,10 @@ public class AdminOrderController {
         }
 
         //TODO 发送邮件和短信通知,这里采用异步发送
-        // 退款成功通知用户
-        /**
-         *
-         * 您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。
-         * 注意订单号只发后6位
-         *
-         */
+        // 退款成功通知用户, 例如“您申请的订单退款 [ 单号:{1} ] 已成功,请耐心等待到账。”
+        // 注意订单号只发后6位
         notifyService.notifySmsTemplate(order.getMobile(), NotifyType.REFUND, new String[]{order.getOrderSn().substring(8, 14)});
 
-
         txManager.commit(status);
 
         return ResponseUtil.ok();

+ 1 - 0
litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxConfig.java

@@ -39,6 +39,7 @@ public class WxConfig {
         payConfig.setMchId(properties.getMchId());
         payConfig.setMchKey(properties.getMchKey());
         payConfig.setNotifyUrl(properties.getNotifyUrl());
+        payConfig.setKeyPath(properties.getKeyPath());
         payConfig.setTradeType("JSAPI");
         payConfig.setSignType("MD5");
         return payConfig;

+ 9 - 0
litemall-core/src/main/java/org/linlinjava/litemall/core/config/WxProperties.java

@@ -17,6 +17,8 @@ public class WxProperties {
 
     private String notifyUrl;
 
+    private String keyPath;
+
     public String getNotifyUrl() {
         return notifyUrl;
     }
@@ -57,4 +59,11 @@ public class WxProperties {
         this.mchId = mchId;
     }
 
+    public String getKeyPath() {
+        return keyPath;
+    }
+
+    public void setKeyPath(String keyPath) {
+        this.keyPath = keyPath;
+    }
 }

+ 3 - 0
litemall-core/src/main/resources/application-core.yml

@@ -6,6 +6,9 @@ litemall:
     mch-id: 111111
     mch-key: xxxxxx
     notify-url: http://www.example.com/wx/order/pay-notify
+    # 商户证书文件路径
+    # 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
+    key-path: xxxxx
 
   #通知相关配置
   notify: