From 01554a611db5804e7d10d370a9eef731aebc5179 Mon Sep 17 00:00:00 2001
From: Cui Zhi Feng <7426394+wuxixiaocui@user.noreply.gitee.com>
Date: 星期四, 10 十月 2024 08:30:16 +0800
Subject: [PATCH] 部分退款优化

---
 src/main/java/com/mzl/flower/service/payment/UserPaymentSybService.java |   44 +++++++++++++++++++++++++++++++++-----------
 1 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/payment/UserPaymentSybService.java b/src/main/java/com/mzl/flower/service/payment/UserPaymentSybService.java
index 5353faa..01a85ae 100644
--- a/src/main/java/com/mzl/flower/service/payment/UserPaymentSybService.java
+++ b/src/main/java/com/mzl/flower/service/payment/UserPaymentSybService.java
@@ -298,7 +298,9 @@
             }
         }
 
-        return !"1001".equals(trxstatus);
+        return !("1001".equals(trxstatus) || StringUtils.isEmpty(trxstatus)
+                || "2008".equals(trxstatus)
+                || "2000".equals(trxstatus));
     }
 
     public boolean checkOrderStatusRefund(String outTradeNo) throws Exception {
@@ -321,7 +323,7 @@
         VSP502:微信支付撤销
         VSP503:微信支付退款
         */
-        if ("VSP502".equals(trxcode)|| "VSP503".equals(trxcode)){
+        if ("VSP502".equals(trxcode) || "VSP503".equals(trxcode)){
             String trxid = params.get("trxid");
             updateOrderRefund(outTradeNo, trxid);
 
@@ -339,12 +341,28 @@
         }
 
         SybPayService service = new SybPayService();
-        Map<String,String> map = service.close(null, orderId);
-        log.info("======关闭订单结果" + toJSONString(map));
 
-        String retcode = map.get("retcode");
-        if(!"SUCCESS".equals(retcode)){
-            throw new ValidationException("取消订单失败: " + map.get("retmsg"));
+        Map<String,String> params = service.query(orderId, up.getTransactionId());
+        String trxstatus = params.get("trxstatus");//支付状态
+
+        if("1001".equals(trxstatus) || "3088".equals(trxstatus)){
+            //3088-交易未支付(在查询时间区间内未成功支付,如已影响资金24小时内会做差错退款处理)
+            //1001:交易不存在
+            //直接取消
+        } else if (StringUtils.isEmpty(trxstatus)
+                || "2008".equals(trxstatus)
+                || "2000".equals(trxstatus)) {
+            String trxid = params.get("trxid");
+            //2008或者2000 : 交易处理中,请查询交易,如果是实时交易(例如刷卡支付,交易撤销,退货),建议每隔一段时间(10秒)查询交易
+            Map<String,String> map = service.close(trxid, orderId);
+            log.info("======关闭订单结果" + toJSONString(map));
+
+            String retcode = map.get("retcode");
+            if(!"SUCCESS".equals(retcode)){
+                throw new ValidationException("取消订单失败: " + map.get("retmsg"));
+            }
+        } else {
+            throw new ValidationException("订单不可取消");
         }
 
         Order order = orderMapper.selectById(orderId);
@@ -424,10 +442,10 @@
             throw new ValidationException("订单不可退款");
         }
 
-        long trxamt = up.getPaymentAmount().longValue();
+        long trxamt = prepareAmount(up.getPaymentAmount());
         String reqsn = orderId;
         String oldtrxid = up.getTransactionId();
-        String oldreqsn = null;
+        String oldreqsn = orderId;
 
         SybPayService service = new SybPayService();
         Map<String,String> map = service.cancel(trxamt, reqsn, oldtrxid, oldreqsn);
@@ -451,10 +469,10 @@
             throw new ValidationException("订单不可退款");
         }
 
-        long trxamt = up.getPaymentAmount().longValue();
+        long trxamt = prepareAmount(up.getPaymentAmount());
         String reqsn = orderId;
         String oldtrxid = up.getTransactionId();
-        String oldreqsn = null;
+        String oldreqsn = orderId;
 
         SybPayService service = new SybPayService();
         Map<String,String> map = service.refund(trxamt, reqsn, oldtrxid, oldreqsn);
@@ -542,6 +560,10 @@
         re.setRequest(toJSONString(map));
         re.create(SecurityUtils.getUserId());
 
+        re.setStatus(retcode);
+        re.setNotification(retcode);
+        re.setNotifyTime(LocalDateTime.now());
+
         orderRefundMapper.insert(re);
 
         return re.getId();

--
Gitblit v1.9.3