mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-13 11:49:49 +08:00
update 优化代码
This commit is contained in:
@@ -156,7 +156,7 @@ public class PaymentTestController {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void processUnsplitRecordToDefaultMemberTest() {
|
public void processUnsplitRecordToDefaultMemberTest() {
|
||||||
adapayUnsplitRecordHandleService.processUnsplitRecordToDefaultMember(wechatAppId1, 20);
|
adapayUnsplitRecordHandleService.processUnsplitRecordToDefaultMember(wechatAppId1, 2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -925,7 +925,6 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
|
|||||||
private boolean ensureRefundBeforeSplit(AdapayUnsplitRecordVO item, String wechatAppId) {
|
private boolean ensureRefundBeforeSplit(AdapayUnsplitRecordVO item, String wechatAppId) {
|
||||||
String orderCode = item.getOrderCode();
|
String orderCode = item.getOrderCode();
|
||||||
String paymentId = item.getPaymentId();
|
String paymentId = item.getPaymentId();
|
||||||
// VO 中 refundAmount 来源于 adapay_unsplit_record.due_refund_amount,表示分账前必须完成的应退款金额。
|
|
||||||
BigDecimal dueRefundAmount = parseAmount(item.getRefundAmount());
|
BigDecimal dueRefundAmount = parseAmount(item.getRefundAmount());
|
||||||
if (dueRefundAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
if (dueRefundAmount.compareTo(BigDecimal.ZERO) <= 0) {
|
||||||
return true;
|
return true;
|
||||||
@@ -938,19 +937,30 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 先只统计成功退款金额;成功退款已足额时才允许继续后续分账。
|
// 以订单表最新退款金额为准,避免 VO 中的旧值导致误判
|
||||||
BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false);
|
BigDecimal freshDueRefund = defaultAmount(orderBasicInfo.getRefundAmount());
|
||||||
|
if (!isSameAmount(dueRefundAmount, freshDueRefund)) {
|
||||||
|
dueRefundAmount = freshDueRefund;
|
||||||
|
AdapayUnsplitRecord syncRecord = buildUpdateRecordByPaymentId(paymentId);
|
||||||
|
syncRecord.setDueRefundAmount(freshDueRefund);
|
||||||
|
adapayUnsplitRecordService.insertOrUpdateSelective(syncRecord);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从汇付 API 查询实际退款状态,避免本地回调延迟导致数据滞后
|
||||||
|
RefundAmountResult refundResult = queryRefundAmountFromAdapay(paymentId, wechatAppId);
|
||||||
|
BigDecimal refundedAmount = refundResult.refundedAmount;
|
||||||
|
BigDecimal acceptedRefundAmount = refundResult.acceptedRefundAmount;
|
||||||
|
|
||||||
updateRefundAmount(paymentId, refundedAmount);
|
updateRefundAmount(paymentId, refundedAmount);
|
||||||
if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
|
if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
|
||||||
markRefundResult(paymentId, HANDLE_FLAG_SUCCESS);
|
markRefundResult(paymentId, HANDLE_FLAG_SUCCESS);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 再统计已受理退款金额(成功 + 处理中),避免处理中退款未回调时重复发起退款。
|
// 已受理退款已足额,等待退款完成
|
||||||
BigDecimal acceptedRefundAmount = getRefundedAmount(orderBasicInfo, true);
|
|
||||||
if (acceptedRefundAmount.compareTo(dueRefundAmount) >= 0) {
|
if (acceptedRefundAmount.compareTo(dueRefundAmount) >= 0) {
|
||||||
markRefundResult(paymentId, HANDLE_FLAG_PROCESSING);
|
markRefundResult(paymentId, HANDLE_FLAG_PROCESSING);
|
||||||
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount);
|
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount, wechatAppId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 已受理退款仍不足时,只补发差额退款;补发后等待成功退款金额达到应退款金额。
|
// 已受理退款仍不足时,只补发差额退款;补发后等待成功退款金额达到应退款金额。
|
||||||
@@ -966,7 +976,7 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
|
|||||||
markRefundResult(paymentId, HANDLE_FLAG_PROCESSING);
|
markRefundResult(paymentId, HANDLE_FLAG_PROCESSING);
|
||||||
log.info("未分账数据先执行退款, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
|
log.info("未分账数据先执行退款, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
|
||||||
paymentId, orderCode, dueRefundAmount, acceptedRefundAmount, refundAmount);
|
paymentId, orderCode, dueRefundAmount, acceptedRefundAmount, refundAmount);
|
||||||
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount);
|
return waitRefundFullySucceeded(orderBasicInfo, paymentId, dueRefundAmount, wechatAppId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
markRefundResult(paymentId, HANDLE_FLAG_FAILED);
|
markRefundResult(paymentId, HANDLE_FLAG_FAILED);
|
||||||
log.error("未分账数据执行退款失败, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
|
log.error("未分账数据执行退款失败, paymentId:{}, orderCode:{}, dueRefundAmount:{}, refundedAmount:{}, refundAmount:{}",
|
||||||
@@ -975,7 +985,8 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean waitRefundFullySucceeded(OrderBasicInfo orderBasicInfo, String paymentId, BigDecimal dueRefundAmount) {
|
private boolean waitRefundFullySucceeded(OrderBasicInfo orderBasicInfo, String paymentId,
|
||||||
|
BigDecimal dueRefundAmount, String wechatAppId) {
|
||||||
for (int i = 1; i <= REFUND_WAIT_MAX_ATTEMPTS; i++) {
|
for (int i = 1; i <= REFUND_WAIT_MAX_ATTEMPTS; i++) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(REFUND_WAIT_INTERVAL_MILLIS);
|
Thread.sleep(REFUND_WAIT_INTERVAL_MILLIS);
|
||||||
@@ -985,8 +996,7 @@ public class AdapayUnsplitRecordHandleServiceImpl implements AdapayUnsplitRecord
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 这里仍然只认成功退款金额,处理中退款不满足“退款足额后再分账”的条件。
|
BigDecimal refundedAmount = queryRefundAmountFromAdapay(paymentId, wechatAppId).refundedAmount;
|
||||||
BigDecimal refundedAmount = getRefundedAmount(orderBasicInfo, false);
|
|
||||||
updateRefundAmount(paymentId, refundedAmount);
|
updateRefundAmount(paymentId, refundedAmount);
|
||||||
if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
|
if (refundedAmount.compareTo(dueRefundAmount) >= 0) {
|
||||||
markRefundResult(paymentId, HANDLE_FLAG_SUCCESS);
|
markRefundResult(paymentId, HANDLE_FLAG_SUCCESS);
|
||||||
|
|||||||
Reference in New Issue
Block a user