新增订单待支付与支付确认后端流程
This commit is contained in:
@@ -9,6 +9,10 @@ type AccountDTO struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type RechargeRequest struct {
|
||||
Amount float64 `json:"amount" binding:"required"`
|
||||
}
|
||||
|
||||
type LedgerDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
LedgerNo string `json:"ledger_no"`
|
||||
|
||||
@@ -46,6 +46,25 @@ func (h *Handler) Ledger(c *gin.Context) {
|
||||
response.OK(c, gin.H{"items": items})
|
||||
}
|
||||
|
||||
func (h *Handler) Recharge(c *gin.Context) {
|
||||
userID, ok := currentUserID(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
var req RechargeRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "充值金额不正确")
|
||||
return
|
||||
}
|
||||
account, err := h.service.Recharge(userID, req)
|
||||
if err != nil {
|
||||
writeWalletError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, account)
|
||||
}
|
||||
|
||||
func (h *Handler) AdminLedger(c *gin.Context) {
|
||||
query, ok := parseAdminLedgerQuery(c)
|
||||
if !ok {
|
||||
@@ -102,6 +121,10 @@ func writeWalletError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, ErrDependencyUnavailable):
|
||||
response.ServiceUnavailable(c, "数据库未连接")
|
||||
case errors.Is(err, ErrInvalidAmount):
|
||||
response.BadRequest(c, "充值金额不正确")
|
||||
case errors.Is(err, ErrInsufficientBalance):
|
||||
response.Error(c, 409, "insufficient_balance", "钱包余额不足")
|
||||
default:
|
||||
response.ServiceUnavailable(c, "钱包服务暂时不可用")
|
||||
}
|
||||
|
||||
@@ -57,6 +57,24 @@ func (r *Repository) Ledger(userID uint64) ([]LedgerDTO, error) {
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Recharge(userID uint64, amount float64) (*AccountDTO, error) {
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
return AppendEntries(tx, Entry{
|
||||
UserID: userID,
|
||||
Direction: "in",
|
||||
Amount: amount,
|
||||
BalanceType: "available",
|
||||
BizType: "dev_recharge",
|
||||
BizNo: "DEV",
|
||||
Remark: "开发环境充值",
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Account(userID)
|
||||
}
|
||||
|
||||
func (r *Repository) AdminLedger(query AdminLedgerQuery) ([]AdminLedgerDTO, error) {
|
||||
limit := query.Limit
|
||||
if limit <= 0 || limit > 500 {
|
||||
@@ -146,6 +164,9 @@ func applyEntry(account *model.WalletAccount, entry Entry) (float64, error) {
|
||||
if entry.Direction == "in" {
|
||||
account.AvailableBalance += entry.Amount
|
||||
} else {
|
||||
if account.AvailableBalance < entry.Amount {
|
||||
return 0, ErrInsufficientBalance
|
||||
}
|
||||
account.AvailableBalance -= entry.Amount
|
||||
}
|
||||
return account.AvailableBalance, nil
|
||||
@@ -153,6 +174,9 @@ func applyEntry(account *model.WalletAccount, entry Entry) (float64, error) {
|
||||
if entry.Direction == "in" {
|
||||
account.FrozenBalance += entry.Amount
|
||||
} else {
|
||||
if account.FrozenBalance < entry.Amount {
|
||||
return 0, ErrInsufficientBalance
|
||||
}
|
||||
account.FrozenBalance -= entry.Amount
|
||||
}
|
||||
return account.FrozenBalance, nil
|
||||
|
||||
@@ -2,7 +2,11 @@ package wallet
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
var (
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
ErrInvalidAmount = errors.New("invalid amount")
|
||||
ErrInsufficientBalance = errors.New("insufficient balance")
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
@@ -26,6 +30,16 @@ func (s *Service) Ledger(userID uint64) ([]LedgerDTO, error) {
|
||||
return s.repo.Ledger(userID)
|
||||
}
|
||||
|
||||
func (s *Service) Recharge(userID uint64, req RechargeRequest) (*AccountDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if req.Amount <= 0 {
|
||||
return nil, ErrInvalidAmount
|
||||
}
|
||||
return s.repo.Recharge(userID, req.Amount)
|
||||
}
|
||||
|
||||
func (s *Service) AdminLedger(query AdminLedgerQuery) ([]AdminLedgerDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
|
||||
Reference in New Issue
Block a user