修复支付取消与成功统计

This commit is contained in:
yml
2026-06-14 23:58:39 +08:00
parent 3ff8c1de88
commit a275a006eb
11 changed files with 412 additions and 7 deletions
@@ -2,12 +2,14 @@ package order
import (
"context"
"encoding/json"
"time"
"hfb_sys/backend/internal/model"
"hfb_sys/backend/internal/modules/chat"
"hfb_sys/backend/internal/modules/notification"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
@@ -264,6 +266,9 @@ func (r *Repository) Cancel(ctx context.Context, userID uint64, orderID uint64)
return err
}
releaseAssetsForRental(listing, account)
if err := closePendingOrderPayments(tx, order.ID, "order_cancel"); err != nil {
return err
}
if err := tx.Save(&order).Error; err != nil {
return err
}
@@ -279,6 +284,23 @@ func (r *Repository) Cancel(ctx context.Context, userID uint64, orderID uint64)
return nil
}
// closePendingOrderPayments 在订单取消时关闭仍未完成的下单支付流水,避免后台一直显示“支付中”。
func closePendingOrderPayments(tx *gorm.DB, orderID uint64, source string) error {
raw, err := json.Marshal(map[string]string{
"source": source,
"reason": "订单已取消,关闭未完成支付单",
})
if err != nil {
return err
}
return tx.Model(&model.PaymentOrder{}).
Where("order_id = ? AND biz_type = ? AND status IN ?", orderID, "order_pay", []string{"created", "paying"}).
Updates(map[string]any{
"status": "closed",
"raw_response": datatypes.JSON(raw),
}).Error
}
func (r *Repository) ConfirmReceive(ctx context.Context, userID uint64, orderID uint64) error {
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var order model.RentalOrder