Commit 04a265a7eed3faf89e1973fdef9f50ed284d8088

Authored by 邓伟
1 parent e7e60463

fix cancel before pay

cashier-mall/src/main/java/com/diligrp/cashier/mall/domain/rtmall/co/RefundCO.java
... ... @@ -4,7 +4,6 @@ import com.diligrp.cashier.mall.domain.rtmall.RtMarkBaseCO;
4 4 import com.fasterxml.jackson.annotation.JsonAlias;
5 5 import jakarta.validation.Valid;
6 6 import jakarta.validation.constraints.NotBlank;
7   -import jakarta.validation.constraints.NotNull;
8 7  
9 8 import java.util.List;
10 9  
... ... @@ -50,6 +49,11 @@ public class RefundCO extends RtMarkBaseCO {
50 49 private String companyCode;
51 50  
52 51 /**
  52 + * 原因
  53 + */
  54 + private String refuseReason;
  55 +
  56 + /**
53 57 * 退款商品列表
54 58 */
55 59 @Valid
... ... @@ -111,4 +115,12 @@ public class RefundCO extends RtMarkBaseCO {
111 115 public void setReturnItemList(List<RefundItemCO> returnItemList) {
112 116 this.returnItemList = returnItemList;
113 117 }
  118 +
  119 + public String getRefuseReason() {
  120 + return refuseReason;
  121 + }
  122 +
  123 + public void setRefuseReason(String refuseReason) {
  124 + this.refuseReason = refuseReason;
  125 + }
114 126 }
... ...
cashier-mall/src/main/java/com/diligrp/cashier/mall/model/MallBizPayment.java
1 1 package com.diligrp.cashier.mall.model;
2 2  
3 3 import com.diligrp.cashier.mall.context.MallInitializeContext;
  4 +import com.diligrp.cashier.mall.domain.rtmall.co.RefundCO;
4 5 import com.diligrp.cashier.mall.domain.rtmall.vo.OrderPaymentVO;
5 6 import com.diligrp.cashier.mall.property.RtMallDynamicProperty;
6 7 import com.diligrp.cashier.mall.util.MallSnowflakeKeyManager;
... ... @@ -379,4 +380,13 @@ public class MallBizPayment extends BaseDO {
379 380 this.setChannelId(event.getOutPayType());
380 381 MallInitializeContext.getByPayChannel(this.getChannelId()).fill(this, event);
381 382 }
  383 +
  384 + public RefundCO ofRefund() {
  385 + RefundCO refundCo = new RefundCO();
  386 + refundCo.setOrderId(orderId);
  387 + refundCo.setTradeId(tradeId);
  388 + refundCo.setRefundFee(payFee);
  389 + refundCo.setRefuseReason("系统自动取消");
  390 + return refundCo;
  391 + }
382 392 }
... ...
cashier-mall/src/main/java/com/diligrp/cashier/mall/service/biz/impl/MallBizRefundServiceImpl.java
... ... @@ -121,7 +121,7 @@ public class MallBizRefundServiceImpl implements MallBizRefundService {
121 121 * doRefund
122 122 */
123 123 private RefundResultBO doRefund(MallBizRefund refund) {
124   - CashierRefundBO cashierRefundBO = new CashierRefundBO(refund.getPayTradeId(), refund.getRefundFee(), null, "用户主动取消!");
  124 + CashierRefundBO cashierRefundBO = new CashierRefundBO(refund.getPayTradeId(), refund.getRefundFee(), null, ObjectUtils.defaultIfNull(refund.getRefuseReason(), "用户主动取消!"));
125 125 LOG.info("doRefund cashierRefundBO: {}", JsonUtils.toJsonString(cashierRefundBO));
126 126 RefundResultBO refundResultBO = cashierDeskManager.doRefund(cashierRefundBO);
127 127 refund.setRefundTradeId(refundResultBO.getRefundId());
... ...
cashier-mall/src/main/java/com/diligrp/cashier/mall/service/sourcechannel/AbstractSourceChannel.java
... ... @@ -4,6 +4,7 @@ import cn.hutool.core.util.IdUtil;
4 4 import com.diligrp.cashier.mall.MallConstants;
5 5 import com.diligrp.cashier.mall.dao.MallBizOrderDao;
6 6 import com.diligrp.cashier.mall.domain.rtmall.co.AuthLoginCO;
  7 +import com.diligrp.cashier.mall.domain.rtmall.co.RefundCO;
7 8 import com.diligrp.cashier.mall.domain.rtmall.vo.UserInfoVO;
8 9 import com.diligrp.cashier.mall.domain.tax.TaxMessage;
9 10 import com.diligrp.cashier.mall.exception.RtMartMallException;
... ... @@ -32,7 +33,9 @@ import org.springframework.data.redis.core.RedisTemplate;
32 33 import org.springframework.transaction.annotation.Transactional;
33 34  
34 35 import java.nio.charset.StandardCharsets;
35   -import java.util.*;
  36 +import java.util.Arrays;
  37 +import java.util.List;
  38 +import java.util.Objects;
36 39 import java.util.concurrent.TimeUnit;
37 40  
38 41 /**
... ... @@ -99,6 +102,9 @@ public abstract class AbstractSourceChannel {
99 102 @Transactional(rollbackFor = {Exception.class})
100 103 public void paymentOnEvent(PaymentResultBO event, MallBizPayment mallBizPayment) {
101 104 LOG.info("paymentOnEvent event: {} mallBizPayment: {}", JsonUtils.toJsonString(event), JsonUtils.toJsonString(mallBizPayment));
  105 +
  106 + Integer curPayState = mallBizPayment.getPayState();
  107 +
102 108 // update mall_biz_payment
103 109 mallBizPayment.payCallBack(event);
104 110 mallBizPaymentService.updateByPay(mallBizPayment);
... ... @@ -115,6 +121,9 @@ public abstract class AbstractSourceChannel {
115 121 .setHeader("x-delay", MallConstants.WAIT_DELAY_MILLIS)
116 122 .build();
117 123 rabbitTemplate.convertAndSend(MallConstants.TAX_REPORT_DELAY_EXCHANGE, MallConstants.TAX_REPORT_DELAY_KEY, msg);
  124 +
  125 + // check pay success
  126 + checkPayHandler(curPayState, mallBizPayment);
118 127 }
119 128  
120 129 /**
... ... @@ -195,6 +204,19 @@ public abstract class AbstractSourceChannel {
195 204 }
196 205  
197 206 /**
  207 + * 如果payState=6表示系统主动取消,如果此时支付响应成功需要撤销该笔交易
  208 + */
  209 + private void checkPayHandler(final Integer curPayState,
  210 + MallBizPayment mallBizPayment) {
  211 + if (Objects.equals(curPayState, PaymentState.FAILED.getCode())
  212 + && (mallBizPayment = mallBizPaymentService.getByPayTradeId(mallBizPayment.getPayTradeId())) != null
  213 + && Objects.equals(mallBizPayment.getPayState(), PaymentState.SUCCESS.getCode())) {
  214 + RefundCO refundCo = mallBizPayment.ofRefund();
  215 + mallBizRefundService.refund(refundCo);
  216 + }
  217 + }
  218 +
  219 + /**
198 220 * 渠道
199 221 */
200 222 public abstract Integer source();
... ...