A status check that ran before the card was entered (orderStatus 0, "no payment attempts") finalized the payment as failed, and later callbacks returned it as already finalized without asking GBÜS again - so cardholderName/maskedPan stayed null and a real payment was never recorded. Unpaid in-progress statuses (0, 1, 5) now keep the row pending with the latest response stored, and rows already failed that way are re-checked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
120 lines
4.1 KiB
Go
120 lines
4.1 KiB
Go
package paymentgateway
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRegisterResultSuccess(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
r RegisterResult
|
|
want bool
|
|
}{
|
|
{"ok blank code", RegisterResult{OrderId: "o1", FormUrl: "https://f"}, true},
|
|
{"ok zero code", RegisterResult{OrderId: "o1", FormUrl: "https://f", ErrorCode: "0"}, true},
|
|
{"no orderId", RegisterResult{FormUrl: "https://f"}, false},
|
|
{"no formUrl", RegisterResult{OrderId: "o1"}, false},
|
|
{"error code", RegisterResult{OrderId: "o1", FormUrl: "https://f", ErrorCode: "5"}, false},
|
|
}
|
|
for _, c := range cases {
|
|
if got := c.r.Success(); got != c.want {
|
|
t.Errorf("%s: Success()=%v want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStatusResultInProgress(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
r StatusResult
|
|
want bool
|
|
}{
|
|
{"registered, not attempted", StatusResult{OrderStatus: 0, ErrorCode: "0"}, true},
|
|
{"pre-authorized", StatusResult{OrderStatus: 1}, true},
|
|
{"3-D Secure", StatusResult{OrderStatus: 5, ErrorCode: "0"}, true},
|
|
{"paid", StatusResult{Paid: true, OrderStatus: 2}, false},
|
|
{"reversed", StatusResult{OrderStatus: 3}, false},
|
|
{"declined", StatusResult{OrderStatus: 6, ErrorCode: "0"}, false},
|
|
{"gateway error", StatusResult{OrderStatus: 0, ErrorCode: "6"}, false},
|
|
}
|
|
for _, c := range cases {
|
|
if got := c.r.InProgress(); got != c.want {
|
|
t.Errorf("%s: InProgress()=%v want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLogClientAlwaysPaid(t *testing.T) {
|
|
c := NewLogClient(nil)
|
|
reg, err := c.Register(context.Background(), RegisterRequest{OrderNumber: "vmo-1-2", ReturnURL: "https://x/cb"})
|
|
if err != nil || !reg.Success() {
|
|
t.Fatalf("stub register should succeed: %+v err=%v", reg, err)
|
|
}
|
|
st, err := c.CheckStatus(context.Background(), StatusRequest{OrderId: reg.OrderId})
|
|
if err != nil {
|
|
t.Fatalf("stub status err: %v", err)
|
|
}
|
|
// Documents debug behaviour: the stub reports EVERY payment as paid.
|
|
if !st.Paid || st.OrderStatus != 2 {
|
|
t.Errorf("stub CheckStatus should report paid/status=2, got paid=%v status=%d", st.Paid, st.OrderStatus)
|
|
}
|
|
}
|
|
|
|
func TestHTTPClientRegisterParsesResponse(t *testing.T) {
|
|
var gotPath, gotAmount, gotCurrency, gotUser string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath = r.URL.Path
|
|
gotAmount = r.URL.Query().Get("amount")
|
|
gotCurrency = r.URL.Query().Get("currency")
|
|
gotUser = r.URL.Query().Get("userName")
|
|
w.Write([]byte(`{"orderId":"GB-123","formUrl":"https://pay/GB-123"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := &HTTPClient{httpClient: srv.Client(), baseURL: srv.URL + "/"}
|
|
res, err := c.Register(context.Background(), RegisterRequest{
|
|
OrderNumber: "vmo-1-2", AmountMinor: 12345, Username: "u", Password: "p", ReturnURL: "https://cb",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("register: %v", err)
|
|
}
|
|
if res.OrderId != "GB-123" || res.FormUrl != "https://pay/GB-123" || !res.Success() {
|
|
t.Fatalf("bad parse: %+v", res)
|
|
}
|
|
if gotPath != "/register.do" || gotAmount != "12345" || gotCurrency != "934" || gotUser != "u" {
|
|
t.Errorf("bad request: path=%s amount=%s currency=%s user=%s", gotPath, gotAmount, gotCurrency, gotUser)
|
|
}
|
|
}
|
|
|
|
func TestHTTPClientCheckStatusPaidMapping(t *testing.T) {
|
|
cases := []struct {
|
|
body string
|
|
paid bool
|
|
}{
|
|
{`{"orderStatus":2,"actionCode":"0"}`, true}, // 2 = deposited
|
|
{`{"orderStatus":1,"actionCode":0}`, false}, // numeric actionCode still decodes
|
|
{`{"orderStatus":6,"actionCode":"declined"}`, false},
|
|
}
|
|
for _, tc := range cases {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), "x-www-form-urlencoded") {
|
|
t.Errorf("status check must be form-encoded POST, got %q", r.Header.Get("Content-Type"))
|
|
}
|
|
w.Write([]byte(tc.body))
|
|
}))
|
|
c := &HTTPClient{httpClient: srv.Client(), baseURL: srv.URL + "/"}
|
|
st, err := c.CheckStatus(context.Background(), StatusRequest{OrderId: "x"})
|
|
srv.Close()
|
|
if err != nil {
|
|
t.Fatalf("status: %v", err)
|
|
}
|
|
if st.Paid != tc.paid {
|
|
t.Errorf("body %s: Paid=%v want %v", tc.body, st.Paid, tc.paid)
|
|
}
|
|
}
|
|
}
|