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

@@ -11,15 +11,16 @@ import (
)
type Config struct {
HTTPAddr string
TrustedProxies []string
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
SigningPrivateKeyPEM string
CredentialEncryptionKey []byte
HTTPAddr string
TrustedProxies []string
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
SigningPrivateKeyPEM string
CredentialEncryptionKey []byte
PaymentConfigEncryptionKey []byte
}
func Load(getenv func(string) string) (Config, error) {
@@ -96,6 +97,16 @@ func Load(getenv func(string) string) (Config, error) {
}
c.CredentialEncryptionKey = data
}
if path := getenv("PAYMENT_CONFIG_ENCRYPTION_KEY_FILE"); path != "" {
if !filepath.IsAbs(path) {
return Config{}, fmt.Errorf("PAYMENT_CONFIG_ENCRYPTION_KEY_FILE must be absolute")
}
data, err := os.ReadFile(path)
if err != nil || len(data) != 32 {
return Config{}, fmt.Errorf("payment config encryption key must be exactly 32 raw bytes")
}
c.PaymentConfigEncryptionKey = data
}
if root := getenv("PACKAGE_STORAGE_ROOT"); root != "" {
if !filepath.IsAbs(root) {
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be absolute")

View File

@@ -63,6 +63,36 @@ func TestLoadRejectsInvalid(t *testing.T) {
}
}
func TestPaymentConfigKeyFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "payments.key")
for _, size := range []int{31, 32, 33} {
if err := os.WriteFile(path, make([]byte, size), 0600); err != nil {
t.Fatal(err)
}
c, err := Load(func(name string) string {
if name == "PAYMENT_CONFIG_ENCRYPTION_KEY_FILE" {
return path
}
return ""
})
if size == 32 {
if err != nil || len(c.PaymentConfigEncryptionKey) != 32 || len(c.CredentialEncryptionKey) != 0 {
t.Fatal("independent payment key loading failed")
}
} else if err == nil {
t.Fatal("invalid payment key length accepted")
}
}
if _, err := Load(func(name string) string {
if name == "PAYMENT_CONFIG_ENCRYPTION_KEY_FILE" {
return "relative/payments.key"
}
return ""
}); err == nil {
t.Fatal("relative payment key accepted")
}
}
func TestCredentialKeyFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "credentials.key")
for _, size := range []int{31, 32, 33} {