fix(payment): 渠道已收款时不再把支付单回滚成支付中
租客在待支付超时或取消的最后几十秒完成付款时,订单先被取消,乐刷的支付 结果几秒后才到,入账被判定订单不可支付而拒收。原先订单推进和支付单更新 在同一个事务里,订单一报错整笔回滚,支付单停在支付中且 paid_at 为空: 钱已在渠道,系统却毫不知情。乐刷重试十次后放弃,之后既补不回入账(订单 不再是待支付),后台人工退款也发不出去(退款要求原支付单是已支付)。 日志保留期内 13 天出了 5 笔,合计 4085.60 元。 - 取消前核对渠道:超时取消、租客取消、客服关单都先查一次渠道,已收款并 已推进订单就放弃取消,让支付正常入账而不是走退款。渠道异常时按未收款 继续,避免渠道一抖订单全卡住 - 回调兜底记账:订单确实已结束时不再回滚,把支付单如实记成已支付并写入 paid_at 与渠道单号,后台人工退款从此可用。退款仍由客服人工发起 - 客服关单:待支付订单按实际已收款减已退款的差额退,补上本次事故中关单 不生成退款的直接原因 - 补齐日志上下文:回调入账失败原先只有 error="order cannot pay",现在带 支付单号、订单号、订单状态和金额;新增 ERROR「渠道已收款但订单无法入账, 需人工退款」作为唯一需人工介入的告警 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -29,12 +29,18 @@ const (
|
||||
maxRenterRetentionDaysAfterOrderEnd = 7
|
||||
)
|
||||
|
||||
// ChannelPaymentProbe 由支付模块实现,用于取消前跟渠道核对订单是否已收款。
|
||||
type ChannelPaymentProbe interface {
|
||||
ConfirmChannelPaidBeforeClose(ctx context.Context, orderID uint64) (paid bool, err error)
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
logger *zap.Logger
|
||||
interval time.Duration
|
||||
instanceID string
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
logger *zap.Logger
|
||||
paymentProbe ChannelPaymentProbe
|
||||
interval time.Duration
|
||||
instanceID string
|
||||
}
|
||||
|
||||
type thresholds struct {
|
||||
@@ -46,13 +52,14 @@ type thresholds struct {
|
||||
RenterRetentionDaysAfterOrderEnd int
|
||||
}
|
||||
|
||||
func New(db *gorm.DB, redisClient *redis.Client, logger *zap.Logger) *Job {
|
||||
func New(db *gorm.DB, redisClient *redis.Client, logger *zap.Logger, paymentProbe ChannelPaymentProbe) *Job {
|
||||
return &Job{
|
||||
db: db,
|
||||
redis: redisClient,
|
||||
logger: logger,
|
||||
interval: time.Minute,
|
||||
instanceID: newInstanceID(),
|
||||
db: db,
|
||||
redis: redisClient,
|
||||
logger: logger,
|
||||
paymentProbe: paymentProbe,
|
||||
interval: time.Minute,
|
||||
instanceID: newInstanceID(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,6 +265,11 @@ func (j *Job) handlePendingPaymentTimeout(ctx context.Context, now time.Time, cf
|
||||
}
|
||||
count := 0
|
||||
for _, row := range rows {
|
||||
// 租客常在超时的最后几十秒完成付款,渠道结果却在取消之后才到。
|
||||
// 先跟渠道核对一次,已收款的就让它正常入账,这一轮不取消。
|
||||
if j.channelPaidBeforeTimeout(ctx, row) {
|
||||
continue
|
||||
}
|
||||
if err := j.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var order model.RentalOrder
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&order, row.ID).Error; err != nil {
|
||||
@@ -314,6 +326,30 @@ func (j *Job) handlePendingPaymentTimeout(ctx context.Context, now time.Time, cf
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// channelPaidBeforeTimeout 超时取消前跟渠道核对收款,返回 true 表示已收款、本轮跳过取消。
|
||||
// 渠道异常时按未收款继续取消,否则渠道一抖所有超时订单都会堆积。
|
||||
func (j *Job) channelPaidBeforeTimeout(ctx context.Context, order model.RentalOrder) bool {
|
||||
if j.paymentProbe == nil {
|
||||
return false
|
||||
}
|
||||
paid, err := j.paymentProbe.ConfirmChannelPaidBeforeClose(ctx, order.ID)
|
||||
if err != nil {
|
||||
j.logger.Warn("支付超时取消前核对渠道失败",
|
||||
zap.Uint64("order_id", order.ID),
|
||||
zap.String("order_no", order.OrderNo),
|
||||
zap.Error(err),
|
||||
)
|
||||
return false
|
||||
}
|
||||
if paid {
|
||||
j.logger.Warn("支付超时取消前核对到渠道已收款,放弃取消",
|
||||
zap.Uint64("order_id", order.ID),
|
||||
zap.String("order_no", order.OrderNo),
|
||||
)
|
||||
}
|
||||
return paid
|
||||
}
|
||||
|
||||
// closePendingOrderPayments 在系统自动取消订单时同步关闭未完成支付单,保持订单和支付流水状态一致。
|
||||
func closePendingOrderPayments(tx *gorm.DB, orderID uint64, source string) error {
|
||||
raw, err := json.Marshal(map[string]string{
|
||||
|
||||
@@ -33,7 +33,7 @@ func setupOrderTimeoutTestDB(t *testing.T) *gorm.DB {
|
||||
|
||||
func TestHandleEndedOrderRenterRetention(t *testing.T) {
|
||||
db := setupOrderTimeoutTestDB(t)
|
||||
job := New(db, nil, zap.NewNop())
|
||||
job := New(db, nil, zap.NewNop(), nil)
|
||||
now := time.Date(2026, 6, 18, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
oldSettledAt := now.AddDate(0, 0, -6)
|
||||
@@ -151,7 +151,7 @@ func TestHandleEndedOrderRenterRetention(t *testing.T) {
|
||||
|
||||
func TestHandleRenterConfirmTimeoutUsesRefreshedStageTime(t *testing.T) {
|
||||
db := setupOrderTimeoutTestDB(t)
|
||||
job := New(db, nil, zap.NewNop())
|
||||
job := New(db, nil, zap.NewNop(), nil)
|
||||
now := time.Date(2026, 6, 18, 12, 0, 0, 0, time.UTC)
|
||||
oldHandoffAt := now.Add(-2 * time.Hour)
|
||||
refreshedAt := now.Add(-5 * time.Minute)
|
||||
@@ -202,7 +202,7 @@ func TestHandleRenterConfirmTimeoutUsesRefreshedStageTime(t *testing.T) {
|
||||
|
||||
func TestHandleOwnerReturnConfirmTimeoutUsesStageTime(t *testing.T) {
|
||||
db := setupOrderTimeoutTestDB(t)
|
||||
job := New(db, nil, zap.NewNop())
|
||||
job := New(db, nil, zap.NewNop(), nil)
|
||||
now := time.Date(2026, 6, 18, 12, 0, 0, 0, time.UTC)
|
||||
oldUpdatedAt := now.Add(-2 * time.Hour)
|
||||
refreshedAt := now.Add(-5 * time.Minute)
|
||||
|
||||
Reference in New Issue
Block a user