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 通过。
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package order
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// RefundStarter 由支付模块适配实现,避免 order 直接依赖 payment。
|
|
type RefundStarter interface {
|
|
StartRefund(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)
|
|
}
|
|
|
|
type RefundStarterFunc func(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (status string, err error)
|
|
|
|
func (fn RefundStarterFunc) StartRefund(ctx context.Context, orderID uint64, refundAmountCent int64, bizType string, remark string) (string, error) {
|
|
return fn(ctx, orderID, refundAmountCent, bizType, remark)
|
|
}
|
|
|
|
type OrderChatNotifier interface {
|
|
NotifyNewConversation(conversationID uint64)
|
|
}
|
|
|
|
type Dependencies struct {
|
|
ChatNotifier OrderChatNotifier
|
|
RefundStarter RefundStarter
|
|
}
|
|
|
|
type refundAction struct {
|
|
OrderID uint64
|
|
RefundAmountCent int64
|
|
BizType string
|
|
Remark string
|
|
}
|
|
|
|
type Repository struct {
|
|
db *gorm.DB
|
|
chatNotifier OrderChatNotifier
|
|
refundStarter RefundStarter
|
|
}
|
|
|
|
const defaultPendingPaymentTimeoutMinutes = 15
|
|
|
|
func NewRepository(db *gorm.DB, deps ...Dependencies) *Repository {
|
|
repo := &Repository{db: db}
|
|
if len(deps) > 0 {
|
|
repo.chatNotifier = deps[0].ChatNotifier
|
|
repo.refundStarter = deps[0].RefundStarter
|
|
}
|
|
return repo
|
|
}
|
|
|
|
func (r *Repository) NotifyNewConversation(conversationID uint64) {
|
|
if conversationID > 0 && r.chatNotifier != nil {
|
|
r.chatNotifier.NotifyNewConversation(conversationID)
|
|
}
|
|
}
|