refactor payment platform canonicalization and order method consistency
Build docker and publish / build (20.15.1) (push) Failing after 8m1s

This commit is contained in:
2026-03-04 02:15:32 -08:00
parent 82626dd749
commit 0c544268e5
10 changed files with 160 additions and 23 deletions
+60 -14
View File
@@ -1,6 +1,10 @@
package payment
import "github.com/perfect-panel/server/internal/types"
import (
"strings"
"github.com/perfect-panel/server/internal/types"
)
type Platform int
@@ -14,32 +18,74 @@ const (
UNSUPPORTED Platform = -1
)
var platformNames = map[string]Platform{
"CryptoSaaS": CryptoSaaS,
"Stripe": Stripe,
"AlipayF2F": AlipayF2F,
"EPay": EPay,
"AppleIAP": AppleIAP,
const (
platformNameStripe = "Stripe"
platformNameAlipayF2F = "AlipayF2F"
platformNameEPay = "EPay"
platformNameBalance = "balance"
platformNameCryptoSaaS = "CryptoSaaS"
platformNameAppleIAP = "AppleIAP"
platformNameUnsupported = "unsupported"
)
var platformAliasToType = map[string]Platform{
"stripe": Stripe,
"alipayf2f": AlipayF2F,
"alipay_f2f": AlipayF2F,
"epay": EPay,
"balance": Balance,
"unsupported": UNSUPPORTED,
"cryptosaas": CryptoSaaS,
"crypto_saas": CryptoSaaS,
"appleiap": AppleIAP,
"apple_iap": AppleIAP,
}
func (p Platform) String() string {
for k, v := range platformNames {
if v == p {
return k
}
switch p {
case Stripe:
return platformNameStripe
case AlipayF2F:
return platformNameAlipayF2F
case EPay:
return platformNameEPay
case Balance:
return platformNameBalance
case CryptoSaaS:
return platformNameCryptoSaaS
case AppleIAP:
return platformNameAppleIAP
default:
return platformNameUnsupported
}
return "unsupported"
}
func ParsePlatform(s string) Platform {
if p, ok := platformNames[s]; ok {
normalized := normalizePlatformAlias(s)
if p, ok := platformAliasToType[normalized]; ok {
return p
}
compact := strings.ReplaceAll(normalized, "_", "")
if p, ok := platformAliasToType[compact]; ok {
return p
}
return UNSUPPORTED
}
func CanonicalPlatformName(input string) (string, bool) {
platform := ParsePlatform(input)
if platform == UNSUPPORTED {
return "", false
}
return platform.String(), true
}
func normalizePlatformAlias(input string) string {
normalized := strings.ToLower(strings.TrimSpace(input))
normalized = strings.ReplaceAll(normalized, "-", "_")
normalized = strings.ReplaceAll(normalized, " ", "")
return normalized
}
func GetSupportedPlatforms() []types.PlatformInfo {
return []types.PlatformInfo{
{
+69
View File
@@ -0,0 +1,69 @@
package payment
import "testing"
func TestParsePlatform(t *testing.T) {
testCases := []struct {
name string
input string
expected Platform
}{
{name: "exact AppleIAP", input: "AppleIAP", expected: AppleIAP},
{name: "snake apple_iap", input: "apple_iap", expected: AppleIAP},
{name: "kebab apple-iap", input: "apple-iap", expected: AppleIAP},
{name: "compact appleiap", input: "appleiap", expected: AppleIAP},
{name: "trimmed value", input: " apple_iap ", expected: AppleIAP},
{name: "legacy exact CryptoSaaS", input: "CryptoSaaS", expected: CryptoSaaS},
{name: "snake crypto_saas", input: "crypto_saas", expected: CryptoSaaS},
{name: "unsupported", input: "unknown_gateway", expected: UNSUPPORTED},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got := ParsePlatform(testCase.input)
if got != testCase.expected {
t.Fatalf("ParsePlatform(%q) = %v, expected %v", testCase.input, got, testCase.expected)
}
})
}
}
func TestPlatformStringIsCanonical(t *testing.T) {
testCases := []struct {
name string
input Platform
expected string
}{
{name: "stripe", input: Stripe, expected: "Stripe"},
{name: "alipay", input: AlipayF2F, expected: "AlipayF2F"},
{name: "epay", input: EPay, expected: "EPay"},
{name: "balance", input: Balance, expected: "balance"},
{name: "crypto", input: CryptoSaaS, expected: "CryptoSaaS"},
{name: "apple", input: AppleIAP, expected: "AppleIAP"},
{name: "unsupported", input: UNSUPPORTED, expected: "unsupported"},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got := testCase.input.String()
if got != testCase.expected {
t.Fatalf("Platform.String() = %q, expected %q", got, testCase.expected)
}
})
}
}
func TestCanonicalPlatformName(t *testing.T) {
canonical, ok := CanonicalPlatformName("apple_iap")
if !ok {
t.Fatalf("expected apple_iap to be supported")
}
if canonical != "AppleIAP" {
t.Fatalf("canonical name mismatch: got %q", canonical)
}
_, ok = CanonicalPlatformName("not_exists")
if ok {
t.Fatalf("expected unsupported platform to return ok=false")
}
}