Răsfoiți Sursa

fix fix 微信支付回调

skyline 2 ani în urmă
părinte
comite
2d6949b69e

+ 0 - 5
entity/src/main/java/com/kym/entity/miniapp/PayLog.java

@@ -24,11 +24,6 @@ public class PayLog extends BaseEntity implements Serializable {
 
 
     private static final long serialVersionUID = 1L;
     private static final long serialVersionUID = 1L;
 
 
-    public static final String STATUS_退款成功 = "SUCCESS";
-    public static final String STATUS_退款关闭 = "CLOSED";
-    public static final String STATUS_退款处理中 = "PROCESSING";
-    public static final String STATUS_退款异常 = "ABNORMAL";
-
     /**
     /**
      * 用户ID
      * 用户ID
      */
      */

+ 5 - 0
entity/src/main/java/com/kym/entity/miniapp/RefundLog.java

@@ -22,6 +22,11 @@ import java.time.LocalDateTime;
 @Accessors(chain = true)
 @Accessors(chain = true)
 public class RefundLog extends BaseEntity {
 public class RefundLog extends BaseEntity {
 
 
+    public static final String STATUS_退款成功 = "SUCCESS";
+    public static final String STATUS_退款关闭 = "CLOSED";
+    public static final String STATUS_退款处理中 = "PROCESSING";
+    public static final String STATUS_退款异常 = "ABNORMAL";
+
     private static final long serialVersionUID = 1L;
     private static final long serialVersionUID = 1L;
 
 
     /**
     /**

+ 1 - 1
miniapp/src/main/java/com/kym/miniapp/controller/PaymentController.java

@@ -59,7 +59,7 @@ public class PaymentController {
     }
     }
 
 
 
 
-    @ApiLog("微信退款")
+    @ApiLog("用户申请退款")
     @GetMapping("/wxApplyRefund")
     @GetMapping("/wxApplyRefund")
     @ResponseBody
     @ResponseBody
     R wxAppRefund() {
     R wxAppRefund() {

+ 12 - 6
service/src/main/java/com/kym/service/wechat/impl/WxPayServiceImpl.java

@@ -315,12 +315,12 @@ public class WxPayServiceImpl implements WxPayService {
         }
         }
 
 
         // 将余额转移至冻结余额
         // 将余额转移至冻结余额
-        accountService.lambdaUpdate().setSql(" frozen_amount = balance ,balance = 0").eq(Account::getUserId, userId).update();
+        accountService.lambdaUpdate().setSql(" frozen_amount = (frozen_amount + balance) ,balance = 0").eq(Account::getUserId, userId).update();
 
 
         // 余额作为退款金额
         // 余额作为退款金额
         AtomicInteger refundAmount = new AtomicInteger(account.getBalance());
         AtomicInteger refundAmount = new AtomicInteger(account.getBalance());
         // 充值记录
         // 充值记录
-        var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, PayLog.STATUS_退款成功).orderByDesc(PayLog::getSuccessTime).list();
+        var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, RefundLog.STATUS_退款成功).orderByDesc(PayLog::getSuccessTime).list();
         // 充值金额综合不足以支付余额退款
         // 充值金额综合不足以支付余额退款
         if (!CommUtil.isEmptyOrNull(payLogs)) {
         if (!CommUtil.isEmptyOrNull(payLogs)) {
             var total = payLogs.stream().mapToInt(PayLog::getTotal).sum();
             var total = payLogs.stream().mapToInt(PayLog::getTotal).sum();
@@ -445,19 +445,25 @@ public class WxPayServiceImpl implements WxPayService {
      *
      *
      * @param request
      * @param request
      */
      */
-    @SneakyThrows
     @Override
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void wxRefundNotify(HttpServletRequest request) {
     public void wxRefundNotify(HttpServletRequest request) {
         var notifyRes = handleWxNotify(request);
         var notifyRes = handleWxNotify(request);
+
         try {
         try {
             // 以支付通知回调为例,验签、解密并转换成 RefundNotification
             // 以支付通知回调为例,验签、解密并转换成 RefundNotification
             RefundNotification refundNotification = ((NotificationParser) notifyRes[1]).parse((RequestParam) notifyRes[0], RefundNotification.class);
             RefundNotification refundNotification = ((NotificationParser) notifyRes[1]).parse((RequestParam) notifyRes[0], RefundNotification.class);
             LOGGER.info("微信退款回调{}:验签解密完毕,数据:\n{}", notifyRes[2], refundNotification);
             LOGGER.info("微信退款回调{}:验签解密完毕,数据:\n{}", notifyRes[2], refundNotification);
-            DateTime dt = DateUtil.parse(refundNotification.getSuccessTime());
-            LocalDateTime successTime = LocalDateTimeUtil.of(dt);
 
 
             //退款日志在申请时插入,接收通知时更新
             //退款日志在申请时插入,接收通知时更新
             var refundLog = refundLogService.lambdaQuery().eq(RefundLog::getOutRefundNo, refundNotification.getOutRefundNo()).one();
             var refundLog = refundLogService.lambdaQuery().eq(RefundLog::getOutRefundNo, refundNotification.getOutRefundNo()).one();
+            if (RefundLog.STATUS_退款成功.equals(refundLog.getStatus())) {
+                return;
+            }
+
+            DateTime dt = DateUtil.parse(refundNotification.getSuccessTime());
+            LocalDateTime successTime = LocalDateTimeUtil.of(dt);
+
             refundLog
             refundLog
                     .setRefundId(refundNotification.getRefundId())
                     .setRefundId(refundNotification.getRefundId())
                     .setTransactionId(refundNotification.getTransactionId())
                     .setTransactionId(refundNotification.getTransactionId())
@@ -469,7 +475,7 @@ public class WxPayServiceImpl implements WxPayService {
             refundLog.setCreateTime(LocalDateTimeUtil.of(DateUtil.parse(refundNotification.getCreateTime())));
             refundLog.setCreateTime(LocalDateTimeUtil.of(DateUtil.parse(refundNotification.getCreateTime())));
             refundLogService.updateById(refundLog);
             refundLogService.updateById(refundLog);
 
 
-            if (PayLog.STATUS_退款成功.equals(refundNotification.getRefundStatus().name())) {
+            if (RefundLog.STATUS_退款成功.equals(refundNotification.getRefundStatus().name())) {
                 // 冻结金额扣减此次退款金额
                 // 冻结金额扣减此次退款金额
                 accountService.lambdaUpdate().setSql(" frozen_amount = (frozen_amount - %d)".formatted(refundNotification.getAmount().getRefund().intValue())).eq(Account::getUserId, refundLog.getUserId()).update();
                 accountService.lambdaUpdate().setSql(" frozen_amount = (frozen_amount - %d)".formatted(refundNotification.getAmount().getRefund().intValue())).eq(Account::getUserId, refundLog.getUserId()).update();
                 // 更新资金流水
                 // 更新资金流水