confirmPaid 原为「先 ConfirmPaidFromChannel 独立事务推进订单, 再 Updates 标 paid」两步非原子, 崩溃在中间会出现订单已 pending_handoff 但 payment 仍 paying 的不一致窗口, 恢复依赖渠道回调重发而非事务闭环。 改造: - 新增 ConfirmPaidFromChannelTx(tx, orderID) 让 order 模块共享 payment 模块的外部事务, 订单推进与 payment 标 paid 落进同一事务, 崩溃一致性窗口消除 - NotifyNewConversation 提到事务提交后触发, 避免事务回滚后误发会话通知 - payment_orders 加行锁后 Updates, 防并发回调覆盖写 - 删除未使用的 providerBizNo 死参数 锁顺序全局一致 (order→listing→account→payment), 与 payment_start/refund/Cancel 路径无反向加锁, 无死锁风险。 新增集成测试: - 正向: 验证 confirmPaid 后 order/listing/account/payment 全部正确推进 - 回滚: payment 更新失败时 order 不残留 pending_handoff, 验证事务原子性 go build/vet/test 通过。
85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
package payment
|
|
|
|
import (
|
|
"context"
|
|
"hfb_sys/backend/internal/model"
|
|
"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 = ?", paymentID).Updates(updates).Error
|
|
}
|
|
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
|
|
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
if payment.OrderID != 0 {
|
|
if r.orderRepo == nil {
|
|
return ErrDependencyUnavailable
|
|
}
|
|
convID, err := r.orderRepo.ConfirmPaidFromChannelTx(tx, payment.OrderID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newConvID = convID
|
|
}
|
|
|
|
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["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 {
|
|
return err
|
|
}
|
|
if newConvID > 0 && r.orderRepo != nil {
|
|
r.orderRepo.NotifyNewConversation(newConvID)
|
|
}
|
|
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
|
|
}
|