支付确认强一致化: 订单推进与标 paid 合并为单事务
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 通过。
This commit is contained in:
@@ -136,61 +136,12 @@ func (r *Repository) Pay(ctx context.Context, userID uint64, orderID uint64) err
|
||||
func (r *Repository) ConfirmPaidFromChannel(ctx context.Context, orderID uint64, providerBizNo string) error {
|
||||
var newConvID uint64
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
assets, err := r.lockOrderAssets(tx, orderID)
|
||||
convID, err := r.ConfirmPaidFromChannelTx(tx, orderID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
order := assets.Order
|
||||
listing := assets.Listing
|
||||
account := assets.Account
|
||||
if order.Status == orderStatusPendingHandoff || order.Status == orderStatusRenting {
|
||||
return nil
|
||||
}
|
||||
if order.Status != orderStatusPendingPayment {
|
||||
return ErrOrderCannotPay
|
||||
}
|
||||
|
||||
if listing.Status != listingStatusPublished || listing.ReviewStatus != listingReviewStatusApproved || !listing.InTransaction {
|
||||
return ErrListingUnavailable
|
||||
}
|
||||
|
||||
// 租客已通过外部渠道付款,这里不写租客钱包流水。
|
||||
orderID := order.ID
|
||||
order.Status = orderStatusPendingHandoff
|
||||
order.HandoffStatus = handoffStatusPendingOwner
|
||||
markAssetsRented(listing, account)
|
||||
conv, err := chat.EnsureOrderConversation(tx, *order)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newConvID = conv.ID
|
||||
if err := notification.Append(tx,
|
||||
notification.Entry{
|
||||
UserID: order.OwnerID,
|
||||
Type: "order",
|
||||
Title: "收到新的租号订单",
|
||||
Content: "租客已完成支付,请尽快提交交接说明。",
|
||||
BizType: "order",
|
||||
BizID: &orderID,
|
||||
},
|
||||
notification.Entry{
|
||||
UserID: order.RenterID,
|
||||
Type: "order",
|
||||
Title: "订单支付成功",
|
||||
Content: "支付已完成,等待号主提交交接说明。",
|
||||
BizType: "order",
|
||||
BizID: &orderID,
|
||||
},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(order).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(listing).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Save(account).Error
|
||||
newConvID = convID
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -201,6 +152,68 @@ func (r *Repository) ConfirmPaidFromChannel(ctx context.Context, orderID uint64,
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfirmPaidFromChannelTx 在外部事务内推进支付成功后的订单状态。
|
||||
// 调用方负责提交事务,并在提交成功后按返回的会话 ID 触发异步通知。
|
||||
func (r *Repository) ConfirmPaidFromChannelTx(tx *gorm.DB, orderID uint64) (uint64, error) {
|
||||
assets, err := r.lockOrderAssets(tx, orderID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
order := assets.Order
|
||||
listing := assets.Listing
|
||||
account := assets.Account
|
||||
if order.Status == orderStatusPendingHandoff || order.Status == orderStatusRenting {
|
||||
return 0, nil
|
||||
}
|
||||
if order.Status != orderStatusPendingPayment {
|
||||
return 0, ErrOrderCannotPay
|
||||
}
|
||||
|
||||
if listing.Status != listingStatusPublished || listing.ReviewStatus != listingReviewStatusApproved || !listing.InTransaction {
|
||||
return 0, ErrListingUnavailable
|
||||
}
|
||||
|
||||
// 租客已通过外部渠道付款,这里不写租客钱包流水。
|
||||
orderID = order.ID
|
||||
order.Status = orderStatusPendingHandoff
|
||||
order.HandoffStatus = handoffStatusPendingOwner
|
||||
markAssetsRented(listing, account)
|
||||
conv, err := chat.EnsureOrderConversation(tx, *order)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := notification.Append(tx,
|
||||
notification.Entry{
|
||||
UserID: order.OwnerID,
|
||||
Type: "order",
|
||||
Title: "收到新的租号订单",
|
||||
Content: "租客已完成支付,请尽快提交交接说明。",
|
||||
BizType: "order",
|
||||
BizID: &orderID,
|
||||
},
|
||||
notification.Entry{
|
||||
UserID: order.RenterID,
|
||||
Type: "order",
|
||||
Title: "订单支付成功",
|
||||
Content: "支付已完成,等待号主提交交接说明。",
|
||||
BizType: "order",
|
||||
BizID: &orderID,
|
||||
},
|
||||
); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := tx.Save(order).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := tx.Save(listing).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := tx.Save(account).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return conv.ID, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Cancel(ctx context.Context, userID uint64, orderID uint64) error {
|
||||
var refund *refundAction
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
@@ -49,3 +49,9 @@ func NewRepository(db *gorm.DB, deps ...Dependencies) *Repository {
|
||||
}
|
||||
return repo
|
||||
}
|
||||
|
||||
func (r *Repository) NotifyNewConversation(conversationID uint64) {
|
||||
if conversationID > 0 && r.chatNotifier != nil {
|
||||
r.chatNotifier.NotifyNewConversation(conversationID)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user