update 订单逻辑改造工厂模式

This commit is contained in:
2023-09-01 17:29:59 +08:00
parent c2be91227b
commit 45af011278
4 changed files with 139 additions and 2 deletions

View File

@@ -0,0 +1,19 @@
package com.jsowell.adapay.operation;
import lombok.*;
import java.math.BigDecimal;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PaymentRefundOperation extends AdapayOperationInfo{
private String paymentId;
private BigDecimal refundAmt;
private String wechatAppId;
private String memberId;
private String scenarioType;
private String orderCode;
}

View File

@@ -14,6 +14,7 @@ import com.jsowell.adapay.config.AbstractAdapayConfig;
import com.jsowell.adapay.factory.AdapayConfigFactory;
import com.jsowell.adapay.operation.PaymentReverseOperation;
import com.jsowell.adapay.response.PaymentReverseResponse;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.TransactionRecordsData;
import com.jsowell.common.enums.AcquirerEnum;
@@ -493,7 +494,51 @@ public class DelayMerchantOrderLogic extends AbstractOrderLogic {
*/
@Override
public void refundBalance(ApplyRefundDTO dto) {
// 做个拦截,如果前一笔退款没有完成,就抛异常
String redisKey = CacheConstants.MEMBER_BALANCE_REFUNDS_ARE_IN_PROGRESS + dto.getMemberId();
PaymentReverseResponse redisResult = redisCache.getCacheObject(redisKey);
if (Objects.nonNull(redisResult)) {
throw new BusinessException(ReturnCodeEnum.CODE_BALANCE_REFUNDS_ARE_IN_PROGRESS_ERROR);
}
// 查会员余额
MemberVO memberVO = memberBasicInfoService.queryMemberInfoByMemberId(dto.getMemberId());
if (memberVO == null) {
throw new BusinessException(ReturnCodeEnum.CODE_MEMBER_NOT_FOUND_ERROR);
}
// 校验退款金额
BigDecimal principalBalance = memberVO.getPrincipalBalance();
BigDecimal refundAmount = dto.getRefundAmount();
if (refundAmount.compareTo(principalBalance) > 0) {
throw new BusinessException(ReturnCodeEnum.CODE_REFUND_MEMBER_BALANCE_ERROR);
}
// 查询用户充值余额订单 过滤掉已经退款的充值订单
List<BalanceDeductionAmountVO> list = calculateTheBalanceDeductionAmount(dto.getMemberId(), refundAmount);
for (BalanceDeductionAmountVO vo : list) {
String paymentId = vo.getPaymentId();
BigDecimal deductionAmount = vo.getDeductionAmount();
// 调汇付的交易撤销接口
// PaymentReverseResponse paymentReverseRequest = adapayService.createPaymentReverseRequest(paymentId, deductionAmount, dto.getWechatAppId(),
// dto.getMemberId(), ScenarioEnum.BALANCE.getValue(), null);
PaymentReverseOperation operation = new PaymentReverseOperation();
operation.setPaymentId(paymentId);
operation.setReverseAmt(deductionAmount);
operation.setMerchantKey(dto.getWechatAppId());
operation.setMemberId(dto.getMemberId());
operation.setScenarioType(ScenarioEnum.BALANCE.getValue());
PaymentReverseResponse paymentReverseRequest = adapayService.createPaymentReverseRequest(operation);
if (paymentReverseRequest != null && paymentReverseRequest.isNotFailed()) {
memberAdapayRecordService.updateRefundAmount(paymentId, deductionAmount);
// 放缓存
redisCache.setCacheObject(redisKey, paymentReverseRequest, CacheConstants.cache_expire_time_30m);
}
}
}
/**

View File

@@ -12,6 +12,7 @@ import com.jsowell.adapay.common.CreateAdaPaymentParam;
import com.jsowell.adapay.config.AbstractAdapayConfig;
import com.jsowell.adapay.factory.AdapayConfigFactory;
import com.jsowell.adapay.response.RefundResponse;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.TransactionRecordsData;
import com.jsowell.common.enums.AcquirerEnum;
@@ -465,7 +466,43 @@ public class NotDelayMerchantOrderLogic extends AbstractOrderLogic{
*/
@Override
public void refundBalance(ApplyRefundDTO dto) {
// 做个拦截,如果前一笔退款没有完成,就抛异常
String redisKey = CacheConstants.MEMBER_BALANCE_REFUNDS_ARE_IN_PROGRESS + dto.getMemberId();
RefundResponse redisResult = redisCache.getCacheObject(redisKey);
if (Objects.nonNull(redisResult)) {
throw new BusinessException(ReturnCodeEnum.CODE_BALANCE_REFUNDS_ARE_IN_PROGRESS_ERROR);
}
// 查会员余额
MemberVO memberVO = memberBasicInfoService.queryMemberInfoByMemberId(dto.getMemberId());
if (memberVO == null) {
throw new BusinessException(ReturnCodeEnum.CODE_MEMBER_NOT_FOUND_ERROR);
}
// 校验退款金额
BigDecimal principalBalance = memberVO.getPrincipalBalance();
BigDecimal refundAmount = dto.getRefundAmount();
if (refundAmount.compareTo(principalBalance) > 0) {
throw new BusinessException(ReturnCodeEnum.CODE_REFUND_MEMBER_BALANCE_ERROR);
}
// 查询用户充值余额订单 过滤掉已经退款的充值订单
List<BalanceDeductionAmountVO> list = calculateTheBalanceDeductionAmount(dto.getMemberId(), refundAmount);
for (BalanceDeductionAmountVO vo : list) {
String paymentId = vo.getPaymentId();
BigDecimal deductionAmount = vo.getDeductionAmount();
// 调汇付的交易退款接口
RefundResponse refundRequest = adapayService.createRefundRequest(paymentId, refundAmount,
dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(), dto.getOrderCode());
if (refundRequest != null && refundRequest.isNotFailed()) {
memberAdapayRecordService.updateRefundAmount(paymentId, deductionAmount);
// 放缓存
redisCache.setCacheObject(redisKey, refundRequest, CacheConstants.cache_expire_time_30m);
}
}
}
// uniApp 发送停止充电订阅消息