added log
This commit is contained in:
98
pkg/paymentgateway/client_test.go
Normal file
98
pkg/paymentgateway/client_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
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 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,16 +25,27 @@ const mpiBaseURL = "https://mpi.gov.tm/payment/rest/"
|
||||
// the gateway's own convention exactly rather than normalizing them.
|
||||
type HTTPClient struct {
|
||||
httpClient *http.Client
|
||||
baseURL string
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewHTTPClient(logger *logging.Logger) *HTTPClient {
|
||||
return &HTTPClient{
|
||||
httpClient: &http.Client{Timeout: 20 * time.Second},
|
||||
baseURL: mpiBaseURL,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// base returns the configured gateway base URL, falling back to the fixed
|
||||
// GBÜS endpoint when the client was constructed without one.
|
||||
func (c *HTTPClient) base() string {
|
||||
if c.baseURL == "" {
|
||||
return mpiBaseURL
|
||||
}
|
||||
return c.baseURL
|
||||
}
|
||||
|
||||
type registerResponse struct {
|
||||
OrderId string `json:"orderId"`
|
||||
FormUrl string `json:"formUrl"`
|
||||
@@ -55,7 +66,7 @@ func (c *HTTPClient) Register(ctx context.Context, req RegisterRequest) (Registe
|
||||
"description": {req.Description},
|
||||
}
|
||||
|
||||
body, err := c.do(ctx, http.MethodGet, mpiBaseURL+"register.do", query, nil)
|
||||
body, err := c.do(ctx, http.MethodGet, c.base()+"register.do", query, nil)
|
||||
if err != nil {
|
||||
return RegisterResult{}, err
|
||||
}
|
||||
@@ -110,7 +121,7 @@ func (c *HTTPClient) CheckStatus(ctx context.Context, req StatusRequest) (Status
|
||||
"password": {req.Password},
|
||||
}
|
||||
|
||||
body, err := c.do(ctx, http.MethodPost, mpiBaseURL+"getOrderStatusExtended.do", nil, form)
|
||||
body, err := c.do(ctx, http.MethodPost, c.base()+"getOrderStatusExtended.do", nil, form)
|
||||
if err != nil {
|
||||
return StatusResult{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user