feat: 支持 0 元退款(仅作废权益不退钱)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
邹进
2026-10-22 11:17:29 +09:00
parent 1f28ac3151
commit 437d1c2c2c
5 changed files with 60 additions and 15 deletions

View File

@@ -63,6 +63,15 @@ func (s *Service) clawbackCommission(ctx context.Context, refundID uint) error {
if refund.Method == constants.RefundMethodOriginalRoute && refund.ChannelRefundStatus != constants.RefundChannelStatusSucceeded {
return errors.New(errors.CodeServiceUnavailable, "原路退款渠道结果尚未明确成功,暂不生成佣金回溯")
}
// 0 元退款只作废权益、不退钱,佣金不处理:与订单佣金是否已终态无关,直接闭合为无需回溯,
// 不能落到下方「成功金额非正」的可恢复失败而反复重试。原路退款的成功金额由渠道决定,不走这里。
if isZeroAmountRefund(&refund) {
return s.settleClawback(ctx, &refund, constants.AuditActionRefundClawbackNotRequired,
"0 元退款无需回溯佣金", clawbackEventID(refund.ID, "clawback-not-required"),
map[string]any{
"order_id": refund.OrderID, "zero_amount_refund": true, "clawback_required": false,
})
}
// 订单佣金未终态时等待:此时「无已发放佣金」只代表尚未计算完成或尚待人工修正,
// 提前判定为无需回溯会让后续入账的佣金永久失去回溯机会,违反「MUST NOT 提前判定为无需回溯」。
@@ -171,6 +180,12 @@ func (s *Service) clawbackCommission(ctx context.Context, refundID uint) error {
return s.applyClawback(ctx, &refund, allocations)
}
// isZeroAmountRefund 判定已通过的退款是否为 0 元退款:非原路方式(含方式为空的存量申请)且审批退款金额为 0。
func isZeroAmountRefund(refund *model.RefundRequest) bool {
return refund.Method != constants.RefundMethodOriginalRoute &&
refund.ApprovedRefundAmount != nil && *refund.ApprovedRefundAmount == 0
}
// clawbackEventID 生成回溯审计事件的稳定键,重复执行返回同一事件,审计写入因此天然幂等。
func clawbackEventID(refundID uint, suffix string) string {
return "refund:" + strconv.FormatUint(uint64(refundID), 10) + ":" + suffix