mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-04-18 16:07:28 +00:00
Implement comprehensive topup billing system with user history viewing and admin management capabilities.
## Features Added
### Frontend
- Add topup history modal with paginated billing records
- Display order details: trade number, payment method, amount, money, status, create time
- Implement empty state with proper illustrations
- Add payment method column with localized display (Stripe, Alipay, WeChat)
- Add admin manual completion feature for pending orders
- Add Coins icon for recharge amount display
- Integrate "Bills" button in RechargeCard header
- Optimize code quality by using shared utility functions (isAdmin)
- Extract constants for status and payment method mappings
- Use React.useMemo for performance optimization
### Backend
- Create GET `/api/user/topup/self` endpoint for user topup history with pagination
- Create POST `/api/user/topup/complete` endpoint for admin manual order completion
- Add `payment_method` field to TopUp model for tracking payment types
- Implement `GetUserTopUps` method with proper pagination and ordering
- Implement `ManualCompleteTopUp` with transaction safety and row-level locking
- Add application-level mutex locks to prevent concurrent order processing
- Record payment method in Epay and Stripe payment flows
- Ensure idempotency and data consistency with proper error handling
### Internationalization
- Add i18n keys for Chinese (zh), English (en), and French (fr)
- Support for billing-related UI text and status messages
## Technical Improvements
- Use database transactions with FOR UPDATE row-level locking
- Implement sync.Map-based mutex for order-level concurrency control
- Proper error handling and user-friendly toast notifications
- Follow existing codebase patterns for empty states and modals
- Maintain code quality with extracted render functions and constants
## Files Changed
- Backend: controller/topup.go, controller/topup_stripe.go, model/topup.go, router/api-router.go
- Frontend: web/src/components/topup/modals/TopupHistoryModal.jsx (new), web/src/components/topup/RechargeCard.jsx, web/src/components/topup/index.jsx
- i18n: web/src/i18n/locales/{zh,en,fr}.json
210 lines
5.3 KiB
Go
210 lines
5.3 KiB
Go
package model
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"one-api/common"
|
||
"one-api/logger"
|
||
|
||
"github.com/shopspring/decimal"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type TopUp struct {
|
||
Id int `json:"id"`
|
||
UserId int `json:"user_id" gorm:"index"`
|
||
Amount int64 `json:"amount"`
|
||
Money float64 `json:"money"`
|
||
TradeNo string `json:"trade_no" gorm:"unique;type:varchar(255);index"`
|
||
PaymentMethod string `json:"payment_method" gorm:"type:varchar(50)"`
|
||
CreateTime int64 `json:"create_time"`
|
||
CompleteTime int64 `json:"complete_time"`
|
||
Status string `json:"status"`
|
||
}
|
||
|
||
func (topUp *TopUp) Insert() error {
|
||
var err error
|
||
err = DB.Create(topUp).Error
|
||
return err
|
||
}
|
||
|
||
func (topUp *TopUp) Update() error {
|
||
var err error
|
||
err = DB.Save(topUp).Error
|
||
return err
|
||
}
|
||
|
||
func GetTopUpById(id int) *TopUp {
|
||
var topUp *TopUp
|
||
var err error
|
||
err = DB.Where("id = ?", id).First(&topUp).Error
|
||
if err != nil {
|
||
return nil
|
||
}
|
||
return topUp
|
||
}
|
||
|
||
func GetTopUpByTradeNo(tradeNo string) *TopUp {
|
||
var topUp *TopUp
|
||
var err error
|
||
err = DB.Where("trade_no = ?", tradeNo).First(&topUp).Error
|
||
if err != nil {
|
||
return nil
|
||
}
|
||
return topUp
|
||
}
|
||
|
||
func Recharge(referenceId string, customerId string) (err error) {
|
||
if referenceId == "" {
|
||
return errors.New("未提供支付单号")
|
||
}
|
||
|
||
var quota float64
|
||
topUp := &TopUp{}
|
||
|
||
refCol := "`trade_no`"
|
||
if common.UsingPostgreSQL {
|
||
refCol = `"trade_no"`
|
||
}
|
||
|
||
err = DB.Transaction(func(tx *gorm.DB) error {
|
||
err := tx.Set("gorm:query_option", "FOR UPDATE").Where(refCol+" = ?", referenceId).First(topUp).Error
|
||
if err != nil {
|
||
return errors.New("充值订单不存在")
|
||
}
|
||
|
||
if topUp.Status != common.TopUpStatusPending {
|
||
return errors.New("充值订单状态错误")
|
||
}
|
||
|
||
topUp.CompleteTime = common.GetTimestamp()
|
||
topUp.Status = common.TopUpStatusSuccess
|
||
err = tx.Save(topUp).Error
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
quota = topUp.Money * common.QuotaPerUnit
|
||
err = tx.Model(&User{}).Where("id = ?", topUp.UserId).Updates(map[string]interface{}{"stripe_customer": customerId, "quota": gorm.Expr("quota + ?", quota)}).Error
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
})
|
||
|
||
if err != nil {
|
||
return errors.New("充值失败," + err.Error())
|
||
}
|
||
|
||
RecordLog(topUp.UserId, LogTypeTopup, fmt.Sprintf("使用在线充值成功,充值金额: %v,支付金额:%d", logger.FormatQuota(int(quota)), topUp.Amount))
|
||
|
||
return nil
|
||
}
|
||
|
||
func GetUserTopUps(userId int, pageInfo *common.PageInfo) (topups []*TopUp, total int64, err error) {
|
||
// Start transaction
|
||
tx := DB.Begin()
|
||
if tx.Error != nil {
|
||
return nil, 0, tx.Error
|
||
}
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
tx.Rollback()
|
||
}
|
||
}()
|
||
|
||
// Get total count within transaction
|
||
err = tx.Model(&TopUp{}).Where("user_id = ?", userId).Count(&total).Error
|
||
if err != nil {
|
||
tx.Rollback()
|
||
return nil, 0, err
|
||
}
|
||
|
||
// Get paginated topups within same transaction
|
||
err = tx.Where("user_id = ?", userId).Order("id desc").Limit(pageInfo.GetPageSize()).Offset(pageInfo.GetStartIdx()).Find(&topups).Error
|
||
if err != nil {
|
||
tx.Rollback()
|
||
return nil, 0, err
|
||
}
|
||
|
||
// Commit transaction
|
||
if err = tx.Commit().Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
return topups, total, nil
|
||
}
|
||
|
||
// ManualCompleteTopUp 管理员手动完成订单并给用户充值
|
||
func ManualCompleteTopUp(tradeNo string) error {
|
||
if tradeNo == "" {
|
||
return errors.New("未提供订单号")
|
||
}
|
||
|
||
refCol := "`trade_no`"
|
||
if common.UsingPostgreSQL {
|
||
refCol = `"trade_no"`
|
||
}
|
||
|
||
var userId int
|
||
var quotaToAdd int
|
||
var payMoney float64
|
||
|
||
err := DB.Transaction(func(tx *gorm.DB) error {
|
||
topUp := &TopUp{}
|
||
// 行级锁,避免并发补单
|
||
if err := tx.Set("gorm:query_option", "FOR UPDATE").Where(refCol+" = ?", tradeNo).First(topUp).Error; err != nil {
|
||
return errors.New("充值订单不存在")
|
||
}
|
||
|
||
// 幂等处理:已成功直接返回
|
||
if topUp.Status == common.TopUpStatusSuccess {
|
||
return nil
|
||
}
|
||
|
||
if topUp.Status != common.TopUpStatusPending {
|
||
return errors.New("订单状态不是待支付,无法补单")
|
||
}
|
||
|
||
// 计算应充值额度:
|
||
// - Stripe 订单:Money 代表经分组倍率换算后的美元数量,直接 * QuotaPerUnit
|
||
// - 其他订单(如易支付):Amount 为美元数量,* QuotaPerUnit
|
||
if topUp.PaymentMethod == "stripe" {
|
||
dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit)
|
||
quotaToAdd = int(decimal.NewFromFloat(topUp.Money).Mul(dQuotaPerUnit).IntPart())
|
||
} else {
|
||
dAmount := decimal.NewFromInt(topUp.Amount)
|
||
dQuotaPerUnit := decimal.NewFromFloat(common.QuotaPerUnit)
|
||
quotaToAdd = int(dAmount.Mul(dQuotaPerUnit).IntPart())
|
||
}
|
||
if quotaToAdd <= 0 {
|
||
return errors.New("无效的充值额度")
|
||
}
|
||
|
||
// 标记完成
|
||
topUp.CompleteTime = common.GetTimestamp()
|
||
topUp.Status = common.TopUpStatusSuccess
|
||
if err := tx.Save(topUp).Error; err != nil {
|
||
return err
|
||
}
|
||
|
||
// 增加用户额度(立即写库,保持一致性)
|
||
if err := tx.Model(&User{}).Where("id = ?", topUp.UserId).Update("quota", gorm.Expr("quota + ?", quotaToAdd)).Error; err != nil {
|
||
return err
|
||
}
|
||
|
||
userId = topUp.UserId
|
||
payMoney = topUp.Money
|
||
return nil
|
||
})
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 事务外记录日志,避免阻塞
|
||
RecordLog(userId, LogTypeTopup, fmt.Sprintf("管理员补单成功,充值金额: %v,支付金额:%f", logger.FormatQuota(quotaToAdd), payMoney))
|
||
return nil
|
||
}
|