125 lines
3.5 KiB
Go
125 lines
3.5 KiB
Go
package payment
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
|
ErrPaymentUnavailable = errors.New("payment unavailable")
|
|
ErrPaymentCannotStart = errors.New("payment cannot start")
|
|
ErrPaymentVerifyFailed = errors.New("payment verify failed")
|
|
ErrPaymentNotFound = errors.New("payment not found")
|
|
ErrRefundCannotStart = errors.New("refund cannot start")
|
|
ErrWalletRechargeDisabled = errors.New("wallet recharge disabled")
|
|
)
|
|
|
|
const MinWalletRechargeAmount = 0.01
|
|
|
|
type Service struct {
|
|
repo *Repository
|
|
walletRechargeEnabled bool
|
|
}
|
|
|
|
func NewService(repo *Repository, appEnv ...string) *Service {
|
|
env := "production"
|
|
if len(appEnv) > 0 {
|
|
env = strings.ToLower(strings.TrimSpace(appEnv[0]))
|
|
}
|
|
return &Service{
|
|
repo: repo,
|
|
walletRechargeEnabled: env != "production",
|
|
}
|
|
}
|
|
|
|
func (s *Service) Start(userID uint64, orderID uint64, req StartPaymentRequest, clientIP string) (*PaymentDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if userID == 0 || orderID == 0 {
|
|
return nil, ErrPaymentCannotStart
|
|
}
|
|
return s.repo.Start(userID, orderID, req, clientIP)
|
|
}
|
|
|
|
func (s *Service) Query(userID uint64, orderID uint64) (*PaymentDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if userID == 0 || orderID == 0 {
|
|
return nil, ErrPaymentNotFound
|
|
}
|
|
return s.repo.Query(userID, orderID)
|
|
}
|
|
|
|
func (s *Service) StartWalletRecharge(userID uint64, req WalletRechargePaymentRequest, clientIP string) (*PaymentDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if !s.walletRechargeEnabled {
|
|
return nil, ErrWalletRechargeDisabled
|
|
}
|
|
return s.repo.StartWalletRecharge(userID, req, clientIP)
|
|
}
|
|
|
|
func (s *Service) QueryWalletRecharge(userID uint64, paymentID uint64) (*PaymentDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if userID == 0 || paymentID == 0 {
|
|
return nil, ErrPaymentNotFound
|
|
}
|
|
return s.repo.QueryWalletRecharge(userID, paymentID)
|
|
}
|
|
|
|
func (s *Service) HandleLeshuaNotify(params map[string]string, rawPayload string, contentType string) (*NotifyResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.HandleLeshuaNotify(params, rawPayload, contentType)
|
|
}
|
|
|
|
func (s *Service) HandleNotify(provider string, params map[string]string, rawPayload string, contentType string, authorization string) (*NotifyResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
return s.repo.HandleNotify(provider, params, rawPayload, contentType, authorization)
|
|
}
|
|
|
|
func (s *Service) StartRefund(orderID uint64, refundAmountCent int64, bizType string, remark string) (*RefundDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 || refundAmountCent <= 0 {
|
|
return nil, ErrRefundCannotStart
|
|
}
|
|
return s.repo.StartRefund(orderID, refundAmountCent, bizType, remark)
|
|
}
|
|
|
|
func (s *Service) QueryRefundStatus(orderID uint64) (*RefundDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if orderID == 0 {
|
|
return nil, ErrPaymentNotFound
|
|
}
|
|
return s.repo.QueryRefundStatus(orderID)
|
|
}
|
|
|
|
func (s *Service) AdminList(query AdminPaymentQuery) (*PaginatedResult, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
if query.Page < 1 {
|
|
query.Page = 1
|
|
}
|
|
if query.PageSize < 1 {
|
|
query.PageSize = 20
|
|
}
|
|
if query.PageSize > 100 {
|
|
query.PageSize = 100
|
|
}
|
|
return s.repo.AdminList(query)
|
|
}
|