交易撤销重构

This commit is contained in:
2023-08-26 18:38:00 +08:00
parent 7529dd80b2
commit da47be42f9
3 changed files with 95 additions and 12 deletions

View File

@@ -0,0 +1,32 @@
package com.jsowell.adapay.operation;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.math.BigDecimal;
/**
* 支付撤销操作对象
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PaymentReverseOperation extends AdapayOperationInfo{
// 支付id
private String paymentId;
// 撤销金额
private BigDecimal reverseAmt;
// order/balance
private String scenarioType;
// scenarioType = balance 会员id不为空
private String memberId;
// scenarioType = order 订单编号不为空
private String orderCode;
}

View File

@@ -14,6 +14,7 @@ import com.jsowell.adapay.config.AbstractAdapayConfig;
import com.jsowell.adapay.dto.*;
import com.jsowell.adapay.factory.AdapayConfigFactory;
import com.jsowell.adapay.operation.PaymentConfirmOperation;
import com.jsowell.adapay.operation.PaymentReverseOperation;
import com.jsowell.adapay.response.*;
import com.jsowell.adapay.vo.*;
import com.jsowell.common.constant.CacheConstants;
@@ -952,10 +953,27 @@ public class AdapayService {
/**
* 创建交易撤销请求
* 延迟分账未确认, 调交易撤销接口退款
*
* delay模式的商户使用
*/
public PaymentReverseResponse createPaymentReverseRequest(PaymentReverseOperation paymentReverseOperation) {
String wechatAppId = paymentReverseOperation.getMerchantKey();
String paymentId = paymentReverseOperation.getPaymentId();
BigDecimal reverseAmt = paymentReverseOperation.getReverseAmt();
String memberId = paymentReverseOperation.getMemberId();
String scenarioType = paymentReverseOperation.getScenarioType();
String orderCode = paymentReverseOperation.getOrderCode();
return createPaymentReverseRequest(paymentId, reverseAmt, wechatAppId, memberId, scenarioType, orderCode);
}
/**
* 创建交易撤销请求
* 延迟分账未确认, 调交易撤销接口退款
* delay模式的商户使用
* @param wechatAppId 微信小程序appId
*/
public PaymentReverseResponse createPaymentReverseRequest(String paymentId, BigDecimal reverseAmt, String wechatAppId, String memberId, String scenarioType, String orderCode) {
public PaymentReverseResponse createPaymentReverseRequest(String paymentId, BigDecimal reverseAmt,
String wechatAppId, String memberId, String scenarioType,
String orderCode) {
AbstractAdapayConfig config = AdapayConfigFactory.getConfig(wechatAppId);
if (config == null) {
throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_CONFIG_IS_NULL_ERROR);
@@ -967,6 +985,7 @@ public class AdapayService {
reverseParams.put("reverse_amt", AdapayUtil.formatAmount(reverseAmt));
reverseParams.put("order_no", IdUtils.fastSimpleUUID());
reverseParams.put("notify_url", ADAPAY_CALLBACK_URL);
// expand 为扩展域
Map<String, String> expendMap = Maps.newHashMap();
expendMap.put("memberId", memberId);
@@ -976,6 +995,7 @@ public class AdapayService {
}
reverseParams.put("expand", expendMap);
reverseParams.put("reason", expendMap);
Map<String, Object> paymentReverse = null;
try {
paymentReverse = PaymentReverse.create(reverseParams, config.getWechatAppId());

View File

@@ -9,6 +9,7 @@ import com.google.common.collect.Maps;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
import com.jsowell.adapay.operation.PaymentConfirmOperation;
import com.jsowell.adapay.operation.PaymentReverseOperation;
import com.jsowell.adapay.response.PaymentConfirmResponse;
import com.jsowell.adapay.response.PaymentReverseResponse;
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
@@ -785,7 +786,7 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
/**
* 不延迟商家订单处理逻辑
* 例如:希晓
*
* 相当于已经分账完成了
* @param orderBasicInfo 订单信息
* @param adapayMemberAccount 汇付用户信息
* @param wechatAppId 小程序appId
@@ -1234,6 +1235,9 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
BigDecimal deductionAmount = vo.getDeductionAmount();
// 延时分账使用确认交易API
// PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, deductionAmount, orderCode, wechatAppId);
// 换新的方法,逻辑不变,参数封装了
PaymentConfirmOperation operation = new PaymentConfirmOperation();
operation.setPaymentId(paymentId);
operation.setConfirmAmt(deductionAmount);
@@ -1242,7 +1246,6 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
operation.setMerchantKey(wechatAppId);
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(operation);
// PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, deductionAmount, orderCode, wechatAppId);
if (paymentConfirmResponse != null && paymentConfirmResponse.isNotFailed()) {
confirmAmt = confirmAmt.add(new BigDecimal(paymentConfirmResponse.getConfirm_amt()));
feeAmt = feeAmt.add(new BigDecimal(paymentConfirmResponse.getFee_amt()));
@@ -2369,9 +2372,19 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
// String payMode = "delay";
if (StringUtils.equalsIgnoreCase(payMode, Constants.ADAPAY_PAY_MODE_DELAY)) {
// 延迟分账未确认调撤销调撤销接口退款
PaymentReverseResponse response = adapayService.createPaymentReverseRequest(
paymentId, refundAmount, dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(),
dto.getOrderCode());
// PaymentReverseResponse response = adapayService.createPaymentReverseRequest(
// paymentId, refundAmount, dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(),
// dto.getOrderCode());
PaymentReverseOperation operation = new PaymentReverseOperation();
operation.setPaymentId(paymentId);
operation.setReverseAmt(refundAmount);
operation.setMerchantKey(dto.getWechatAppId());
operation.setMemberId(dto.getMemberId());
operation.setScenarioType(ScenarioEnum.ORDER.getValue());
operation.setOrderCode(dto.getOrderCode());
PaymentReverseResponse response = adapayService.createPaymentReverseRequest(operation);
if (response != null && response.isNotFailed()) {
MemberAdapayRecord record = memberAdapayRecordService.selectByPaymentId(paymentId);
BigDecimal reverseAmt = new BigDecimal(response.getReverse_amt());
@@ -2470,8 +2483,16 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
BigDecimal deductionAmount = vo.getDeductionAmount();
// 调汇付的交易撤销接口
PaymentReverseResponse paymentReverseRequest = adapayService.createPaymentReverseRequest(paymentId, deductionAmount, dto.getWechatAppId(),
dto.getMemberId(), ScenarioEnum.BALANCE.getValue(), null);
// 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);
@@ -2865,9 +2886,19 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
List<PaymentReverseResponse> paymentReverses = queryOrderAdapayRefund(orderBasicInfo);
if (CollectionUtils.isEmpty(paymentReverses)) {
// 如果没有退款过,重新执行一遍退款
PaymentReverseResponse response = adapayService.createPaymentReverseRequest(
paymentId, refundAmount, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(),
orderCode);
// PaymentReverseResponse response = adapayService.createPaymentReverseRequest(
// paymentId, refundAmount, wechatAppId, memberId, ScenarioEnum.ORDER.getValue(),
// orderCode);
PaymentReverseOperation operation = new PaymentReverseOperation();
operation.setPaymentId(paymentId);
operation.setReverseAmt(refundAmount);
operation.setMerchantKey(wechatAppId);
operation.setMemberId(memberId);
operation.setScenarioType(ScenarioEnum.ORDER.getValue());
operation.setOrderCode(orderCode);
PaymentReverseResponse response = adapayService.createPaymentReverseRequest(operation);
logger.info("重试订单退款response:{}", JSON.toJSONString(response));
} else {
BigDecimal reversedAmt = BigDecimal.ZERO;