【功能优化】支付:钱包充值的支付回调,增加“已支付”情况下的防重处理

This commit is contained in:
YunaiV 2024-09-24 09:34:52 +08:00
parent 57a934ae09
commit 3bde43b148
2 changed files with 54 additions and 45 deletions

View File

@ -115,18 +115,19 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService {
// 1.1 校验订单是否存在 // 1.1 校验订单是否存在
PayDemoOrderDO order = payDemoOrderMapper.selectById(id); PayDemoOrderDO order = payDemoOrderMapper.selectById(id);
if (order == null) { if (order == null) {
log.error("[updateDemoOrderPaid][order({}) payOrder({}) 不存在订单,请进行处理!]", id, payOrderId);
throw exception(DEMO_ORDER_NOT_FOUND); throw exception(DEMO_ORDER_NOT_FOUND);
} }
// 1.2 校验订单已支付 // 1.2 校验订单已支付
if (order.getPayStatus()) { if (order.getPayStatus()) {
// 特殊如果订单已支付且支付单号相同直接返回说明重复回调 // 特殊如果订单已支付且支付单号相同直接返回说明重复回调
if (ObjectUtil.equals(order.getPayOrderId(), payOrderId)) { if (ObjectUtil.equals(order.getPayOrderId(), payOrderId)) {
log.warn("[updateDemoOrderPaid][order({}) 已支付,且支付单号相同({}),直接返回]", id, payOrderId); log.warn("[updateDemoOrderPaid][order({}) 已支付,且支付单号相同({}),直接返回]", order, payOrderId);
return; return;
} }
// 异常支付单号不同说明支付单号错误 // 异常支付单号不同说明支付单号错误
log.error("[updateDemoOrderPaid][order({}) 支付单不匹配({})请进行处理order 数据是:{}]", log.error("[updateDemoOrderPaid][order({}) 支付单不匹配({})请进行处理order 数据是:{}]",
id, payOrderId, toJsonString(order)); order, payOrderId, toJsonString(order));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR);
} }
@ -153,24 +154,24 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService {
// 1. 校验支付单是否存在 // 1. 校验支付单是否存在
PayOrderRespDTO payOrder = payOrderApi.getOrder(payOrderId); PayOrderRespDTO payOrder = payOrderApi.getOrder(payOrderId);
if (payOrder == null) { if (payOrder == null) {
log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 不存在,请进行处理!]", order, payOrderId); log.error("[validatePayOrderPaid][order({}) payOrder({}) 不存在,请进行处理!]", order, payOrderId);
throw exception(PAY_ORDER_NOT_FOUND); throw exception(PAY_ORDER_NOT_FOUND);
} }
// 2.1 校验支付单已支付 // 2.1 校验支付单已支付
if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) { if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) {
log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]", log.error("[validatePayOrderPaid][order({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]",
order, payOrderId, toJsonString(payOrder)); order, payOrderId, toJsonString(payOrder));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_STATUS_NOT_SUCCESS); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_STATUS_NOT_SUCCESS);
} }
// 2.1 校验支付金额一致 // 2.1 校验支付金额一致
if (notEqual(payOrder.getPrice(), order.getPrice())) { if (notEqual(payOrder.getPrice(), order.getPrice())) {
log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 支付金额不匹配请进行处理order 数据是:{}payOrder 数据是:{}]", log.error("[validatePayOrderPaid][order({}) payOrder({}) 支付金额不匹配请进行处理order 数据是:{}payOrder 数据是:{}]",
order, payOrderId, toJsonString(order), toJsonString(payOrder)); order, payOrderId, toJsonString(order), toJsonString(payOrder));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_PRICE_NOT_MATCH); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_PRICE_NOT_MATCH);
} }
// 2.2 校验支付订单匹配二次 // 2.2 校验支付订单匹配二次
if (notEqual(payOrder.getMerchantOrderId(), order.getId().toString())) { if (notEqual(payOrder.getMerchantOrderId(), order.getId().toString())) {
log.error("[validateDemoOrderCanPaid][order({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]", log.error("[validatePayOrderPaid][order({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]",
order, payOrderId, toJsonString(payOrder)); order, payOrderId, toJsonString(payOrder));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR);
} }

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.pay.service.wallet;
import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -113,16 +114,28 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateWalletRechargerPaid(Long id, Long payOrderId) { public void updateWalletRechargerPaid(Long id, Long payOrderId) {
// 1.1 获取钱包充值记录 // 1.1 校验钱包充值是否存在
PayWalletRechargeDO walletRecharge = walletRechargeMapper.selectById(id); PayWalletRechargeDO recharge = walletRechargeMapper.selectById(id);
if (walletRecharge == null) { if (recharge == null) {
log.error("[updateWalletRechargerPaid][钱包充值记录不存在,钱包充值记录 id({})]", id); log.error("[updateWalletRechargerPaid][recharge({}) payOrder({}) 不存在充值订单,请进行处理!]", id, payOrderId);
throw exception(WALLET_RECHARGE_NOT_FOUND); throw exception(WALLET_RECHARGE_NOT_FOUND);
} }
// 1.2 校验钱包充值是否可以支付 // 1.2 校验钱包充值是否可以支付
PayOrderDO payOrderDO = validateWalletRechargerCanPaid(walletRecharge, payOrderId); if (recharge.getPayStatus()) {
// 特殊如果订单已支付且支付单号相同直接返回说明重复回调
if (ObjectUtil.equals(recharge.getPayOrderId(), payOrderId)) {
log.warn("[updateWalletRechargerPaid][recharge({}) 已支付,且支付单号相同({}),直接返回]", recharge, payOrderId);
return;
}
// 异常支付单号不同说明支付单号错误
log.error("[updateWalletRechargerPaid][recharge({}) 已支付,但是支付单号不同({}),请进行处理!]", recharge, payOrderId);
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
}
// 2. 更新钱包充值的支付状态 // 2. 校验支付订单的合法性
PayOrderDO payOrderDO = validatePayOrderPaid(recharge, payOrderId);
// 3. 更新钱包充值的支付状态
int updateCount = walletRechargeMapper.updateByIdAndPaid(id, false, int updateCount = walletRechargeMapper.updateByIdAndPaid(id, false,
new PayWalletRechargeDO().setId(id).setPayStatus(true).setPayTime(LocalDateTime.now()) new PayWalletRechargeDO().setId(id).setPayStatus(true).setPayTime(LocalDateTime.now())
.setPayChannelCode(payOrderDO.getChannelCode())); .setPayChannelCode(payOrderDO.getChannelCode()));
@ -130,14 +143,14 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID); throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID);
} }
// 3. 更新钱包余额 // 4. 更新钱包余额
// TODO @jason这样的话未来提现会不会把充值的也提现走哈类似先充 100 110然后提现 110 // TODO @jason这样的话未来提现会不会把充值的也提现走哈类似先充 100 110然后提现 110
// TODO 需要钱包中加个可提现余额 // TODO 需要钱包中加个可提现余额
payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(id), payWalletService.addWalletBalance(recharge.getWalletId(), String.valueOf(id),
PayWalletBizTypeEnum.RECHARGE, walletRecharge.getTotalPrice()); PayWalletBizTypeEnum.RECHARGE, recharge.getTotalPrice());
// 4. 发送订阅消息 // 5. 发送订阅消息
getSelf().sendWalletRechargerPaidMessage(payOrderId, walletRecharge); getSelf().sendWalletRechargerPaidMessage(payOrderId, recharge);
} }
@Async @Async
@ -267,43 +280,38 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
return wallet; return wallet;
} }
private PayOrderDO validateWalletRechargerCanPaid(PayWalletRechargeDO walletRecharge, Long payOrderId) { /**
// 1.1 校验充值记录的支付状态 * 校验支付订单的合法性
if (walletRecharge.getPayStatus()) { *
log.error("[validateWalletRechargerCanPaid][钱包({}) 不处于未支付状态! 钱包数据是:{}]", * @param recharge 充值订单
walletRecharge.getId(), toJsonString(walletRecharge)); * @param payOrderId 支付订单编号
throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID); * @return 支付订单
} */
// 1.2 校验支付订单匹配 private PayOrderDO validatePayOrderPaid(PayWalletRechargeDO recharge, Long payOrderId) {
if (notEqual(walletRecharge.getPayOrderId(), payOrderId)) { // 支付单号 // 1. 校验支付单是否存在
log.error("[validateWalletRechargerCanPaid][钱包({}) 支付单不匹配({}),请进行处理! 钱包数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(walletRecharge));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
}
// 2.1 校验支付单是否存在
PayOrderDO payOrder = payOrderService.getOrder(payOrderId); PayOrderDO payOrder = payOrderService.getOrder(payOrderId);
if (payOrder == null) { if (payOrder == null) {
log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 不存在,请进行处理!]", log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 不存在,请进行处理!]",
walletRecharge.getId(), payOrderId); recharge, payOrderId);
throw exception(PAY_ORDER_NOT_FOUND); throw exception(PAY_ORDER_NOT_FOUND);
} }
// 2.2 校验支付单已支付
// 2.1 校验支付单已支付
if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) { if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) {
log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]", log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(payOrder)); recharge, payOrderId, toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS); throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS);
} }
// 2.3 校验支付金额一致 // 2.2 校验支付金额一致
if (notEqual(payOrder.getPrice(), walletRecharge.getPayPrice())) { if (notEqual(payOrder.getPrice(), recharge.getPayPrice())) {
log.error("[validateDemoOrderCanPaid][钱包({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{}payOrder 数据是:{}]", log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{}payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(walletRecharge), toJsonString(payOrder)); recharge, payOrderId, toJsonString(recharge), toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH); throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH);
} }
// 2.4 校验支付订单的商户订单匹配 // 2.3 校验支付订单的商户订单匹配
if (notEqual(payOrder.getMerchantOrderId(), walletRecharge.getId().toString())) { if (notEqual(payOrder.getMerchantOrderId(), recharge.getId().toString())) {
log.error("[validateDemoOrderCanPaid][钱包({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]", log.error("[validatePayOrderPaid][充值订单({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(payOrder)); recharge, payOrderId, toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR); throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
} }
return payOrder; return payOrder;