init: 1.0.0
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/cache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ Model = (*customPaymentModel)(nil)
|
||||
var (
|
||||
cachePaymentIdPrefix = "cache:payment:id:"
|
||||
cachePaymentTokenPrefix = "cache:payment:token:"
|
||||
)
|
||||
|
||||
type (
|
||||
Model interface {
|
||||
paymentModel
|
||||
customPaymentLogicModel
|
||||
}
|
||||
paymentModel interface {
|
||||
Insert(ctx context.Context, data *Payment, tx ...*gorm.DB) error
|
||||
FindOne(ctx context.Context, id int64) (*Payment, error)
|
||||
Update(ctx context.Context, data *Payment, tx ...*gorm.DB) error
|
||||
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
|
||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||
}
|
||||
|
||||
customPaymentModel struct {
|
||||
*defaultPaymentModel
|
||||
}
|
||||
defaultPaymentModel struct {
|
||||
cache.CachedConn
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
func newPaymentModel(db *gorm.DB, c *redis.Client) *defaultPaymentModel {
|
||||
return &defaultPaymentModel{
|
||||
CachedConn: cache.NewConn(db, c),
|
||||
table: "`Payment`",
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:unused
|
||||
func (m *defaultPaymentModel) batchGetCacheKeys(Payments ...*Payment) []string {
|
||||
var keys []string
|
||||
for _, payment := range Payments {
|
||||
keys = append(keys, m.getCacheKeys(payment)...)
|
||||
}
|
||||
return keys
|
||||
|
||||
}
|
||||
func (m *defaultPaymentModel) getCacheKeys(data *Payment) []string {
|
||||
if data == nil {
|
||||
return []string{}
|
||||
}
|
||||
paymentIdKey := fmt.Sprintf("%s%v", cachePaymentIdPrefix, data.Id)
|
||||
paymentNameKey := fmt.Sprintf("%s%v", cachePaymentTokenPrefix, data.Token)
|
||||
cacheKeys := []string{
|
||||
paymentIdKey,
|
||||
paymentNameKey,
|
||||
}
|
||||
return cacheKeys
|
||||
}
|
||||
|
||||
func (m *defaultPaymentModel) Insert(ctx context.Context, data *Payment, tx ...*gorm.DB) error {
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Create(&data).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultPaymentModel) FindOne(ctx context.Context, id int64) (*Payment, error) {
|
||||
PaymentIdKey := fmt.Sprintf("%s%v", cachePaymentIdPrefix, id)
|
||||
var resp Payment
|
||||
err := m.QueryCtx(ctx, &resp, PaymentIdKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Payment{}).Where("`id` = ?", id).First(&resp).Error
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return &resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultPaymentModel) Update(ctx context.Context, data *Payment, tx ...*gorm.DB) error {
|
||||
old, err := m.FindOne(ctx, data.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Save(data).Error
|
||||
}, m.getCacheKeys(old)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultPaymentModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Delete(&Payment{}, id).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultPaymentModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||
return m.TransactCtx(ctx, fn)
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type customPaymentLogicModel interface {
|
||||
FindOneByPaymentToken(ctx context.Context, token string) (*Payment, error)
|
||||
FindAll(ctx context.Context) ([]*Payment, error)
|
||||
FindListByPage(ctx context.Context, page, size int, req *Filter) (int64, []*Payment, error)
|
||||
FindAvailableMethods(ctx context.Context) ([]*Payment, error)
|
||||
}
|
||||
|
||||
// NewModel returns a model for the database table.
|
||||
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||
return &customPaymentModel{
|
||||
defaultPaymentModel: newPaymentModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customPaymentModel) FindOneByPaymentToken(ctx context.Context, token string) (*Payment, error) {
|
||||
var resp *Payment
|
||||
key := cachePaymentTokenPrefix + token
|
||||
err := m.QueryCtx(ctx, &resp, key, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Payment{}).Where("token = ?", token).First(v).Error
|
||||
})
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (m *customPaymentModel) FindAll(ctx context.Context) ([]*Payment, error) {
|
||||
var resp []*Payment
|
||||
err := m.QueryNoCacheCtx(ctx, &resp, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Payment{}).Find(v).Error
|
||||
})
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (m *customPaymentModel) FindAvailableMethods(ctx context.Context) ([]*Payment, error) {
|
||||
var resp []*Payment
|
||||
err := m.QueryNoCacheCtx(ctx, &resp, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Payment{}).Where("enable = ?", true).Find(v).Error
|
||||
})
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (m *customPaymentModel) FindListByPage(ctx context.Context, page, size int, req *Filter) (int64, []*Payment, error) {
|
||||
var resp []*Payment
|
||||
var total int64
|
||||
err := m.QueryNoCacheCtx(ctx, &resp, func(conn *gorm.DB, v interface{}) error {
|
||||
conn = conn.Model(&Payment{})
|
||||
if req != nil {
|
||||
if req.Enable != nil {
|
||||
conn = conn.Where("`enable` = ?", *req.Enable)
|
||||
}
|
||||
if req.Mark != "" {
|
||||
conn = conn.Where("`mark` = ?", req.Mark)
|
||||
}
|
||||
if req.Search != "" {
|
||||
conn = conn.Where("`name` LIKE ?", "%"+req.Search+"%")
|
||||
}
|
||||
}
|
||||
|
||||
return conn.Count(&total).Offset((page - 1) * size).Limit(size).Find(v).Error
|
||||
})
|
||||
return total, resp, err
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Payment struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
Name string `gorm:"type:varchar(100);not null;default:'';comment:Payment Name"`
|
||||
Platform string `gorm:"<-:create;type:varchar(100);not null;comment:Payment Platform"`
|
||||
Icon string `gorm:"type:varchar(255);default:'';comment:Payment Icon"`
|
||||
Domain string `gorm:"type:varchar(255);default:'';comment:Notification Domain"`
|
||||
Config string `gorm:"type:text;not null;comment:Payment Configuration"`
|
||||
Description string `gorm:"type:text;comment:Payment Description"`
|
||||
FeeMode uint `gorm:"type:tinyint(1);not null;default:0;comment:Fee Mode: 0: No Fee 1: Percentage 2: Fixed Amount 3: Percentage + Fixed Amount"`
|
||||
FeePercent int64 `gorm:"type:int;default:0;comment:Fee Percentage"`
|
||||
FeeAmount int64 `gorm:"type:int;default:0;comment:Fixed Fee Amount"`
|
||||
Enable *bool `gorm:"type:tinyint(1);not null;default:0;comment:Is Enabled"`
|
||||
Token string `gorm:"type:varchar(255);unique;not null;default:'';comment:Payment Token"`
|
||||
}
|
||||
|
||||
func (*Payment) TableName() string {
|
||||
return "payment"
|
||||
}
|
||||
|
||||
func (l *Payment) BeforeDelete(_ *gorm.DB) (err error) {
|
||||
if l.Id == -1 {
|
||||
return fmt.Errorf("can't delete default payment method")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Filter struct {
|
||||
Mark string
|
||||
Enable *bool
|
||||
Search string
|
||||
}
|
||||
|
||||
type StripeConfig struct {
|
||||
PublicKey string `json:"public_key"`
|
||||
SecretKey string `json:"secret_key"`
|
||||
WebhookSecret string `json:"webhook_secret"`
|
||||
Payment string `json:"payment"`
|
||||
}
|
||||
|
||||
func (l *StripeConfig) Marshal() string {
|
||||
b, _ := json.Marshal(l)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (l *StripeConfig) Unmarshal(s string) error {
|
||||
return json.Unmarshal([]byte(s), l)
|
||||
}
|
||||
|
||||
type AlipayF2FConfig struct {
|
||||
AppId string `json:"app_id"`
|
||||
PrivateKey string `json:"private_key"`
|
||||
PublicKey string `json:"public_key"`
|
||||
InvoiceName string `json:"invoice_name"`
|
||||
Sandbox bool `json:"sandbox"`
|
||||
}
|
||||
|
||||
func (l *AlipayF2FConfig) Marshal() string {
|
||||
b, _ := json.Marshal(l)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (l *AlipayF2FConfig) Unmarshal(s string) error {
|
||||
return json.Unmarshal([]byte(s), l)
|
||||
}
|
||||
|
||||
type EPayConfig struct {
|
||||
Pid string `json:"pid"`
|
||||
Url string `json:"url"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
func (l *EPayConfig) Marshal() string {
|
||||
b, _ := json.Marshal(l)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (l *EPayConfig) Unmarshal(s string) error {
|
||||
return json.Unmarshal([]byte(s), l)
|
||||
}
|
||||
Reference in New Issue
Block a user