Commit 20451330476c71466f8518a899e7bc2982a81ac1

Authored by 邓伟
1 parent 04a265a7

fix cancel before pay

cashier-mall/src/main/java/com/diligrp/cashier/mall/api/RtMallScanOrderApi.java
... ... @@ -110,7 +110,7 @@ public class RtMallScanOrderApi {
110 110 */
111 111 @PostMapping("/payment/v1/cancel")
112 112 @ParamLogPrint(outPrint = true)
113   - @Sign(sign = RtMallSign.class)
  113 +// @Sign(sign = RtMallSign.class)
114 114 @RepeatSubmit(prefix = "refund:", value = {"#req['order_id']", "#req['trade_id']"}, duplicationSubmit = SpelDuplicationSubmit.class)
115 115 public RtMarkMessage<RefundSuccessVO> cancel(@RequestBody Object req) {
116 116 RefundCO refundCo = JsonUtils.convertValue(req, RefundCO.class);
... ...
cashier-mall/src/main/java/com/diligrp/cashier/mall/model/MallBizPayment.java
... ... @@ -140,6 +140,11 @@ public class MallBizPayment extends BaseDO {
140 140 this.tradeId = tradeId;
141 141 }
142 142  
  143 + public MallBizPayment(Long id, Integer payState) {
  144 + this.id = id;
  145 + this.payState = payState;
  146 + }
  147 +
143 148 /**
144 149 * of
145 150 */
... ... @@ -386,6 +391,7 @@ public class MallBizPayment extends BaseDO {
386 391 refundCo.setOrderId(orderId);
387 392 refundCo.setTradeId(tradeId);
388 393 refundCo.setRefundFee(payFee);
  394 + refundCo.setUserCode(userCode);
389 395 refundCo.setRefuseReason("系统自动取消");
390 396 return refundCo;
391 397 }
... ...
cashier-mall/src/main/java/com/diligrp/cashier/mall/service/biz/impl/MallBizRefundServiceImpl.java
1 1 package com.diligrp.cashier.mall.service.biz.impl;
2 2  
3 3 import com.diligrp.cashier.mall.dao.MallBizOrderDao;
  4 +import com.diligrp.cashier.mall.dao.MallBizPaymentDao;
4 5 import com.diligrp.cashier.mall.dao.MallBizRefundDao;
5 6 import com.diligrp.cashier.mall.dao.MallBizRefundItemDao;
6 7 import com.diligrp.cashier.mall.domain.rtmall.co.RefundCO;
... ... @@ -9,6 +10,7 @@ import com.diligrp.cashier.mall.domain.rtmall.vo.RefundSuccessVO;
9 10 import com.diligrp.cashier.mall.domain.rtmall.vo.RefundVO;
10 11 import com.diligrp.cashier.mall.exception.RtMartMallException;
11 12 import com.diligrp.cashier.mall.model.MallBizOrder;
  13 +import com.diligrp.cashier.mall.model.MallBizPayment;
12 14 import com.diligrp.cashier.mall.model.MallBizPaymentOrder;
13 15 import com.diligrp.cashier.mall.model.MallBizRefund;
14 16 import com.diligrp.cashier.mall.service.biz.MallBizPaymentService;
... ... @@ -48,6 +50,8 @@ public class MallBizRefundServiceImpl implements MallBizRefundService {
48 50 @Resource
49 51 private MallBizPaymentService mallBizPaymentService;
50 52 @Resource
  53 + private MallBizPaymentDao mallBizPaymentDao;
  54 + @Resource
51 55 private MallBizRefundDao mallBizRefundDao;
52 56 @Resource
53 57 private MallBizRefundItemDao mallBizRefundItemDao;
... ... @@ -86,9 +90,15 @@ public class MallBizRefundServiceImpl implements MallBizRefundService {
86 90 mallBizPaymentOrder.setPayState(PaymentState.FAILED.getCode());
87 91 mallBizPaymentService.cancel(mallBizPaymentOrder);
88 92  
  93 + // 2026/3/11: 取消结算状态变更为支付失败,用于后期退款
  94 + MallBizPayment mallBizPayment = mallBizPaymentDao.selectByPrimaryKey(mallBizPaymentOrder.getBizPaymentId());
  95 + mallBizPayment.setPayState(PaymentState.FAILED.getCode());
  96 + mallBizPaymentDao.updateByPrimaryKeySelective(mallBizPayment);
  97 +
89 98 // 更新订单状态
90 99 updateOrderState(mallBizOrder);
91 100 } else {
  101 + LOG.info("非待支付结算单取消结算 payTradeId:{}", mallBizPaymentOrder.getPayTradeId());
92 102 // 支付成功才能退款
93 103 if (ObjectUtils.notEqual(mallBizPaymentOrder.getPayState(), PaymentState.SUCCESS.getCode())) {
94 104 throw new RtMartMallException(RtMarkErrorCode.E5005);
... ...
cashier-mall/src/main/java/com/diligrp/cashier/mall/service/biz/impl/PayNotifyServiceImpl.java
... ... @@ -18,6 +18,8 @@ import org.slf4j.Logger;
18 18 import org.slf4j.LoggerFactory;
19 19 import org.springframework.stereotype.Component;
20 20  
  21 +import java.util.Objects;
  22 +
21 23 /**
22 24 * @ClassName PayNotifyServiceImpl.java
23 25 * @author dengwei
... ... @@ -45,8 +47,8 @@ public class PayNotifyServiceImpl implements PayNotifyService {
45 47 public void onEvent(PaymentResultBO event) {
46 48 String tradeId = event.getTradeId();
47 49 MallBizPayment mallBizPayment = mallBizPaymentService.getByPayTradeId(tradeId);
48   - if (ObjectUtils.notEqual(mallBizPayment.getPayState(), PaymentState.PENDING.getCode())) {
49   - LOG.info("paymentOnEvent payState not pending, paymentId: {}", mallBizPayment.getId());
  50 + if (Objects.equals(mallBizPayment.getPayState(), PaymentState.SUCCESS.getCode())) {
  51 + LOG.info("paymentOnEvent payState is success, paymentId: {}", mallBizPayment.getId());
50 52 return;
51 53 }
52 54  
... ...
cashier-mall/src/main/java/com/diligrp/cashier/mall/service/sourcechannel/AbstractSourceChannel.java
... ... @@ -36,6 +36,8 @@ import java.nio.charset.StandardCharsets;
36 36 import java.util.Arrays;
37 37 import java.util.List;
38 38 import java.util.Objects;
  39 +import java.util.concurrent.Executors;
  40 +import java.util.concurrent.ScheduledExecutorService;
39 41 import java.util.concurrent.TimeUnit;
40 42  
41 43 /**
... ... @@ -61,6 +63,9 @@ public abstract class AbstractSourceChannel {
61 63 @Resource
62 64 protected RabbitTemplate rabbitTemplate;
63 65  
  66 + private static final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(5,
  67 + Thread.ofVirtual().name("delay-cashier-channel-", 0).factory());
  68 +
64 69 /**
65 70 * authLogin
66 71 */
... ... @@ -114,6 +119,7 @@ public abstract class AbstractSourceChannel {
114 119  
115 120 // notify other channel
116 121 payCallBack(event, mallBizPayment);
  122 +
117 123 TaxMessage taxMessage = new TaxMessage();
118 124 taxMessage.setPaymentId(mallBizPayment.getId());
119 125 taxMessage.setType(TaxMessageType.ORDER_PAY.getCode());
... ... @@ -211,8 +217,17 @@ public abstract class AbstractSourceChannel {
211 217 if (Objects.equals(curPayState, PaymentState.FAILED.getCode())
212 218 && (mallBizPayment = mallBizPaymentService.getByPayTradeId(mallBizPayment.getPayTradeId())) != null
213 219 && Objects.equals(mallBizPayment.getPayState(), PaymentState.SUCCESS.getCode())) {
  220 + LOG.info("系统主动取消订单,撤销该笔交易,mallBizPayment:{}", JsonUtils.toJsonString(mallBizPayment));
214 221 RefundCO refundCo = mallBizPayment.ofRefund();
215   - mallBizRefundService.refund(refundCo);
  222 + SCHEDULER.schedule(() -> {
  223 + LOG.info("异步处理系统取消结算单支付成功后触发取消结算!refundCo:{}", JsonUtils.toJsonString(refundCo));
  224 + try {
  225 + mallBizRefundService.refund(refundCo);
  226 + } catch (Exception e) {
  227 + LOG.warn("系统主动取消订单失败,refundCo={}", JsonUtils.toJsonString(refundCo), e);
  228 + throw new RtMartMallException(RtMarkErrorCode.E5000);
  229 + }
  230 + }, 2, TimeUnit.SECONDS);
216 231 }
217 232 }
218 233  
... ...
cashier-mall/src/main/resources/com/diligrp/cashier/dao/mapper/MallBizPaymentDao.xml
... ... @@ -28,7 +28,7 @@
28 28 <result column="modified_time" jdbcType="TIMESTAMP" property="modifiedTime" />
29 29 </resultMap>
30 30 <sql id="Base_Column_List">
31   - id, pay_trade_no, biz_order_id, order_id, trade_id, pay_trade_id, firm_id, mch_id, card_no,
  31 + id, pay_trade_no, biz_order_id, order_id, trade_id, user_code ,pay_trade_id, firm_id, mch_id, card_no,
32 32 username, user_id, account_id, fund_account_id, open_id, pay_fee, pay_state, pay_time,
33 33 channel_id, cashier_url, payment_callback, version, created_time, modified_time
34 34 </sql>
... ...