feat: add online payment management and checkout
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-15 09:26:40 +08:00
parent 5353a4a128
commit dc2efcde08
41 changed files with 4855 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import (
"log/slog"
"net"
"net/http"
"time"
_ "github.com/go-sql-driver/mysql"
"os"
@@ -15,6 +16,7 @@ import (
"license-platform/backend/internal/config"
"license-platform/backend/internal/httpapi"
"license-platform/backend/internal/payments"
)
func run() error {
@@ -30,6 +32,11 @@ func run() error {
}
defer closeLimiter()
router := httpapi.NewOperationalRouter(ops)
providers, err := payments.LoadFromEnv()
if err != nil {
return fmt.Errorf("payment configuration invalid: %w", err)
}
var paymentAPI *httpapi.BusinessAPI
var readinessDB *sql.DB
if dsn := os.Getenv("MYSQL_DSN"); dsn != "" {
db, err := sql.Open("mysql", dsn)
@@ -48,7 +55,7 @@ func run() error {
if err != nil {
return err
}
httpapi.RegisterBusinessWithKeyring(router, db, cfg.SigningPrivateKeyPEM, ring, cfg.CredentialEncryptionKey)
paymentAPI = httpapi.RegisterBusinessWithPayments(router, db, cfg.SigningPrivateKeyPEM, ring, cfg.CredentialEncryptionKey, providers, cfg.PaymentConfigEncryptionKey)
}
// Readiness is registered only on the loopback operations listener.
if err := router.SetTrustedProxies(cfg.TrustedProxies); err != nil {
@@ -61,6 +68,22 @@ func run() error {
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if paymentAPI != nil {
go func() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
checkCtx, cancel := context.WithTimeout(ctx, 45*time.Second)
paymentAPI.ReconcilePayments(checkCtx)
cancel()
}
}
}()
}
if addr := os.Getenv("METRICS_ADDR"); addr != "" {
host, _, err := net.SplitHostPort(addr)
if err != nil || net.ParseIP(host) == nil || !net.ParseIP(host).IsLoopback() {