mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-21 15:09:21 +00:00
Replace iframe-based external payment with native implementation: Backend (Go): - 5 new Ent schemas: PaymentOrder, PaymentAuditLog, PaymentChannel, SubscriptionPlan, PaymentProviderInstance - 5 SQL migrations (090-094) - Payment provider interface + registry + load balancer - 4 provider implementations (EasyPay, Alipay, WxPay, Stripe) - PaymentService (order lifecycle, fulfillment, refund 3-phase) - PaymentConfigService (settings, instance/channel/plan CRUD) - 31 API endpoints (9 user + 4 webhook + 18 admin) - Wire DI integration Frontend (Vue3): - Payment types, API modules, store, router changes - Sidebar: payment_enabled controls visibility, new menu items - User pages: PaymentView (replace iframe), QR payment, Stripe, result page, order list - Admin pages: Order management (4 tabs), provider instance mgmt - Settings: new payment tab, removed old purchase iframe config - i18n: full payment namespace (en + zh) Docs: - PAYMENT_FRONTEND_MIGRATION_PLAN.md - PAYMENT_BACKEND_MIGRATION_PLAN.md - PAYMENT_IMPLEMENTATION_ROADMAP.md
135 lines
4.0 KiB
Go
135 lines
4.0 KiB
Go
package payment
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
dbent "github.com/Wei-Shaw/sub2api/ent"
|
|
"github.com/Wei-Shaw/sub2api/ent/paymentorder"
|
|
"github.com/Wei-Shaw/sub2api/ent/paymentproviderinstance"
|
|
)
|
|
|
|
// Strategy represents a load balancing strategy for provider instance selection.
|
|
type Strategy string
|
|
|
|
const (
|
|
StrategyRoundRobin Strategy = "round_robin"
|
|
StrategyLeastAmount Strategy = "least_amount"
|
|
)
|
|
|
|
// InstanceLimits holds per-channel limits for a provider instance (JSON).
|
|
type InstanceLimits map[string]struct {
|
|
DailyLimit float64 `json:"dailyLimit,omitempty"`
|
|
SingleMin float64 `json:"singleMin,omitempty"`
|
|
SingleMax float64 `json:"singleMax,omitempty"`
|
|
}
|
|
|
|
// LoadBalancer selects a provider instance for a given payment type.
|
|
type LoadBalancer interface {
|
|
SelectInstance(ctx context.Context, providerKey string, paymentType PaymentType) (*InstanceSelection, error)
|
|
}
|
|
|
|
// DefaultLoadBalancer implements LoadBalancer using database queries.
|
|
type DefaultLoadBalancer struct {
|
|
db *dbent.Client
|
|
encryptionKey []byte
|
|
counter atomic.Uint64
|
|
}
|
|
|
|
// NewDefaultLoadBalancer creates a new load balancer.
|
|
func NewDefaultLoadBalancer(db *dbent.Client, encryptionKey []byte) *DefaultLoadBalancer {
|
|
return &DefaultLoadBalancer{db: db, encryptionKey: encryptionKey}
|
|
}
|
|
|
|
// SelectInstance picks an enabled instance for the given provider key and payment type.
|
|
func (lb *DefaultLoadBalancer) SelectInstance(ctx context.Context, providerKey string, paymentType PaymentType) (*InstanceSelection, error) {
|
|
instances, err := lb.db.PaymentProviderInstance.Query().
|
|
Where(
|
|
paymentproviderinstance.ProviderKey(providerKey),
|
|
paymentproviderinstance.Enabled(true),
|
|
).
|
|
Order(dbent.Asc(paymentproviderinstance.FieldSortOrder)).
|
|
All(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query provider instances: %w", err)
|
|
}
|
|
|
|
// Filter by supported types
|
|
var candidates []*dbent.PaymentProviderInstance
|
|
for _, inst := range instances {
|
|
if inst.SupportedTypes == "" || containsType(inst.SupportedTypes, paymentType) {
|
|
candidates = append(candidates, inst)
|
|
}
|
|
}
|
|
|
|
if len(candidates) == 0 {
|
|
return nil, fmt.Errorf("no enabled instance for provider %s type %s", providerKey, paymentType)
|
|
}
|
|
|
|
// Round-robin selection (default strategy)
|
|
idx := lb.counter.Add(1) % uint64(len(candidates))
|
|
selected := candidates[idx]
|
|
|
|
// Decrypt config
|
|
config, err := lb.decryptConfig(selected.Config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("decrypt instance %d config: %w", selected.ID, err)
|
|
}
|
|
|
|
return &InstanceSelection{
|
|
InstanceID: fmt.Sprintf("%d", selected.ID),
|
|
Config: config,
|
|
}, nil
|
|
}
|
|
|
|
func (lb *DefaultLoadBalancer) decryptConfig(encrypted string) (map[string]string, error) {
|
|
plaintext, err := Decrypt(encrypted, lb.encryptionKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var config map[string]string
|
|
if err := json.Unmarshal([]byte(plaintext), &config); err != nil {
|
|
return nil, fmt.Errorf("unmarshal config: %w", err)
|
|
}
|
|
return config, nil
|
|
}
|
|
|
|
// GetInstanceDailyAmount returns the total completed order amount for an instance today.
|
|
func (lb *DefaultLoadBalancer) GetInstanceDailyAmount(ctx context.Context, instanceID string) (float64, error) {
|
|
now := time.Now()
|
|
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
|
|
var result []struct {
|
|
Sum float64 `json:"sum"`
|
|
}
|
|
err := lb.db.PaymentOrder.Query().
|
|
Where(
|
|
paymentorder.ProviderInstanceID(instanceID),
|
|
paymentorder.StatusIn("COMPLETED", "PAID", "RECHARGING"),
|
|
paymentorder.PaidAtGTE(todayStart),
|
|
).
|
|
Aggregate(dbent.Sum(paymentorder.FieldPayAmount)).
|
|
Scan(ctx, &result)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("query daily amount: %w", err)
|
|
}
|
|
if len(result) > 0 {
|
|
return result[0].Sum, nil
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
// containsType checks if a comma-separated list contains the given type.
|
|
func containsType(supportedTypes string, target PaymentType) bool {
|
|
for _, t := range strings.Split(supportedTypes, ",") {
|
|
if strings.TrimSpace(t) == target {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|