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.
74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
package payment
|
|
|
|
import "time"
|
|
|
|
// PaymentType 支付类型
|
|
type PaymentType int
|
|
|
|
const (
|
|
PaymentTypeWechat PaymentType = iota + 1 // 微信支付
|
|
PaymentTypeAlipay // 支付宝支付
|
|
)
|
|
|
|
// PaymentStatus 支付状态
|
|
type PaymentStatus int
|
|
|
|
const (
|
|
PaymentStatusPending PaymentStatus = iota + 1 // 待支付
|
|
PaymentStatusSuccess // 支付成功
|
|
PaymentStatusFailed // 支付失败
|
|
PaymentStatusRefunded // 已退款
|
|
)
|
|
|
|
// Payment 支付订单实体
|
|
type Payment struct {
|
|
ID uint64 `gorm:"primarykey" json:"id"` // 支付ID
|
|
OrderNo string `gorm:"uniqueIndex" json:"orderNo"` // 订单编号
|
|
TransactionID string `json:"transactionId"` // 第三方交易号
|
|
UserID uint64 `json:"userId"` // 用户ID
|
|
TargetID uint64 `json:"targetId"` // 商品ID
|
|
Type PaymentType `json:"type"` // 支付类型
|
|
Amount int64 `json:"amount"` // 支付金额
|
|
Status PaymentStatus `json:"status"` // 支付状态
|
|
Description string `json:"description"` // 支付描述
|
|
NotifyData string `json:"notifyData"` // 支付回调数据
|
|
CreatedAt time.Time `json:"createdAt"` // 创建时间
|
|
UpdatedAt time.Time `json:"updatedAt"` // 更新时间
|
|
DeletedAt *time.Time `gorm:"index" json:"deletedAt"` // 删除时间
|
|
}
|
|
|
|
// TableName 指定表名
|
|
|
|
// IsValid 检查支付订单是否有效
|
|
func (p *Payment) IsValid() bool {
|
|
return p.ID > 0 && p.OrderNo != "" && p.UserID > 0 && p.TargetID > 0
|
|
}
|
|
|
|
// IsSuccess 检查支付是否成功
|
|
func (p *Payment) IsSuccess() bool {
|
|
return p.Status == PaymentStatusSuccess
|
|
}
|
|
|
|
// IsFailed 检查支付是否失败
|
|
func (p *Payment) IsFailed() bool {
|
|
return p.Status == PaymentStatusFailed
|
|
}
|
|
|
|
// IsRefunded 检查是否已退款
|
|
func (p *Payment) IsRefunded() bool {
|
|
return p.Status == PaymentStatusRefunded
|
|
}
|
|
|
|
// NewPayment 创建支付订单
|
|
func NewPayment(orderNo string, userID, targetID uint64, paymentType PaymentType, amount int64, description string) *Payment {
|
|
return &Payment{
|
|
OrderNo: orderNo,
|
|
UserID: userID,
|
|
TargetID: targetID,
|
|
Type: paymentType,
|
|
Amount: amount,
|
|
Status: PaymentStatusPending,
|
|
Description: description,
|
|
}
|
|
}
|