mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 02:55:04 +08:00
优化订单退款逻辑
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.jsowell.adapay.response;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* 退款请求返回的对象
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class RefundResponse {
|
||||
/**
|
||||
* 由 Adapay 生成的退款对象 id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 退款对象创建时间
|
||||
*/
|
||||
private String created_time;
|
||||
|
||||
/**
|
||||
* 退款目标支付对象 id
|
||||
*/
|
||||
private String payment_id;
|
||||
|
||||
/**
|
||||
* 退款金额(必须大于 0),保留两位小数点,如0.10、100.05等
|
||||
*/
|
||||
private String refund_amt;
|
||||
|
||||
/**
|
||||
* 退款状态,S-成功,F-失败,P-处理中
|
||||
*/
|
||||
private String trans_state;
|
||||
|
||||
/**
|
||||
* 退款手续费
|
||||
*/
|
||||
private String fee_amt;
|
||||
|
||||
/**
|
||||
* 当前交易状态,参见 状态 说明
|
||||
*/
|
||||
private String status;
|
||||
}
|
||||
@@ -68,6 +68,7 @@ public class AdapayMemberService {
|
||||
|
||||
/**
|
||||
* 创建结算账户
|
||||
*
|
||||
* @param dto
|
||||
* @throws BaseAdaPayException
|
||||
* @throws BusinessException
|
||||
@@ -88,7 +89,7 @@ public class AdapayMemberService {
|
||||
* @throws Exception
|
||||
*/
|
||||
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
|
||||
public void createMember(SettleAccountDTO dto) throws BaseAdaPayException, BusinessException {
|
||||
public void createMember(SettleAccountDTO dto) throws BaseAdaPayException, BusinessException {
|
||||
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(dto.getMerchantId());
|
||||
if (adapayMemberAccount != null) {
|
||||
log.error("通过merchantId:{}, 没有查询到结算账户配置", dto.getMerchantId());
|
||||
@@ -449,6 +450,7 @@ public class AdapayMemberService {
|
||||
|
||||
/**
|
||||
* 提现逻辑
|
||||
*
|
||||
* @param dto
|
||||
* @throws BaseAdaPayException
|
||||
*/
|
||||
@@ -511,6 +513,7 @@ public class AdapayMemberService {
|
||||
|
||||
/**
|
||||
* 更新汇付会员信息
|
||||
*
|
||||
* @param dto
|
||||
* @return
|
||||
* @throws BaseAdaPayException
|
||||
@@ -553,10 +556,11 @@ public class AdapayMemberService {
|
||||
/**
|
||||
* 创建交易确认请求/创建分账请求
|
||||
* 这个方法只适用于给一个用户分账
|
||||
* @param paymentId 支付对象id
|
||||
*
|
||||
* @param paymentId 支付对象id
|
||||
* @param adapayMemberAccount 收到该张的汇付会员信息
|
||||
* @param confirmAmt 确认的金额
|
||||
* @param orderCode 订单编号
|
||||
* @param confirmAmt 确认的金额
|
||||
* @param orderCode 订单编号
|
||||
*/
|
||||
public PaymentConfirmResponse createPaymentConfirmRequest(String paymentId, AdapayMemberAccount adapayMemberAccount, BigDecimal confirmAmt, String orderCode) {
|
||||
// 调汇付的分账接口 确认交易
|
||||
@@ -591,13 +595,14 @@ public class AdapayMemberService {
|
||||
|
||||
/**
|
||||
* 创建余额支付请求
|
||||
*
|
||||
* @param outMemberId 出账用户的member_id, 若为商户本身时,请传入0
|
||||
* @param inMemberId 入账用户的member_id, 若为商户本身时,请传入0
|
||||
* @param transAmt 交易金额, 必须大于0,保留两位小数点,如0.10、100.05等
|
||||
* @param title 标题
|
||||
* @param desc 描述信息
|
||||
* @param inMemberId 入账用户的member_id, 若为商户本身时,请传入0
|
||||
* @param transAmt 交易金额, 必须大于0,保留两位小数点,如0.10、100.05等
|
||||
* @param title 标题
|
||||
* @param desc 描述信息
|
||||
*/
|
||||
public BalancePaymentResponse createBalancePaymentRequest(String outMemberId, String inMemberId, String transAmt, String title, String desc){
|
||||
public BalancePaymentResponse createBalancePaymentRequest(String outMemberId, String inMemberId, String transAmt, String title, String desc) {
|
||||
Map<String, Object> balanceParam = Maps.newHashMap();
|
||||
balanceParam.put("app_id", ADAPAY_APP_ID);
|
||||
balanceParam.put("adapay_func_code", "settle_accounts.balancePay");
|
||||
@@ -634,18 +639,21 @@ public class AdapayMemberService {
|
||||
/**
|
||||
* 创建退款请求
|
||||
*/
|
||||
public void createRefundRequest(String paymentId, BigDecimal refundAmt) {
|
||||
public RefundResponse createRefundRequest(String paymentId, BigDecimal refundAmt) {
|
||||
// 延迟分账确认的调退款接口
|
||||
Map<String, Object> refundParams = Maps.newHashMap();
|
||||
refundParams.put("refund_amt", AdapayUtil.formatAmount(refundAmt));
|
||||
refundParams.put("refund_order_no", IdUtils.fastSimpleUUID());
|
||||
refundParams.put("notify_url", ADAPAY_CALLBACK_URL);
|
||||
Map<String, Object> resultResponse = null;
|
||||
try {
|
||||
Map<String, Object> response = Refund.create(paymentId, refundParams);
|
||||
log.info("汇付支付创建退款对象:{}", JSON.toJSONString(response));
|
||||
resultResponse = Refund.create(paymentId, refundParams);
|
||||
} catch (BaseAdaPayException e) {
|
||||
log.error("汇付支付创建退款对象error", e);
|
||||
}
|
||||
String jsonString = JSON.toJSONString(resultResponse);
|
||||
log.info("汇付支付创建退款对象param:{}, result:{}", JSON.toJSONString(refundParams), JSON.toJSONString(jsonString));
|
||||
return JSONObject.parseObject(jsonString, RefundResponse.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@ import com.jsowell.adapay.common.CreateAdaPaymentParam;
|
||||
import com.jsowell.adapay.common.DivMember;
|
||||
import com.jsowell.adapay.response.BalancePaymentResponse;
|
||||
import com.jsowell.adapay.response.PaymentConfirmResponse;
|
||||
import com.jsowell.adapay.response.PaymentReverseResponse;
|
||||
import com.jsowell.adapay.service.AdapayMemberService;
|
||||
import com.jsowell.adapay.vo.OrderSettleResult;
|
||||
import com.jsowell.common.constant.CacheConstants;
|
||||
@@ -825,7 +826,9 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
||||
* @param residue 需退款金额
|
||||
*/
|
||||
private void refundOrder(OrderBasicInfo orderBasicInfo, BigDecimal residue) {
|
||||
// 订单编号
|
||||
String orderCode = orderBasicInfo.getOrderCode();
|
||||
// 订单消费金额
|
||||
BigDecimal orderAmount = orderBasicInfo.getOrderAmount();
|
||||
// 查支付记录
|
||||
List<OrderPayRecord> payRecordList = orderPayRecordService.getOrderPayRecordList(orderCode);
|
||||
@@ -2314,15 +2317,16 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
||||
public void refundOrderWithAdapay(ApplyRefundDTO dto) {
|
||||
logger.info("汇付支付订单:{}, 执行退款逻辑 param:{}", dto.getOrderCode(), JSON.toJSONString(dto));
|
||||
// 查出来原来的支付信息
|
||||
AdapayCallbackRecord record = adapayCallbackRecordService.selectByOrderCode(dto.getOrderCode());
|
||||
if (Objects.isNull(record)) {
|
||||
AdapayCallbackRecord callbackRecord = adapayCallbackRecordService.selectByOrderCode(dto.getOrderCode());
|
||||
if (Objects.isNull(callbackRecord)) {
|
||||
logger.error("汇付支付orderCode:{}, 订单退款处理逻辑, 查询订单微信支付记录为空!", dto.getOrderCode());
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_REFUND_ORDER_CALLBACK_RECORD_ERROR);
|
||||
}
|
||||
|
||||
// 判断支付金额和退款金额
|
||||
String paymentId = callbackRecord.getPaymentId();
|
||||
BigDecimal refundAmount = dto.getRefundAmount();
|
||||
BigDecimal payAmt = record.getPayAmt();
|
||||
BigDecimal payAmt = callbackRecord.getPayAmt();
|
||||
if (refundAmount.compareTo(payAmt) > 0) {
|
||||
logger.error("汇付支付订单号:{}, 退款金额:{}(元),大于可退金额{}(元), 抛出异常", dto.getOrderCode(), refundAmount, payAmt);
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_REFUND_ORDER_AMOUNT_ERROR);
|
||||
@@ -2334,10 +2338,27 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
||||
String payMode = "delay";
|
||||
if (StringUtils.equals("", payMode)) {
|
||||
// 延迟分账确认的调退款接口
|
||||
adapayMemberService.createRefundRequest(record.getPaymentId(), dto.getRefundAmount());
|
||||
adapayMemberService.createRefundRequest(paymentId, refundAmount);
|
||||
} else {
|
||||
// 延迟分账未确认调撤销调撤销接口退款
|
||||
adapayMemberService.createPaymentReverseRequest(record.getPaymentId(), dto.getRefundAmount());
|
||||
PaymentReverseResponse response = adapayMemberService.createPaymentReverseRequest(paymentId, refundAmount);
|
||||
if (response != null) {
|
||||
MemberAdapayRecord record = memberAdapayRecordService.selectByPaymentId(paymentId);
|
||||
BigDecimal reverseAmt = new BigDecimal(response.getReverse_amt());
|
||||
// 更新此笔交易单的消费金额 = 支付金额 - 撤销金额
|
||||
BigDecimal spendAmt = callbackRecord.getPayAmt().subtract(reverseAmt);
|
||||
record.setSpendAmt(spendAmt);
|
||||
// 退款金额
|
||||
record.setRefundAmt(reverseAmt);
|
||||
// 更新此笔交易单的剩余金额 = 支付金额 - 累计退款金额 - 累计消费金额
|
||||
record.setBalanceAmt(record.getPayAmt().subtract(record.getRefundAmt()).subtract(record.getSpendAmt()));
|
||||
if (BigDecimal.ZERO.compareTo(record.getBalanceAmt()) != 0) {
|
||||
logger.error("订单分账结束后账不平,paymentId:{}, orderCode:{}, 支付金额:{}, 消费金额:{}, 退款金额:{}",
|
||||
paymentId, dto.getOrderCode(), payAmt, spendAmt, reverseAmt);
|
||||
}
|
||||
memberAdapayRecordService.updateByPrimaryKeySelective(record);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user