Browse Source

微信退款调试

skyline 2 năm trước cách đây
mục cha
commit
719ae4612e

+ 1 - 1
admin/src/main/java/com/kym/admin/AdminApplication.java

@@ -8,7 +8,7 @@ import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.FilterType;
 
 @SpringBootApplication()
-@ComponentScan(value = {"com.kym"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {WxPayServiceImpl.class}))
+@ComponentScan(value = {"com.kym"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE))
 @MapperScan(basePackages = {"com.kym.mapper"})
 public class AdminApplication {
 

+ 10 - 2
admin/src/main/java/com/kym/admin/controller/CustomController.java

@@ -8,7 +8,7 @@ import com.kym.entity.admin.queryParams.CustomChargeOrdersQueryParam;
 import com.kym.service.miniapp.ChargeOrderService;
 import com.kym.service.miniapp.PayLogService;
 import com.kym.service.miniapp.UserService;
-import com.kym.service.miniapp.WalletDetailService;
+import com.kym.service.wechat.WxPayService;
 import org.springframework.web.bind.annotation.*;
 
 /**
@@ -24,11 +24,13 @@ public class CustomController {
     private final UserService customUserService;
     private final ChargeOrderService customChargeOrders;
     private final PayLogService payLogService;
+    private final WxPayService wxPayService;
 
-    public CustomController(UserService customUserService, ChargeOrderService customChargeOrders, WalletDetailService customWalletDetailService, PayLogService payLogService) {
+    public CustomController(UserService customUserService, ChargeOrderService customChargeOrders, PayLogService payLogService, WxPayService wxPayService) {
         this.customUserService = customUserService;
         this.customChargeOrders = customChargeOrders;
         this.payLogService = payLogService;
+        this.wxPayService = wxPayService;
     }
 
     @SaCheckPermission("user.list")
@@ -62,5 +64,11 @@ public class CustomController {
         return R.success(payLogService.listRecharge(params, pageNum, pageSize));
     }
 
+    @SysLog("处理用户微信退款")
+    @GetMapping("/customWxRefund/{refundLogId}")
+    public R<?> customWxRefund(@PathVariable("refundLogId") long refundLogId) {
+        return R.success(wxPayService.wxRefund(refundLogId));
+    }
+
 
 }

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

@@ -24,6 +24,11 @@ public class PayLog extends BaseEntity implements Serializable {
 
     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
      */

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

@@ -11,10 +11,7 @@ import lombok.SneakyThrows;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -63,9 +60,9 @@ public class PaymentController {
 
 
     @ApiLog("微信退款")
-    @PostMapping("/wxRefund")
+    @GetMapping("/wxApplyRefund")
     @ResponseBody
-    R wxRefund() {
+    R wxAppRefund() {
         wxPayService.applyWxRefund();
         return R.success();
     }
@@ -88,6 +85,4 @@ public class PaymentController {
         logger.info("wxRefund notify result>>>:{}", resp);
         return resp;
     }
-
-
 }

+ 17 - 4
service/src/main/java/com/kym/service/wechat/impl/WxPayServiceImpl.java

@@ -7,6 +7,7 @@ import cn.hutool.core.date.LocalDateTimeUtil;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.RandomUtil;
 import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.dynamic.datasource.annotation.DS;
 import com.kym.common.config.WxPayConfig;
 import com.kym.common.constant.ResponseEnum;
 import com.kym.common.exception.BusinessException;
@@ -301,6 +302,7 @@ public class WxPayServiceImpl implements WxPayService {
      * 申请退款
      */
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void applyWxRefund() {
         var userId = StpUtil.getLoginIdAsLong();
         var chargeOrder = chargeOrderService.getChargingOrderByUserId(userId);
@@ -313,12 +315,20 @@ public class WxPayServiceImpl implements WxPayService {
         }
 
         // 将余额转移至冻结余额
-        accountService.lambdaUpdate().setSql(" frozen_amount = balance ,balance = 0").eq(Account::getUserId, userId);
+        accountService.lambdaUpdate().setSql(" frozen_amount = balance ,balance = 0").eq(Account::getUserId, userId).update();
 
         // 余额作为退款金额
         AtomicInteger refundAmount = new AtomicInteger(account.getBalance());
         // 充值记录
-        var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, "SUCCESS").orderByDesc(PayLog::getSuccessTime).list();
+        var payLogs = payLogService.lambdaQuery().eq(PayLog::getUserId, userId).eq(PayLog::getTradeState, PayLog.STATUS_退款成功).orderByDesc(PayLog::getSuccessTime).list();
+        // 充值金额综合不足以支付余额退款
+        if (!CommUtil.isEmptyOrNull(payLogs)) {
+            var total = payLogs.stream().mapToInt(PayLog::getTotal).sum();
+            if (total < account.getBalance()) {
+                LOGGER.error("用户:{},充值总金额:{},不足以支付余额退款金额:{}", userId, total, account.getBalance());
+                throw new BusinessException("充值金额不足以支付余额退款");
+            }
+        }
         // 最后一次的充值金额可以覆盖退款金额
         if (!CommUtil.isEmptyOrNull(payLogs) && payLogs.get(0).getTotal() >= refundAmount.get()) {
             // 退款日志
@@ -375,6 +385,7 @@ public class WxPayServiceImpl implements WxPayService {
      *
      * @return
      */
+    @DS("db-miniapp")
     @Override
     public Refund wxRefund(long refundLogId) {
         // 通过退款申请id获取退款申请记录
@@ -402,6 +413,8 @@ public class WxPayServiceImpl implements WxPayService {
         var amount = new AmountReq();
         // 退款金额
         amount.setRefund((long) refundLog.getRefund());
+        amount.setTotal((long) refundLog.getTotal());
+        amount.setCurrency(refundLog.getCurrency());
         request.setAmount(amount);
         return refundService.create(request);
     }
@@ -453,9 +466,9 @@ public class WxPayServiceImpl implements WxPayService {
             refundLog.setCreateTime(LocalDateTimeUtil.of(DateUtil.parse(refundNotification.getCreateTime())));
             refundLogService.updateById(refundLog);
 
-            if ("SUCCESS".equals(refundNotification.getRefundStatus().name())) {
+            if (PayLog.STATUS_退款成功.equals(refundNotification.getRefundStatus().name())) {
                 // 冻结金额扣减此次退款金额
-                accountService.lambdaUpdate().setSql(" frozen_amount = (frozen_amount - %d)".formatted(refundNotification.getAmount().getRefund().intValue())).eq(Account::getUserId, refundLog.getUserId());
+                accountService.lambdaUpdate().setSql(" frozen_amount = (frozen_amount - %d)".formatted(refundNotification.getAmount().getRefund().intValue())).eq(Account::getUserId, refundLog.getUserId()).update();
                 // 更新资金流水
                 var walletDetail = walletDetailService.getWalletDetailByOrderNo(refundNotification.getOutTradeNo(), WalletDetail.TYPE_提现);
                 walletDetail.setStatus(WalletDetail.STATUS_已确认)  //已确认

+ 0 - 0
miniapp/src/main/resources/cert/apiclient_cert.pem → service/src/main/resources/cert/apiclient_cert.pem


+ 0 - 0
miniapp/src/main/resources/cert/apiclient_key.pem → service/src/main/resources/cert/apiclient_key.pem