汇付回调添加事务控制

This commit is contained in:
Guoqs
2024-09-03 17:03:52 +08:00
parent a6f1b48cd1
commit c0e26358a4
2 changed files with 91 additions and 91 deletions

View File

@@ -55,6 +55,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@@ -1093,7 +1094,8 @@ public class OrderService {
* @param data * @param data
* @throws JsonProcessingException * @throws JsonProcessingException
*/ */
private void paymentSucceeded(String data) throws JsonProcessingException { @Transactional(rollbackFor = Exception.class)
public void paymentSucceeded(String data) throws JsonProcessingException {
// 验签成功 保存到回调记录表中 // 验签成功 保存到回调记录表中
JSONObject jsonObject = JSON.parseObject(data); JSONObject jsonObject = JSON.parseObject(data);
log.info("adapay支付成功回调:{}", jsonObject.toJSONString()); log.info("adapay支付成功回调:{}", jsonObject.toJSONString());

View File

@@ -13,7 +13,6 @@ import com.jsowell.adapay.common.RefundInfo;
import com.jsowell.adapay.config.AbstractAdapayConfig; import com.jsowell.adapay.config.AbstractAdapayConfig;
import com.jsowell.adapay.factory.AdapayConfigFactory; import com.jsowell.adapay.factory.AdapayConfigFactory;
import com.jsowell.adapay.response.RefundResponse; import com.jsowell.adapay.response.RefundResponse;
import com.jsowell.adapay.vo.PaymentInfo;
import com.jsowell.common.constant.CacheConstants; import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants; import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.TransactionRecordsData; import com.jsowell.common.core.domain.ykc.TransactionRecordsData;
@@ -45,7 +44,6 @@ import java.text.ParseException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -130,94 +128,94 @@ public class NotDelayMerchantProgramLogic extends AbstractProgramLogic {
* *
* @param dto * @param dto
*/ */
@Transactional(rollbackFor = Exception.class) // @Transactional(rollbackFor = Exception.class)
public void balancePayOrder(PayOrderDTO dto) { // public void balancePayOrder(PayOrderDTO dto) {
logger.info("【{}】-余额支付订单start, param:{}", this.getClass().getSimpleName(), JSON.toJSONString(dto)); // logger.info("【{}】-余额支付订单start, param:{}", this.getClass().getSimpleName(), JSON.toJSONString(dto));
String orderCode = dto.getOrderCode(); // 订单编号 // String orderCode = dto.getOrderCode(); // 订单编号
BigDecimal chargeAmount = dto.getPayAmount(); // 支付金额 // BigDecimal chargeAmount = dto.getPayAmount(); // 支付金额
// 查询该会员的余额 // // 查询该会员的余额
MemberVO memberVO = memberBasicInfoService.queryMemberInfoByMemberId(dto.getMemberId()); // MemberVO memberVO = memberBasicInfoService.queryMemberInfoByMemberId(dto.getMemberId());
BigDecimal totalAccountAmount = memberVO.getPrincipalBalance(); // BigDecimal totalAccountAmount = memberVO.getPrincipalBalance();
//
if (totalAccountAmount.compareTo(chargeAmount) < 0) { // if (totalAccountAmount.compareTo(chargeAmount) < 0) {
// 总余额小于充电金额 // // 总余额小于充电金额
throw new BusinessException(ReturnCodeEnum.CODE_BALANCE_IS_INSUFFICIENT); // throw new BusinessException(ReturnCodeEnum.CODE_BALANCE_IS_INSUFFICIENT);
} // }
BigDecimal principalPay = chargeAmount; // BigDecimal principalPay = chargeAmount;
//
// 更新会员钱包 // // 更新会员钱包
UpdateMemberBalanceDTO updateMemberBalanceDTO = UpdateMemberBalanceDTO.builder() // UpdateMemberBalanceDTO updateMemberBalanceDTO = UpdateMemberBalanceDTO.builder()
.memberId(dto.getMemberId()) // .memberId(dto.getMemberId())
.type(MemberWalletEnum.TYPE_OUT.getValue()) // .type(MemberWalletEnum.TYPE_OUT.getValue())
.subType(MemberWalletEnum.SUBTYPE_PAYMENT_FOR_ORDER.getValue()) // .subType(MemberWalletEnum.SUBTYPE_PAYMENT_FOR_ORDER.getValue())
.updatePrincipalBalance(principalPay) // 使用本金支付的金额 // .updatePrincipalBalance(principalPay) // 使用本金支付的金额
.relatedOrderCode(orderCode) // .relatedOrderCode(orderCode)
.build(); // .build();
memberBasicInfoService.updateMemberBalance(updateMemberBalanceDTO); // memberBasicInfoService.updateMemberBalance(updateMemberBalanceDTO);
//
// 查询余额充值有剩余的记录 // // 查询余额充值有剩余的记录
List<BalanceDeductionAmountVO> list = calculateTheBalanceDeductionAmount(dto.getMemberId(), chargeAmount); // List<BalanceDeductionAmountVO> list = calculateTheBalanceDeductionAmount(dto.getMemberId(), chargeAmount);
// 记录支订单付流水 // // 记录支订单付流水
List<PaymentInfo> paymentInfos = Lists.newArrayList(); // List<PaymentInfo> paymentInfos = Lists.newArrayList();
BigDecimal deductionAmount = BigDecimal.ZERO; // BigDecimal deductionAmount = BigDecimal.ZERO;
for (BalanceDeductionAmountVO balanceDeductionAmountVO : list) { // for (BalanceDeductionAmountVO balanceDeductionAmountVO : list) {
String paymentId = balanceDeductionAmountVO.getPaymentId(); // String paymentId = balanceDeductionAmountVO.getPaymentId();
deductionAmount = deductionAmount.add(balanceDeductionAmountVO.getDeductionAmount()); // deductionAmount = deductionAmount.add(balanceDeductionAmountVO.getDeductionAmount());
//
PaymentInfo paymentInfo = new PaymentInfo(); // PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setPaymentId(paymentId); // paymentInfo.setPaymentId(paymentId);
paymentInfo.setAmount(deductionAmount.toString()); // paymentInfo.setAmount(deductionAmount.toString());
paymentInfos.add(paymentInfo); // paymentInfos.add(paymentInfo);
} // }
// 记录流水 // // 记录流水
OrderPayRecord orderPayRecord = OrderPayRecord.builder() // OrderPayRecord orderPayRecord = OrderPayRecord.builder()
.orderCode(orderCode) // .orderCode(orderCode)
.payMode(OrderPayRecordEnum.PRINCIPAL_BALANCE_PAYMENT.getValue()) // .payMode(OrderPayRecordEnum.PRINCIPAL_BALANCE_PAYMENT.getValue())
.payAmount(deductionAmount) // .payAmount(deductionAmount)
.acquirer(AcquirerEnum.LOCAL.getValue()) // .acquirer(AcquirerEnum.LOCAL.getValue())
.deductionRecord(JSON.toJSONString(paymentInfos)) // .deductionRecord(JSON.toJSONString(paymentInfos))
.createBy(dto.getMemberId()) // .createBy(dto.getMemberId())
.delFlag(DelFlagEnum.NORMAL.getValue()) // .delFlag(DelFlagEnum.NORMAL.getValue())
.build(); // .build();
//
// 订单支付流水入库 // // 订单支付流水入库
List<OrderPayRecord> payRecordList = Lists.newArrayList(orderPayRecord); // List<OrderPayRecord> payRecordList = Lists.newArrayList(orderPayRecord);
orderPayRecordService.batchInsert(payRecordList); // orderPayRecordService.batchInsert(payRecordList);
//
// 把消费金额冻结 // // 把消费金额冻结
for (OrderPayRecord record : payRecordList) { // for (OrderPayRecord record : payRecordList) {
List<PaymentInfo> paymentInfoList = orderPayRecordService.parseDeductionRecord(record.getDeductionRecord()); // List<PaymentInfo> paymentInfoList = orderPayRecordService.parseDeductionRecord(record.getDeductionRecord());
// 循环冻结金额 // // 循环冻结金额
for (PaymentInfo paymentInfo : paymentInfoList) { // for (PaymentInfo paymentInfo : paymentInfoList) {
String paymentId = paymentInfo.getPaymentId(); // String paymentId = paymentInfo.getPaymentId();
BigDecimal amount = new BigDecimal(paymentInfo.getAmount()); // BigDecimal amount = new BigDecimal(paymentInfo.getAmount());
// 余额支付 临时冻结金额 // // 余额支付 临时冻结金额
memberAdapayRecordService.updateFreezeAmount(paymentId, amount); // memberAdapayRecordService.updateFreezeAmount(paymentId, amount);
} // }
} // }
//
// 余额支付可以直接调支付回调方法 // // 余额支付可以直接调支付回调方法
PayOrderSuccessCallbackDTO callbackDTO = PayOrderSuccessCallbackDTO.builder() // PayOrderSuccessCallbackDTO callbackDTO = PayOrderSuccessCallbackDTO.builder()
.orderCode(orderCode) // .orderCode(orderCode)
.payAmount(chargeAmount) // .payAmount(chargeAmount)
.payMode(dto.getPayMode()) // .payMode(dto.getPayMode())
.startMode(dto.getStartMode()) // .startMode(dto.getStartMode())
.acquirer(AcquirerEnum.LOCAL.getValue()) // .acquirer(AcquirerEnum.LOCAL.getValue())
.build(); // .build();
payOrderSuccessCallback(callbackDTO); // payOrderSuccessCallback(callbackDTO);
//
// 余额支付订单 记录会员交易流水 // // 余额支付订单 记录会员交易流水
MemberTransactionRecord record = MemberTransactionRecord.builder() // MemberTransactionRecord record = MemberTransactionRecord.builder()
.orderCode(orderCode) // .orderCode(orderCode)
.scenarioType(ScenarioEnum.ORDER.getValue()) // .scenarioType(ScenarioEnum.ORDER.getValue())
.memberId(memberVO.getMemberId()) // .memberId(memberVO.getMemberId())
.actionType(ActionTypeEnum.FORWARD.getValue()) // .actionType(ActionTypeEnum.FORWARD.getValue())
.payMode(PayModeEnum.PAYMENT_OF_BALANCE.getValue()) // .payMode(PayModeEnum.PAYMENT_OF_BALANCE.getValue())
.paymentInstitutions(PaymentInstitutionsEnum.LOCAL_ACCOUNTS.getValue()) // .paymentInstitutions(PaymentInstitutionsEnum.LOCAL_ACCOUNTS.getValue())
.amount(dto.getPayAmount()) // 单位元 // .amount(dto.getPayAmount()) // 单位元
.build(); // .build();
memberTransactionRecordService.insertSelective(record); // memberTransactionRecordService.insertSelective(record);
} // }
/** /**
* 余额支付订单逻辑 * 余额支付订单逻辑