package trustpayments import ( "context" "encoding/json" "io" "net/http" "strings" "testing" ) type transportFunc func(*http.Request) (*http.Response, error) func (f transportFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } func fixtureConfig() Config { return Config{BaseURL: "https://sandbox.link.trustpayments.com/trustpayments/rest/", SiteID: "site", Password: "test-secret", Terminal: "terminal", Currency: "GBP", Timezone: "Europe/London"} } func TestTrustSigningMoneyAndConfiguration(t *testing.T) { if got := Token("123456", "2020-03-01:10:20:05", "7876547", "tv%61w%5"); got != "990bc7bb7efee9e383a96f6f929fd00c90f4ec94f4344270c1552e4d5d4d80c5" { t.Errorf("Token=%s", got) } for _, s := range []string{"", "0.00", "-1.00", "1.001", "1", "NaN", "92233720368547758.08", "1e2"} { if _, e := ParseAmount(s); e == nil { t.Errorf("ParseAmount(%q) accepted", s) } } if n, e := ParseAmount("123456.78"); e != nil || Decimal(n) != "123456.78" { t.Fatalf("money=%d/%v", n, e) } for _, url := range []string{"", "http://link.trustpayments.com/trustpayments/rest/", "https://other.invalid/trustpayments/rest/", "https://link.trustpayments.com:443/trustpayments/rest/", "https://link.trustpayments.com/trustpayments/rest/?x=1"} { cfg := fixtureConfig() cfg.BaseURL = url if _, e := New(cfg); e == nil { t.Errorf("New(%q) accepted", url) } } } func TestTrustFinancialEvidenceAndUncertainErrors(t *testing.T) { for _, tc := range []struct { name string change func(map[string]any) status bool state string }{ {"sale", nil, false, Approved}, {"status", nil, true, Approved}, {"adjusted", func(r map[string]any) { r["finalamount"] = "11.00" }, false, Review}, {"wrongAmount", func(r map[string]any) { r["amount"] = "9.00" }, false, Review}, {"providerCurrencyIgnored", func(r map[string]any) { r["currency"] = "EUR" }, false, Approved}, {"wrongRef", func(r map[string]any) { r["refid"] = "other" }, false, Review}, {"wrongSite", func(r map[string]any) { r["siteid"] = "other" }, true, Review}, {"wrongType", func(r map[string]any) { r["type"] = "preauth" }, true, Review}, {"noAmount", func(r map[string]any) { delete(r, "finalamount") }, true, Review}, {"declined", func(r map[string]any) { r["resultcode"] = "Declined"; delete(r, "authcode") }, false, Declined}, {"cancelled", func(r map[string]any) { r["resultcode"] = "Cancelled"; delete(r, "authcode") }, false, Cancelled}, {"pending", func(r map[string]any) { r["resultcode"] = "Pending" }, false, Pending}, {"2003", func(r map[string]any) { r["errorcode"] = 2003 }, false, Unknown}, {"2005", func(r map[string]any) { r["errorcode"] = "2005" }, false, Unknown}, {"2006", func(r map[string]any) { r["errorcode"] = 2006 }, false, Unknown}, {"1011", func(r map[string]any) { r["errorcode"] = 1011 }, false, Review}, {"1008", func(r map[string]any) { r["errorcode"] = 1008 }, false, Review}, {"offline", func(r map[string]any) { r["resultcode"] = "Terminal Busy" }, false, Unknown}, } { t.Run(tc.name, func(t *testing.T) { cfg := fixtureConfig() calls := 0 cfg.Transport = transportFunc(func(req *http.Request) (*http.Response, error) { calls++ var input map[string]string if e := json.NewDecoder(req.Body).Decode(&input); e != nil { t.Fatal(e) } if input["txndate"] != "2026-10-23:12:00:00" || req.Header.Get("token") != Token(cfg.SiteID, input["txndate"], "ref", cfg.Password) { t.Error("signing context changed") } if !tc.status && (input["wait"] != "y" || input["amount"] != "10.00") { t.Error("incorrect financial payload") } r := map[string]any{"refid": "ref", "siteid": "site", "terminalserialnumber": "terminal", "type": "sale", "amount": "10.00", "finalamount": "10.00", "resultcode": "Approved", "receipt": "Sequence number: 42\r\nReceipt: 31\r\nPAN: 4111111111111111", "authcode": "123456", "cardtype": "Visa", "last4": "1133"} if tc.change != nil { tc.change(r) } b, _ := json.Marshal(r) return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(string(b)))}, nil }) c, e := New(cfg) if e != nil { t.Fatal(e) } result, e := c.Execute(context.Background(), Operation{RefID: "ref", TxnDate: "2026-10-23:12:00:00", Request: Request{Kind: "sale", Amount: 1000, Currency: "GBP"}}, tc.status) if e != nil || result.State != tc.state || calls != 1 { t.Fatalf("Execute=%+v/%v, calls=%d, want %s/1", result, e, calls, tc.state) } if result.State == Approved && (result.Metadata.Receipt != "31" || result.Metadata.Sequence != "42" || result.Metadata.LastFour != "1133") { t.Error("metadata lost") } if result.State == Approved && result.Currency != cfg.Currency { t.Errorf("currency metadata=%q, want CMS currency %q", result.Currency, cfg.Currency) } if tc.name == "adjusted" && (!result.ObservedFinalKnown || result.ObservedFinalAmount != 1100 || result.Amount != 0) { t.Fatal("financial mismatch evidence was lost or admitted") } b, _ := json.Marshal(result) if strings.Contains(string(b), "4111111111111111") || strings.Contains(string(b), cfg.Password) { t.Error("sensitive data escaped") } }) } cfg := fixtureConfig() c, _ := New(cfg) r, e := c.Execute(context.Background(), Operation{}, false) if e == nil || r.State == Approved { t.Fatal("invalid operation admitted") } } func TestTrustTransportHTTPAndMalformedResultsRemainUnknown(t *testing.T) { for _, tc := range []struct { name, body string status int }{{"http500", `{"resultcode":"Approved"}`, 500}, {"http401", "", 401}, {"malformed", "{", 200}, {"empty", "{}", 200}, {"unexpectedError", `{"resultcode":"Approved","error":"private-token"}`, 200}} { t.Run(tc.name, func(t *testing.T) { cfg := fixtureConfig() calls := 0 cfg.Transport = transportFunc(func(*http.Request) (*http.Response, error) { calls++ return &http.Response{StatusCode: tc.status, Body: io.NopCloser(strings.NewReader(tc.body))}, nil }) c, e := New(cfg) if e != nil { t.Fatal(e) } r, _ := c.Execute(context.Background(), Operation{RefID: "ref", TxnDate: "2026-10-23:12:00:00", Request: Request{Kind: "sale", Amount: 1000, Currency: "GBP"}}, false) if r.State == Approved || r.State == Declined || calls != 1 { t.Fatalf("uncertain=%+v calls=%d", r, calls) } }) } }