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

@@ -0,0 +1,253 @@
package payments
import (
"crypto/x509"
"encoding/pem"
"reflect"
"strings"
"testing"
)
func testConfigs(t *testing.T) map[string]Config {
t.Helper()
merchant := testKey(t)
platform := testKey(t)
private := string(pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: mustPKCS8(t, merchant)}))
public := string(pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: mustPKIX(t, &platform.PublicKey)}))
return map[string]Config{
"alipay": {
"ALIPAY_APP_ID": "app", "ALIPAY_SELLER_ID": "seller", "ALIPAY_PRIVATE_KEY": private,
"ALIPAY_PUBLIC_KEY": public, "ALIPAY_NOTIFY_URL": "https://merchant.example/alipay",
},
"wechat": {
"WECHAT_MCH_ID": "merchant", "WECHAT_APP_ID": "app", "WECHAT_SERIAL_NO": "merchant-serial",
"WECHAT_PUBLIC_KEY_ID": "PUB_KEY_ID_0000000000000024101100397200000006", "WECHAT_PUBLIC_KEY": public,
"WECHAT_PLATFORM_SERIAL": "platform-serial", "WECHAT_PRIVATE_KEY": private,
"WECHAT_PLATFORM_PUBLIC_KEY": public, "WECHAT_API_V3_KEY": "01234567890123456789012345678901",
"WECHAT_NOTIFY_URL": "https://merchant.example/wechat",
},
}
}
func mustPKCS8(t *testing.T, key any) []byte {
t.Helper()
b, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
t.Fatal(err)
}
return b
}
func mustPKIX(t *testing.T, key any) []byte {
t.Helper()
b, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
t.Fatal(err)
}
return b
}
func cloneConfig(c Config) Config {
copy := Config{}
for k, v := range c {
copy[k] = v
}
return copy
}
func TestNewProviderExplicitConfig(t *testing.T) {
for channel, cfg := range testConfigs(t) {
t.Run(channel, func(t *testing.T) {
before := cloneConfig(cfg)
// An invalid process environment must not affect explicit construction.
for _, key := range RequiredSettings(channel) {
t.Setenv(key, "invalid environment value")
}
p, err := NewProvider(channel, cfg)
if err != nil || p == nil {
t.Fatalf("provider: %T, %v", p, err)
}
if !reflect.DeepEqual(cfg, before) {
t.Fatal("constructor modified configuration")
}
switch v := p.(type) {
case *alipay:
if v.endpoint != alipayEndpoint || v.app != cfg["ALIPAY_APP_ID"] {
t.Fatal("incorrect Alipay config")
}
case *wechat:
if v.endpoint != wechatEndpoint || v.publicKeyID != cfg["WECHAT_PUBLIC_KEY_ID"] || v.verificationKey(cfg["WECHAT_PLATFORM_SERIAL"]) == nil {
t.Fatal("incorrect WeChat config")
}
default:
t.Fatalf("unexpected provider %T", p)
}
})
}
}
func TestNewProviderValidation(t *testing.T) {
configs := testConfigs(t)
for channel, cfg := range configs {
t.Run(channel, func(t *testing.T) {
for _, key := range RequiredSettings(channel) {
t.Run("missing_"+key, func(t *testing.T) {
missing := cloneConfig(cfg)
delete(missing, key)
if _, err := NewProvider(channel, missing); err == nil || !strings.Contains(err.Error(), key) {
t.Fatalf("missing %s: %v", key, err)
}
})
t.Run("blank_"+key, func(t *testing.T) {
blank := cloneConfig(cfg)
blank[key] = " \t"
if _, err := NewProvider(channel, blank); err == nil || !strings.Contains(err.Error(), key) {
t.Fatalf("blank %s: %v", key, err)
}
})
}
for _, value := range []string{"http://merchant.example/notify", "javascript:alert(1)", "https://", "https://name:secret@merchant.example/notify", "https://merchant.example/notify#fragment"} {
t.Run("notify_"+value, func(t *testing.T) {
bad := cloneConfig(cfg)
bad[strings.ToUpper(channel)+"_NOTIFY_URL"] = value
if _, err := NewProvider(channel, bad); err == nil {
t.Fatalf("accepted %q", value)
}
})
}
bad := cloneConfig(cfg)
bad[strings.ToUpper(channel)+"_PRIVATE_KEY"] = "invalid PEM"
if _, err := NewProvider(channel, bad); err == nil {
t.Fatal("accepted invalid RSA private key")
}
bad = cloneConfig(cfg)
publicKeyName := "ALIPAY_PUBLIC_KEY"
if channel == "wechat" {
publicKeyName = "WECHAT_PLATFORM_PUBLIC_KEY"
}
bad[publicKeyName] = "invalid PEM"
if _, err := NewProvider(channel, bad); err == nil {
t.Fatal("accepted invalid RSA public key")
}
bad = cloneConfig(cfg)
bad[strings.ToUpper(channel)+"_ENDPOINT"] = "https://attacker.example"
if _, err := NewProvider(channel, bad); err == nil {
t.Fatal("accepted custom gateway")
}
bad[strings.ToUpper(channel)+"_ENDPOINT"] = "http://127.0.0.1:8080"
if _, err := NewProvider(channel, bad); err == nil {
t.Fatal("accepted local gateway")
}
bad[strings.ToUpper(channel)+"_ENDPOINT"] = map[string]string{"alipay": alipayEndpoint, "wechat": wechatEndpoint}[channel]
if _, err := NewProvider(channel, bad); err != nil {
t.Fatalf("rejected official gateway: %v", err)
}
})
}
bad := cloneConfig(configs["wechat"])
bad["WECHAT_API_V3_KEY"] = "short"
if _, err := NewProvider("wechat", bad); err == nil {
t.Fatal("accepted short API v3 key")
}
for _, channel := range []string{"", "ALIPAY", "other"} {
if _, err := NewProvider(channel, nil); err == nil {
t.Fatalf("accepted channel %q", channel)
}
if len(RequiredSettings(channel)) != 0 || len(ConfigFromEnv(channel)) != 0 {
t.Fatalf("unknown channel %q has settings", channel)
}
}
}
func TestConfigFromEnvSnapshot(t *testing.T) {
for channel, cfg := range testConfigs(t) {
t.Run(channel, func(t *testing.T) {
for key, value := range cfg {
t.Setenv(key, value)
}
endpointKey := strings.ToUpper(channel) + "_ENDPOINT"
t.Setenv(endpointKey, "https://custom.example")
first := ConfigFromEnv(channel)
if len(first) != len(cfg)+1 {
t.Fatalf("unexpected snapshot size: %d", len(first))
}
for key, value := range cfg {
if first[key] != strings.TrimSpace(value) {
t.Fatalf("incorrect %s snapshot", key)
}
}
if first[endpointKey] != "https://custom.example" {
t.Fatal("legacy endpoint missing")
}
t.Setenv(endpointKey, "")
for key := range cfg {
t.Setenv(key, "")
}
if len(ConfigFromEnv(channel)) != 0 {
t.Fatal("environment did not clear")
}
if first[endpointKey] != "https://custom.example" {
t.Fatal("snapshot mutated")
}
if _, err := NewProvider(channel, first); err == nil {
t.Fatal("custom endpoint from environment accepted")
}
})
}
}
func TestWechatVerificationPairs(t *testing.T) {
base := testConfigs(t)["wechat"]
for _, mode := range []string{"public-only", "platform-only", "both", "none", "missing-public", "missing-id", "bad-id"} {
t.Run(mode, func(t *testing.T) {
cfg := cloneConfig(base)
switch mode {
case "public-only":
delete(cfg, "WECHAT_PLATFORM_SERIAL")
delete(cfg, "WECHAT_PLATFORM_PUBLIC_KEY")
case "platform-only":
delete(cfg, "WECHAT_PUBLIC_KEY_ID")
delete(cfg, "WECHAT_PUBLIC_KEY")
case "none":
delete(cfg, "WECHAT_PLATFORM_SERIAL")
delete(cfg, "WECHAT_PLATFORM_PUBLIC_KEY")
delete(cfg, "WECHAT_PUBLIC_KEY_ID")
delete(cfg, "WECHAT_PUBLIC_KEY")
case "missing-public":
delete(cfg, "WECHAT_PUBLIC_KEY")
case "missing-id":
delete(cfg, "WECHAT_PUBLIC_KEY_ID")
case "bad-id":
cfg["WECHAT_PUBLIC_KEY_ID"] = "merchant-cert"
}
_, err := NewProvider("wechat", cfg)
valid := mode == "public-only" || mode == "platform-only" || mode == "both"
if (err == nil) != valid {
t.Fatalf("unexpected configuration result: %v", err)
}
})
}
}
func TestRequiredSettingsIndependent(t *testing.T) {
for channel, cfg := range testConfigs(t) {
a := RequiredSettings(channel)
if channel == "wechat" {
if len(a) >= len(cfg) {
t.Fatalf("wechat optional verification settings became required")
}
} else if len(a) != len(cfg) {
t.Fatalf("%s: expected %d required settings, got %d", channel, len(cfg), len(a))
}
a[0] = "CHANGED"
b := RequiredSettings(channel)
if b[0] == "CHANGED" {
t.Fatalf("%s: shared settings slice", channel)
}
for _, key := range b {
if _, ok := cfg[key]; !ok {
t.Fatalf("%s: unknown setting %s", channel, key)
}
}
}
}