租客在待支付超时或取消的最后几十秒完成付款时,订单先被取消,乐刷的支付 结果几秒后才到,入账被判定订单不可支付而拒收。原先订单推进和支付单更新 在同一个事务里,订单一报错整笔回滚,支付单停在支付中且 paid_at 为空: 钱已在渠道,系统却毫不知情。乐刷重试十次后放弃,之后既补不回入账(订单 不再是待支付),后台人工退款也发不出去(退款要求原支付单是已支付)。 日志保留期内 13 天出了 5 笔,合计 4085.60 元。 - 取消前核对渠道:超时取消、租客取消、客服关单都先查一次渠道,已收款并 已推进订单就放弃取消,让支付正常入账而不是走退款。渠道异常时按未收款 继续,避免渠道一抖订单全卡住 - 回调兜底记账:订单确实已结束时不再回滚,把支付单如实记成已支付并写入 paid_at 与渠道单号,后台人工退款从此可用。退款仍由客服人工发起 - 客服关单:待支付订单按实际已收款减已退款的差额退,补上本次事故中关单 不生成退款的直接原因 - 补齐日志上下文:回调入账失败原先只有 error="order cannot pay",现在带 支付单号、订单号、订单状态和金额;新增 ERROR「渠道已收款但订单无法入账, 需人工退款」作为唯一需人工介入的告警 Co-authored-by: Cursor <cursoragent@cursor.com>
84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package order
|
|
|
|
import (
|
|
"context"
|
|
|
|
"hfb_sys/backend/internal/logging"
|
|
"hfb_sys/backend/internal/model"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (r *Repository) prepareRefund(order *model.RentalOrder, amountCent int64, bizType string, remark string) (*refundAction, error) {
|
|
if amountCent <= 0 {
|
|
return nil, nil
|
|
}
|
|
if r.refundStarter == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
order.RefundStatus = refundStatusPending
|
|
order.RefundAmountCent = amountCent
|
|
order.RefundedAt = nil
|
|
return &refundAction{
|
|
OrderID: order.ID,
|
|
RefundAmountCent: amountCent,
|
|
BizType: bizType,
|
|
Remark: remark,
|
|
}, nil
|
|
}
|
|
|
|
// outstandingPaidOrderPaymentCentTx 算出订单实际收到、尚未退回的下单款项。
|
|
// 口径是已支付的下单流水合计减去已退/在退的退款流水合计,重复关单不会重复退。
|
|
func outstandingPaidOrderPaymentCentTx(tx *gorm.DB, orderID uint64) (int64, error) {
|
|
var paidCent int64
|
|
if err := tx.Model(&model.PaymentOrder{}).
|
|
Where("order_id = ? AND biz_type = ? AND status = ?", orderID, "order_pay", "paid").
|
|
Select("COALESCE(SUM(amount_cent), 0)").Scan(&paidCent).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
if paidCent <= 0 {
|
|
return 0, nil
|
|
}
|
|
var refundedCent int64
|
|
if err := tx.Model(&model.PaymentOrder{}).
|
|
Where("order_id = ? AND biz_type <> ? AND status IN ?", orderID, "order_pay", []string{"refunding", "refunded"}).
|
|
Select("COALESCE(SUM(amount_cent), 0)").Scan(&refundedCent).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
if refundedCent >= paidCent {
|
|
return 0, nil
|
|
}
|
|
return paidCent - refundedCent, nil
|
|
}
|
|
|
|
// pendingPaymentOrderPaidAtChannelForAdmin 在客服关单前核对待支付订单的渠道收款情况。
|
|
func (r *Repository) pendingPaymentOrderPaidAtChannelForAdmin(ctx context.Context, orderID uint64) bool {
|
|
if r.paymentProbe == nil {
|
|
return false
|
|
}
|
|
var status string
|
|
err := r.db.WithContext(ctx).Model(&model.RentalOrder{}).
|
|
Where("id = ?", orderID).
|
|
Pluck("status", &status).Error
|
|
if err != nil || status != orderStatusPendingPayment {
|
|
return false
|
|
}
|
|
return r.channelPaidBeforeClose(ctx, orderID)
|
|
}
|
|
|
|
func (r *Repository) startRefundBestEffort(ctx context.Context, action *refundAction) {
|
|
if action == nil || r.refundStarter == nil {
|
|
return
|
|
}
|
|
if _, err := r.refundStarter.StartRefund(ctx, action.OrderID, action.RefundAmountCent, action.BizType, action.Remark); err != nil {
|
|
logging.FromContext(ctx).Error("订单退款提交失败",
|
|
zap.String("module", "order"),
|
|
zap.Uint64("order_id", action.OrderID),
|
|
zap.String("biz_type", action.BizType),
|
|
zap.Int64("amount_cent", action.RefundAmountCent),
|
|
zap.Error(err),
|
|
)
|
|
}
|
|
}
|