You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.2 KiB
Go
131 lines
3.2 KiB
Go
package payment
|
|
|
|
import (
|
|
"cls/internal/domain/payment"
|
|
"cls/pkg/logger"
|
|
"cls/pkg/util/page"
|
|
"cls/pkg/web"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidAmount = errors.New("价格不能小于0")
|
|
ErrInvalidType = errors.New("无效的支付类型")
|
|
ErrOrderExists = errors.New("订单已存在")
|
|
ErrOrderNotFound = errors.New("订单不存在")
|
|
ErrOrderExpired = errors.New("订单已过期")
|
|
ErrOrderPaid = errors.New("订单已支付")
|
|
ErrOrderCanceled = errors.New("订单已取消")
|
|
)
|
|
|
|
// PaymentService 支付服务
|
|
type PaymentService struct {
|
|
repo payment.PaymentRepository
|
|
log logger.Logger
|
|
}
|
|
|
|
// NewService 创建支付服务
|
|
func NewService(repo payment.PaymentRepository, log logger.New) *PaymentService {
|
|
return &PaymentService{
|
|
repo: repo,
|
|
log: log("cls:service:payment"),
|
|
}
|
|
}
|
|
|
|
// CreateOrder 创建支付订单
|
|
func (s *PaymentService) CreateOrder(userID, targetID uint64, paymentType payment.PaymentType, amount int64, description string) (*payment.Payment, error) {
|
|
if amount <= 0 {
|
|
return nil, ErrInvalidAmount
|
|
}
|
|
|
|
// 生成订单号
|
|
orderNo := fmt.Sprintf("%d%d%d", time.Now().UnixNano(), userID, targetID)
|
|
|
|
// 检查订单是否已存在
|
|
_, err := s.repo.GetByOrderNo(orderNo)
|
|
if err == nil {
|
|
return nil, ErrOrderExists
|
|
}
|
|
|
|
// 创建支付订单
|
|
payment := &payment.Payment{
|
|
OrderNo: orderNo,
|
|
UserID: userID,
|
|
TargetID: targetID,
|
|
Type: paymentType,
|
|
Amount: amount,
|
|
Status: payment.PaymentStatusPending,
|
|
Description: description,
|
|
}
|
|
|
|
// 保存订单
|
|
if err := s.repo.Create(payment); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return payment, nil
|
|
}
|
|
|
|
// GetOrder 获取支付订单
|
|
func (s *PaymentService) GetOrder(orderNo string) (*payment.Payment, error) {
|
|
return s.repo.GetByOrderNo(orderNo)
|
|
}
|
|
|
|
// GetUserOrders 获取用户支付订单列表
|
|
func (s *PaymentService) GetUserOrders(userID uint64, page *page.Page, params map[string]string) error {
|
|
conds := web.ParseFilters(params)
|
|
return s.repo.ListByUserID(userID, page, conds)
|
|
}
|
|
|
|
// GetOrderList 获取支付订单列表
|
|
func (s *PaymentService) GetOrderList(page *page.Page, params map[string]string) error {
|
|
conds := web.ParseFilters(params)
|
|
return s.repo.FindAll(page, conds)
|
|
}
|
|
|
|
// UpdateOrderStatus 更新订单状态
|
|
func (s *PaymentService) UpdateOrderStatus(orderNo string, status payment.PaymentStatus, transactionID string, notifyData string) error {
|
|
order, err := s.repo.GetByOrderNo(orderNo)
|
|
if err != nil {
|
|
return ErrOrderNotFound
|
|
}
|
|
|
|
// 检查订单状态
|
|
if order.Status != payment.PaymentStatusPending {
|
|
return ErrOrderPaid
|
|
}
|
|
|
|
// 更新订单状态
|
|
order.Status = status
|
|
order.TransactionID = transactionID
|
|
order.NotifyData = notifyData
|
|
|
|
return s.repo.Update(order)
|
|
}
|
|
|
|
// CancelOrder 取消支付订单
|
|
func (s *PaymentService) CancelOrder(orderNo string) error {
|
|
order, err := s.repo.GetByOrderNo(orderNo)
|
|
if err != nil {
|
|
return ErrOrderNotFound
|
|
}
|
|
|
|
// 检查订单状态
|
|
if order.Status != payment.PaymentStatusFailed {
|
|
return ErrOrderPaid
|
|
}
|
|
|
|
// 更新订单状态为已取消
|
|
order.Status = payment.PaymentStatusRefunded
|
|
return s.repo.Update(order)
|
|
}
|
|
|
|
// DeleteOrder 删除支付订单
|
|
func (s *PaymentService) DeleteOrder(id uint64) error {
|
|
return s.repo.Delete(id)
|
|
}
|
|
|
|
// CreatePayment 创建支付订单
|