租客在待支付超时或取消的最后几十秒完成付款时,订单先被取消,乐刷的支付 结果几秒后才到,入账被判定订单不可支付而拒收。原先订单推进和支付单更新 在同一个事务里,订单一报错整笔回滚,支付单停在支付中且 paid_at 为空: 钱已在渠道,系统却毫不知情。乐刷重试十次后放弃,之后既补不回入账(订单 不再是待支付),后台人工退款也发不出去(退款要求原支付单是已支付)。 日志保留期内 13 天出了 5 笔,合计 4085.60 元。 - 取消前核对渠道:超时取消、租客取消、客服关单都先查一次渠道,已收款并 已推进订单就放弃取消,让支付正常入账而不是走退款。渠道异常时按未收款 继续,避免渠道一抖订单全卡住 - 回调兜底记账:订单确实已结束时不再回滚,把支付单如实记成已支付并写入 paid_at 与渠道单号,后台人工退款从此可用。退款仍由客服人工发起 - 客服关单:待支付订单按实际已收款减已退款的差额退,补上本次事故中关单 不生成退款的直接原因 - 补齐日志上下文:回调入账失败原先只有 error="order cannot pay",现在带 支付单号、订单号、订单状态和金额;新增 ERROR「渠道已收款但订单无法入账, 需人工退款」作为唯一需人工介入的告警 Co-authored-by: Cursor <cursoragent@cursor.com>
119 lines
4.0 KiB
Go
119 lines
4.0 KiB
Go
package payment
|
|
|
|
import (
|
|
"context"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/modules/distribution"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
func (r *Repository) applyChannelStatus(ctx context.Context, payment *model.PaymentOrder, status string, payTime string, raw map[string]string, source string) error {
|
|
switch status {
|
|
case "paid":
|
|
paidAt := parseChannelTime(payTime)
|
|
if paidAt == nil {
|
|
now := time.Now()
|
|
paidAt = &now
|
|
}
|
|
return r.confirmPaid(ctx, payment, status, *paidAt, raw, source)
|
|
case "closed":
|
|
return r.updateChannelStatus(ctx, payment.ID, "closed", raw, source)
|
|
case "failed":
|
|
return r.updateChannelStatus(ctx, payment.ID, "failed", raw, source)
|
|
default:
|
|
return r.updateChannelStatus(ctx, payment.ID, "paying", raw, source)
|
|
}
|
|
}
|
|
func (r *Repository) updateChannelStatus(ctx context.Context, paymentID uint64, status string, raw map[string]string, source string) error {
|
|
updates := map[string]any{
|
|
"status": status,
|
|
"raw_response": jsonMap(withRawSource(raw, source)),
|
|
}
|
|
if source == channelSourceNotify {
|
|
updates["notified_at"] = time.Now()
|
|
}
|
|
return r.db.WithContext(ctx).Model(&model.PaymentOrder{}).
|
|
Where("id = ? AND status NOT IN ?", paymentID, []string{"paid", "closed", "failed"}).
|
|
Updates(updates).Error
|
|
}
|
|
|
|
// isOrderPaymentTerminalStatus 判断订单支付流水是否已进入终态,终态不再被查询或普通回调刷回支付中。
|
|
func isOrderPaymentTerminalStatus(status string) bool {
|
|
return status == "paid" || status == "closed" || status == "failed"
|
|
}
|
|
func (r *Repository) confirmPaid(ctx context.Context, payment *model.PaymentOrder, status string, paidAt time.Time, raw map[string]string, source string) error {
|
|
var newConvID uint64
|
|
var notifyFn func(uint64)
|
|
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
if payment.OrderID != 0 {
|
|
switch payment.BizType {
|
|
case "mohong_pay":
|
|
if r.mohongRepo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
convID, err := r.mohongRepo.ConfirmPaidFromChannelTx(tx, payment.OrderID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newConvID = convID
|
|
notifyFn = r.mohongRepo.NotifyNewConversation
|
|
default:
|
|
if r.orderRepo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
convID, err := r.orderRepo.ConfirmPaidFromChannelTx(tx, payment.OrderID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newConvID = convID
|
|
notifyFn = r.orderRepo.NotifyNewConversation
|
|
if err := distribution.SnapshotPaidOrderTx(tx, payment.OrderID, payment.AmountCent); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
var latest model.PaymentOrder
|
|
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&latest, payment.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
updates := map[string]any{
|
|
"status": "paid",
|
|
"provider_order_id": firstNonEmpty(raw["provider_order_id"], raw["schc_order_id"], raw["leshua_order_id"], raw["pay_order_no"], raw["trade_no"], latest.ProviderOrderID),
|
|
"raw_response": jsonMap(withRawSource(raw, source)),
|
|
"paid_at": paidAt,
|
|
}
|
|
if source == channelSourceNotify {
|
|
updates["notified_at"] = time.Now()
|
|
}
|
|
return tx.Model(&model.PaymentOrder{}).Where("id = ?", payment.ID).Updates(updates).Error
|
|
})
|
|
if err != nil {
|
|
if settleErr, handled := r.settleUnpayableOrderPayment(ctx, payment, paidAt, raw, source, err); handled {
|
|
return settleErr
|
|
}
|
|
return err
|
|
}
|
|
if newConvID > 0 && notifyFn != nil {
|
|
notifyFn(newConvID)
|
|
}
|
|
if latest, err := r.findPaymentByID(ctx, payment.ID); err == nil {
|
|
if runtimeConfig, err := r.runtimeConfigForPayment(ctx, latest); err == nil {
|
|
r.recordConfigUsage(ctx, runtimeConfig, latest)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (r *Repository) markPaymentFailed(ctx context.Context, paymentID uint64, raw map[string]string, message string) error {
|
|
if raw == nil {
|
|
raw = map[string]string{"error": message}
|
|
}
|
|
return r.db.WithContext(ctx).Model(&model.PaymentOrder{}).Where("id = ?", paymentID).Updates(map[string]any{
|
|
"status": "failed",
|
|
"raw_response": jsonMap(raw),
|
|
}).Error
|
|
}
|