141 lines
4.9 KiB
Go
141 lines
4.9 KiB
Go
|
|
package entities
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"github.com/google/uuid"
|
|||
|
|
"github.com/shopspring/decimal"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// AlipayOrderStatus 支付宝订单状态枚举
|
|||
|
|
type AlipayOrderStatus string
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
AlipayOrderStatusPending AlipayOrderStatus = "pending" // 待支付
|
|||
|
|
AlipayOrderStatusSuccess AlipayOrderStatus = "success" // 支付成功
|
|||
|
|
AlipayOrderStatusFailed AlipayOrderStatus = "failed" // 支付失败
|
|||
|
|
AlipayOrderStatusCancelled AlipayOrderStatus = "cancelled" // 已取消
|
|||
|
|
AlipayOrderStatusClosed AlipayOrderStatus = "closed" // 已关闭
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
AlipayOrderPlatformApp = "app" // 支付宝APP支付
|
|||
|
|
AlipayOrderPlatformH5 = "h5" // 支付宝H5支付
|
|||
|
|
AlipayOrderPlatformPC = "pc" // 支付宝PC支付
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// AlipayOrder 支付宝订单详情实体
|
|||
|
|
type AlipayOrder struct {
|
|||
|
|
// 基础标识
|
|||
|
|
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"支付宝订单唯一标识"`
|
|||
|
|
RechargeID string `gorm:"type:varchar(36);not null;uniqueIndex" json:"recharge_id" comment:"关联充值记录ID"`
|
|||
|
|
OutTradeNo string `gorm:"type:varchar(64);not null;uniqueIndex" json:"out_trade_no" comment:"商户订单号"`
|
|||
|
|
TradeNo *string `gorm:"type:varchar(64);uniqueIndex" json:"trade_no,omitempty" comment:"支付宝交易号"`
|
|||
|
|
|
|||
|
|
// 订单信息
|
|||
|
|
Subject string `gorm:"type:varchar(200);not null" json:"subject" comment:"订单标题"`
|
|||
|
|
Amount decimal.Decimal `gorm:"type:decimal(20,8);not null" json:"amount" comment:"订单金额"`
|
|||
|
|
Platform string `gorm:"type:varchar(20);not null" json:"platform" comment:"支付平台:app/h5/pc"`
|
|||
|
|
Status AlipayOrderStatus `gorm:"type:varchar(20);not null;default:'pending';index" json:"status" comment:"订单状态"`
|
|||
|
|
|
|||
|
|
// 支付宝返回信息
|
|||
|
|
BuyerID string `gorm:"type:varchar(64)" json:"buyer_id,omitempty" comment:"买家支付宝用户ID"`
|
|||
|
|
SellerID string `gorm:"type:varchar(64)" json:"seller_id,omitempty" comment:"卖家支付宝用户ID"`
|
|||
|
|
PayAmount decimal.Decimal `gorm:"type:decimal(20,8)" json:"pay_amount,omitempty" comment:"实际支付金额"`
|
|||
|
|
ReceiptAmount decimal.Decimal `gorm:"type:decimal(20,8)" json:"receipt_amount,omitempty" comment:"实收金额"`
|
|||
|
|
|
|||
|
|
// 回调信息
|
|||
|
|
NotifyTime *time.Time `gorm:"index" json:"notify_time,omitempty" comment:"异步通知时间"`
|
|||
|
|
ReturnTime *time.Time `gorm:"index" json:"return_time,omitempty" comment:"同步返回时间"`
|
|||
|
|
|
|||
|
|
// 错误信息
|
|||
|
|
ErrorCode string `gorm:"type:varchar(64)" json:"error_code,omitempty" comment:"错误码"`
|
|||
|
|
ErrorMessage string `gorm:"type:text" json:"error_message,omitempty" comment:"错误信息"`
|
|||
|
|
|
|||
|
|
// 时间戳字段
|
|||
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
|||
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
|
|||
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TableName 指定数据库表名
|
|||
|
|
func (AlipayOrder) TableName() string {
|
|||
|
|
return "alipay_orders"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BeforeCreate GORM钩子:创建前自动生成UUID
|
|||
|
|
func (a *AlipayOrder) BeforeCreate(tx *gorm.DB) error {
|
|||
|
|
if a.ID == "" {
|
|||
|
|
a.ID = uuid.New().String()
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// IsPending 检查是否为待支付状态
|
|||
|
|
func (a *AlipayOrder) IsPending() bool {
|
|||
|
|
return a.Status == AlipayOrderStatusPending
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// IsSuccess 检查是否为支付成功状态
|
|||
|
|
func (a *AlipayOrder) IsSuccess() bool {
|
|||
|
|
return a.Status == AlipayOrderStatusSuccess
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// IsFailed 检查是否为支付失败状态
|
|||
|
|
func (a *AlipayOrder) IsFailed() bool {
|
|||
|
|
return a.Status == AlipayOrderStatusFailed
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// IsCancelled 检查是否为已取消状态
|
|||
|
|
func (a *AlipayOrder) IsCancelled() bool {
|
|||
|
|
return a.Status == AlipayOrderStatusCancelled
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// IsClosed 检查是否为已关闭状态
|
|||
|
|
func (a *AlipayOrder) IsClosed() bool {
|
|||
|
|
return a.Status == AlipayOrderStatusClosed
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// MarkSuccess 标记为支付成功
|
|||
|
|
func (a *AlipayOrder) MarkSuccess(tradeNo, buyerID, sellerID string, payAmount, receiptAmount decimal.Decimal) {
|
|||
|
|
a.Status = AlipayOrderStatusSuccess
|
|||
|
|
a.TradeNo = &tradeNo
|
|||
|
|
a.BuyerID = buyerID
|
|||
|
|
a.SellerID = sellerID
|
|||
|
|
a.PayAmount = payAmount
|
|||
|
|
a.ReceiptAmount = receiptAmount
|
|||
|
|
now := time.Now()
|
|||
|
|
a.NotifyTime = &now
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// MarkFailed 标记为支付失败
|
|||
|
|
func (a *AlipayOrder) MarkFailed(errorCode, errorMessage string) {
|
|||
|
|
a.Status = AlipayOrderStatusFailed
|
|||
|
|
a.ErrorCode = errorCode
|
|||
|
|
a.ErrorMessage = errorMessage
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// MarkCancelled 标记为已取消
|
|||
|
|
func (a *AlipayOrder) MarkCancelled() {
|
|||
|
|
a.Status = AlipayOrderStatusCancelled
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// MarkClosed 标记为已关闭
|
|||
|
|
func (a *AlipayOrder) MarkClosed() {
|
|||
|
|
a.Status = AlipayOrderStatusClosed
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewAlipayOrder 工厂方法 - 创建支付宝订单
|
|||
|
|
func NewAlipayOrder(rechargeID, outTradeNo, subject string, amount decimal.Decimal, platform string) *AlipayOrder {
|
|||
|
|
return &AlipayOrder{
|
|||
|
|
ID: uuid.New().String(),
|
|||
|
|
RechargeID: rechargeID,
|
|||
|
|
OutTradeNo: outTradeNo,
|
|||
|
|
Subject: subject,
|
|||
|
|
Amount: amount,
|
|||
|
|
Platform: platform,
|
|||
|
|
Status: AlipayOrderStatusPending,
|
|||
|
|
}
|
|||
|
|
}
|