94 lines
4.0 KiB
Go
Raw Normal View History

2025-07-18 20:10:29 +07:00
package entities
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type PaymentMethodType string
const (
PaymentMethodTypeCash PaymentMethodType = "cash"
PaymentMethodTypeCard PaymentMethodType = "card"
PaymentMethodTypeDigitalWallet PaymentMethodType = "digital_wallet"
)
type PaymentMethod struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
OrganizationID uuid.UUID `gorm:"type:uuid;not null;index" json:"organization_id" validate:"required"`
Name string `gorm:"not null;size:100" json:"name" validate:"required,min=1,max=100"`
Type PaymentMethodType `gorm:"not null;size:50" json:"type" validate:"required,oneof=cash card digital_wallet"`
Processor *string `gorm:"size:100" json:"processor"`
Configuration Metadata `gorm:"type:jsonb;default:'{}'" json:"configuration"`
IsActive bool `gorm:"default:true" json:"is_active"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
Organization Organization `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
Payments []Payment `gorm:"foreignKey:PaymentMethodID" json:"payments,omitempty"`
}
func (pm *PaymentMethod) BeforeCreate(tx *gorm.DB) error {
if pm.ID == uuid.Nil {
pm.ID = uuid.New()
}
return nil
}
func (PaymentMethod) TableName() string {
return "payment_methods"
}
type PaymentTransactionStatus string
const (
PaymentTransactionStatusPending PaymentTransactionStatus = "pending"
PaymentTransactionStatusCompleted PaymentTransactionStatus = "completed"
PaymentTransactionStatusFailed PaymentTransactionStatus = "failed"
PaymentTransactionStatusRefunded PaymentTransactionStatus = "refunded"
)
type Payment struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
OrderID uuid.UUID `gorm:"type:uuid;not null;index" json:"order_id" validate:"required"`
PaymentMethodID uuid.UUID `gorm:"type:uuid;not null;index" json:"payment_method_id" validate:"required"`
Amount float64 `gorm:"type:decimal(10,2);not null" json:"amount" validate:"required,min=0"`
Status PaymentTransactionStatus `gorm:"default:'pending';size:50" json:"status"`
TransactionID *string `gorm:"size:255" json:"transaction_id"`
SplitNumber int `gorm:"default:1" json:"split_number"`
SplitTotal int `gorm:"default:1" json:"split_total"`
SplitDescription *string `gorm:"size:255" json:"split_description,omitempty"`
RefundAmount float64 `gorm:"type:decimal(10,2);default:0.00" json:"refund_amount"`
RefundReason *string `gorm:"size:255" json:"refund_reason,omitempty"`
RefundedAt *time.Time `gorm:"" json:"refunded_at,omitempty"`
RefundedBy *uuid.UUID `gorm:"type:uuid" json:"refunded_by,omitempty"`
Metadata Metadata `gorm:"type:jsonb;default:'{}'" json:"metadata"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
Order Order `gorm:"foreignKey:OrderID" json:"order,omitempty"`
PaymentMethod PaymentMethod `gorm:"foreignKey:PaymentMethodID" json:"payment_method,omitempty"`
PaymentOrderItems []PaymentOrderItem `gorm:"foreignKey:PaymentID" json:"payment_order_items,omitempty"`
}
func (p *Payment) BeforeCreate(tx *gorm.DB) error {
if p.ID == uuid.Nil {
p.ID = uuid.New()
}
return nil
}
func (Payment) TableName() string {
return "payments"
}
func (p *Payment) CanBeRefunded() bool {
return p.Status == PaymentTransactionStatusCompleted
}
func (p *Payment) IsSuccessful() bool {
return p.Status == PaymentTransactionStatusCompleted
}