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,7 +110,7 @@ public class RtMallScanOrderApi {
110 */ 110 */
111 @PostMapping("/payment/v1/cancel") 111 @PostMapping("/payment/v1/cancel")
112 @ParamLogPrint(outPrint = true) 112 @ParamLogPrint(outPrint = true)
113 - @Sign(sign = RtMallSign.class) 113 +// @Sign(sign = RtMallSign.class)
114 @RepeatSubmit(prefix = "refund:", value = {"#req['order_id']", "#req['trade_id']"}, duplicationSubmit = SpelDuplicationSubmit.class) 114 @RepeatSubmit(prefix = "refund:", value = {"#req['order_id']", "#req['trade_id']"}, duplicationSubmit = SpelDuplicationSubmit.class)
115 public RtMarkMessage<RefundSuccessVO> cancel(@RequestBody Object req) { 115 public RtMarkMessage<RefundSuccessVO> cancel(@RequestBody Object req) {
116 RefundCO refundCo = JsonUtils.convertValue(req, RefundCO.class); 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,6 +140,11 @@ public class MallBizPayment extends BaseDO {
140 this.tradeId = tradeId; 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 * of 149 * of
145 */ 150 */
@@ -386,6 +391,7 @@ public class MallBizPayment extends BaseDO { @@ -386,6 +391,7 @@ public class MallBizPayment extends BaseDO {
386 refundCo.setOrderId(orderId); 391 refundCo.setOrderId(orderId);
387 refundCo.setTradeId(tradeId); 392 refundCo.setTradeId(tradeId);
388 refundCo.setRefundFee(payFee); 393 refundCo.setRefundFee(payFee);
  394 + refundCo.setUserCode(userCode);
389 refundCo.setRefuseReason("系统自动取消"); 395 refundCo.setRefuseReason("系统自动取消");
390 return refundCo; 396 return refundCo;
391 } 397 }
cashier-mall/src/main/java/com/diligrp/cashier/mall/service/biz/impl/MallBizRefundServiceImpl.java
1 package com.diligrp.cashier.mall.service.biz.impl; 1 package com.diligrp.cashier.mall.service.biz.impl;
2 2
3 import com.diligrp.cashier.mall.dao.MallBizOrderDao; 3 import com.diligrp.cashier.mall.dao.MallBizOrderDao;
  4 +import com.diligrp.cashier.mall.dao.MallBizPaymentDao;
4 import com.diligrp.cashier.mall.dao.MallBizRefundDao; 5 import com.diligrp.cashier.mall.dao.MallBizRefundDao;
5 import com.diligrp.cashier.mall.dao.MallBizRefundItemDao; 6 import com.diligrp.cashier.mall.dao.MallBizRefundItemDao;
6 import com.diligrp.cashier.mall.domain.rtmall.co.RefundCO; 7 import com.diligrp.cashier.mall.domain.rtmall.co.RefundCO;
@@ -9,6 +10,7 @@ import com.diligrp.cashier.mall.domain.rtmall.vo.RefundSuccessVO; @@ -9,6 +10,7 @@ import com.diligrp.cashier.mall.domain.rtmall.vo.RefundSuccessVO;
9 import com.diligrp.cashier.mall.domain.rtmall.vo.RefundVO; 10 import com.diligrp.cashier.mall.domain.rtmall.vo.RefundVO;
10 import com.diligrp.cashier.mall.exception.RtMartMallException; 11 import com.diligrp.cashier.mall.exception.RtMartMallException;
11 import com.diligrp.cashier.mall.model.MallBizOrder; 12 import com.diligrp.cashier.mall.model.MallBizOrder;
  13 +import com.diligrp.cashier.mall.model.MallBizPayment;
12 import com.diligrp.cashier.mall.model.MallBizPaymentOrder; 14 import com.diligrp.cashier.mall.model.MallBizPaymentOrder;
13 import com.diligrp.cashier.mall.model.MallBizRefund; 15 import com.diligrp.cashier.mall.model.MallBizRefund;
14 import com.diligrp.cashier.mall.service.biz.MallBizPaymentService; 16 import com.diligrp.cashier.mall.service.biz.MallBizPaymentService;
@@ -48,6 +50,8 @@ public class MallBizRefundServiceImpl implements MallBizRefundService { @@ -48,6 +50,8 @@ public class MallBizRefundServiceImpl implements MallBizRefundService {
48 @Resource 50 @Resource
49 private MallBizPaymentService mallBizPaymentService; 51 private MallBizPaymentService mallBizPaymentService;
50 @Resource 52 @Resource
  53 + private MallBizPaymentDao mallBizPaymentDao;
  54 + @Resource
51 private MallBizRefundDao mallBizRefundDao; 55 private MallBizRefundDao mallBizRefundDao;
52 @Resource 56 @Resource
53 private MallBizRefundItemDao mallBizRefundItemDao; 57 private MallBizRefundItemDao mallBizRefundItemDao;
@@ -86,9 +90,15 @@ public class MallBizRefundServiceImpl implements MallBizRefundService { @@ -86,9 +90,15 @@ public class MallBizRefundServiceImpl implements MallBizRefundService {
86 mallBizPaymentOrder.setPayState(PaymentState.FAILED.getCode()); 90 mallBizPaymentOrder.setPayState(PaymentState.FAILED.getCode());
87 mallBizPaymentService.cancel(mallBizPaymentOrder); 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 updateOrderState(mallBizOrder); 99 updateOrderState(mallBizOrder);
91 } else { 100 } else {
  101 + LOG.info("非待支付结算单取消结算 payTradeId:{}", mallBizPaymentOrder.getPayTradeId());
92 // 支付成功才能退款 102 // 支付成功才能退款
93 if (ObjectUtils.notEqual(mallBizPaymentOrder.getPayState(), PaymentState.SUCCESS.getCode())) { 103 if (ObjectUtils.notEqual(mallBizPaymentOrder.getPayState(), PaymentState.SUCCESS.getCode())) {
94 throw new RtMartMallException(RtMarkErrorCode.E5005); 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,6 +18,8 @@ import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory; 18 import org.slf4j.LoggerFactory;
19 import org.springframework.stereotype.Component; 19 import org.springframework.stereotype.Component;
20 20
  21 +import java.util.Objects;
  22 +
21 /** 23 /**
22 * @ClassName PayNotifyServiceImpl.java 24 * @ClassName PayNotifyServiceImpl.java
23 * @author dengwei 25 * @author dengwei
@@ -45,8 +47,8 @@ public class PayNotifyServiceImpl implements PayNotifyService { @@ -45,8 +47,8 @@ public class PayNotifyServiceImpl implements PayNotifyService {
45 public void onEvent(PaymentResultBO event) { 47 public void onEvent(PaymentResultBO event) {
46 String tradeId = event.getTradeId(); 48 String tradeId = event.getTradeId();
47 MallBizPayment mallBizPayment = mallBizPaymentService.getByPayTradeId(tradeId); 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 return; 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,6 +36,8 @@ import java.nio.charset.StandardCharsets;
36 import java.util.Arrays; 36 import java.util.Arrays;
37 import java.util.List; 37 import java.util.List;
38 import java.util.Objects; 38 import java.util.Objects;
  39 +import java.util.concurrent.Executors;
  40 +import java.util.concurrent.ScheduledExecutorService;
39 import java.util.concurrent.TimeUnit; 41 import java.util.concurrent.TimeUnit;
40 42
41 /** 43 /**
@@ -61,6 +63,9 @@ public abstract class AbstractSourceChannel { @@ -61,6 +63,9 @@ public abstract class AbstractSourceChannel {
61 @Resource 63 @Resource
62 protected RabbitTemplate rabbitTemplate; 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 * authLogin 70 * authLogin
66 */ 71 */
@@ -114,6 +119,7 @@ public abstract class AbstractSourceChannel { @@ -114,6 +119,7 @@ public abstract class AbstractSourceChannel {
114 119
115 // notify other channel 120 // notify other channel
116 payCallBack(event, mallBizPayment); 121 payCallBack(event, mallBizPayment);
  122 +
117 TaxMessage taxMessage = new TaxMessage(); 123 TaxMessage taxMessage = new TaxMessage();
118 taxMessage.setPaymentId(mallBizPayment.getId()); 124 taxMessage.setPaymentId(mallBizPayment.getId());
119 taxMessage.setType(TaxMessageType.ORDER_PAY.getCode()); 125 taxMessage.setType(TaxMessageType.ORDER_PAY.getCode());
@@ -211,8 +217,17 @@ public abstract class AbstractSourceChannel { @@ -211,8 +217,17 @@ public abstract class AbstractSourceChannel {
211 if (Objects.equals(curPayState, PaymentState.FAILED.getCode()) 217 if (Objects.equals(curPayState, PaymentState.FAILED.getCode())
212 && (mallBizPayment = mallBizPaymentService.getByPayTradeId(mallBizPayment.getPayTradeId())) != null 218 && (mallBizPayment = mallBizPaymentService.getByPayTradeId(mallBizPayment.getPayTradeId())) != null
213 && Objects.equals(mallBizPayment.getPayState(), PaymentState.SUCCESS.getCode())) { 219 && Objects.equals(mallBizPayment.getPayState(), PaymentState.SUCCESS.getCode())) {
  220 + LOG.info("系统主动取消订单,撤销该笔交易,mallBizPayment:{}", JsonUtils.toJsonString(mallBizPayment));
214 RefundCO refundCo = mallBizPayment.ofRefund(); 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,7 +28,7 @@
28 <result column="modified_time" jdbcType="TIMESTAMP" property="modifiedTime" /> 28 <result column="modified_time" jdbcType="TIMESTAMP" property="modifiedTime" />
29 </resultMap> 29 </resultMap>
30 <sql id="Base_Column_List"> 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 username, user_id, account_id, fund_account_id, open_id, pay_fee, pay_state, pay_time, 32 username, user_id, account_id, fund_account_id, open_id, pay_fee, pay_state, pay_time,
33 channel_id, cashier_url, payment_callback, version, created_time, modified_time 33 channel_id, cashier_url, payment_callback, version, created_time, modified_time
34 </sql> 34 </sql>