package httpapi import ( "context" "database/sql" "encoding/json" "errors" "net/http" "net/http/httptest" "strings" "testing" "github.com/gin-gonic/gin" "license-platform/backend/internal/payments" ) type paymentTestProvider struct { notification payments.Notification verifyErr error } func (p paymentTestProvider) Create(context.Context, string, int64, string) (string, error) { return "https://example.invalid/code", nil } func (p paymentTestProvider) Query(context.Context, string) (payments.Notification, error) { return p.notification, nil } func (p paymentTestProvider) Verify(*http.Request) (payments.Notification, error) { return p.notification, p.verifyErr } func (p paymentTestProvider) Close(context.Context, string) error { return nil } func (p paymentTestProvider) Refund(context.Context, string, string, int64) error { return nil } func paymentTestEngine(db *sql.DB, providers map[string]payments.Provider) *gin.Engine { gin.SetMode(gin.TestMode) r := gin.New() RegisterBusinessWithPayments(r, db, "", nil, nil, providers) return r } func TestPaymentNotifyWithoutConfiguredProvider(t *testing.T) { r := paymentTestEngine(nil, map[string]payments.Provider{}) req := httptest.NewRequest(http.MethodPost, "/api/v1/payments/alipay/notify", strings.NewReader("{}")) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() r.ServeHTTP(w, req) if w.Code != http.StatusServiceUnavailable || !strings.Contains(w.Body.String(), "channel_unavailable") { t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) } } func TestPaymentNotifyVerificationFailureDoesNotTouchDB(t *testing.T) { provider := paymentTestProvider{verifyErr: errors.New("bad signature")} r := paymentTestEngine(nil, map[string]payments.Provider{"alipay": provider}) req := httptest.NewRequest(http.MethodPost, "/api/v1/payments/alipay/notify", strings.NewReader(`{"trade_no":"should-not-be-used"}`)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() r.ServeHTTP(w, req) if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "invalid_notification") { t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) } } func TestPaymentCheckoutRequiresToken(t *testing.T) { r := paymentTestEngine(nil, nil) req := httptest.NewRequest(http.MethodPost, "/api/v1/payments/checkout", strings.NewReader(`{}`)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() r.ServeHTTP(w, req) if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "invalid_request") { t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) } } func TestPaymentManagementRequiresAuthentication(t *testing.T) { r := paymentTestEngine(nil, nil) for _, path := range []string{"/api/v1/payment-channels", "/api/v1/payment-orders"} { t.Run(path, func(t *testing.T) { w := httptest.NewRecorder() r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil)) if w.Code != 401 || !strings.Contains(w.Body.String(), "unauthorized") { t.Fatalf("status=%d body=%s", w.Code, w.Body.String()) } }) } } func TestPaymentChannelConfiguration(t *testing.T) { for _, enabled := range []string{"none", "alipay", "wechat", "both"} { t.Run(enabled, func(t *testing.T) { providers := map[string]payments.Provider{} for _, channel := range []string{"alipay", "wechat"} { if enabled == channel || enabled == "both" { providers[channel] = paymentTestProvider{} } } t.Setenv("ALIPAY_PRIVATE_KEY", "secret-alipay-test-value") t.Setenv("WECHAT_API_V3_KEY", "secret-wechat-test-value") a := &BusinessAPI{paymentProviders: providers, paymentEnvConfigs: map[string]payments.Config{ "alipay": payments.ConfigFromEnv("alipay"), "wechat": payments.ConfigFromEnv("wechat"), }} r := gin.New() r.GET("/channels", a.listPaymentChannels) w := httptest.NewRecorder() r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/channels", nil)) if w.Code != 200 || w.Header().Get("Cache-Control") != "no-store" { t.Fatalf("status=%d headers=%v", w.Code, w.Header()) } var response struct { Data []struct { Channel string `json:"channel"` Enabled bool `json:"enabled"` Source string `json:"configuration_source"` Settings []string `json:"required_settings"` } } if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil { t.Fatal(err) } if len(response.Data) != 2 { t.Fatalf("channel count=%d", len(response.Data)) } seen := map[string]bool{} for _, row := range response.Data { want := providers[row.Channel] != nil if row.Enabled != want || row.Source != "environment" || len(row.Settings) == 0 { t.Fatalf("unexpected channel: %+v", row) } seen[row.Channel] = true } if !seen["alipay"] || !seen["wechat"] { t.Fatal("missing payment channel") } if strings.Contains(w.Body.String(), "secret-alipay-test-value") || strings.Contains(w.Body.String(), "secret-wechat-test-value") { t.Fatal("configuration values exposed") } }) } }