payment-history: GET /{id}/gateway - all of an order's payments with their HALKBANK JSON
The gateway view now takes an order ID and returns every payment of the
order, newest first, each in the same PaymentGatewayDTO shape with its
own last stored GBÜS response. It moves from /payments/{id}/gateway to
/{id}/gateway, next to the other order-scoped routes (/{id}, /{id}/logs).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ef3a1baee2
commit
a3cbb52f4b
7 files changed
+182
-118
No files matched your search
@@ -12,20 +12,15 @@ import (
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type fakeRepository struct {
|
||||
Repository
|
||||
payments map[int64]PaymentHistoryDetailDTO
|
||||
orders map[int64][]PaymentHistoryDetailDTO
|
||||
}
|
||||
|
||||
func (f fakeRepository) Get(_ context.Context, paymentID int64) (PaymentHistoryDetailDTO, error) {
|
||||
p, ok := f.payments[paymentID]
|
||||
if !ok {
|
||||
return PaymentHistoryDetailDTO{}, pgx.ErrNoRows
|
||||
}
|
||||
return p, nil
|
||||
func (f fakeRepository) OrderPaymentDetails(_ context.Context, orderID int64) ([]PaymentHistoryDetailDTO, error) {
|
||||
return append([]PaymentHistoryDetailDTO{}, f.orders[orderID]...), nil
|
||||
}
|
||||
|
||||
func strPtr(s string) *string { return &s }
|
||||
@@ -40,14 +35,14 @@ func gatewayRequest(t *testing.T, repo Repository, path string) *httptest.Respon
|
||||
router.Use(func(c *gin.Context) {
|
||||
c.Set("rbacPrincipal", &rbac.Principal{UserID: 1, Scope: rbac.ScopeAll})
|
||||
})
|
||||
router.GET("/payment-history/payments/:id/gateway", h.gateway)
|
||||
router.GET("/payment-history/:id/gateway", h.gateway)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/payment-history/payments/"+path, nil))
|
||||
router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/payment-history/"+path, nil))
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestGatewayReturnsTableRowWithHalkbankJSON(t *testing.T) {
|
||||
func TestGatewayReturnsOrderPaymentsWithHalkbankJSON(t *testing.T) {
|
||||
created := time.Date(2026, 10, 22, 8, 19, 57, 0, time.UTC)
|
||||
paid := PaymentHistoryDetailDTO{
|
||||
PaymentHistoryItemDTO: PaymentHistoryItemDTO{
|
||||
@@ -59,57 +54,80 @@ func TestGatewayReturnsTableRowWithHalkbankJSON(t *testing.T) {
|
||||
},
|
||||
GatewayResponse: json.RawMessage(`{"orderStatus":2,"actionCode":0}`),
|
||||
}
|
||||
repo := fakeRepository{payments: map[int64]PaymentHistoryDetailDTO{2922: paid}}
|
||||
failed := PaymentHistoryDetailDTO{PaymentHistoryItemDTO: PaymentHistoryItemDTO{Id: 2921, OrderId: 9437, GatewayStatus: "failed"}}
|
||||
repo := fakeRepository{orders: map[int64][]PaymentHistoryDetailDTO{9437: {paid, failed}}}
|
||||
|
||||
rec := gatewayRequest(t, repo, "2922/gateway")
|
||||
rec := gatewayRequest(t, repo, "9437/gateway")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("got %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Data map[string]json.RawMessage `json:"data"`
|
||||
Data []map[string]json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(body.Data) != 2 {
|
||||
t.Fatalf("got %d payments, want 2", len(body.Data))
|
||||
}
|
||||
if got := string(body.Data[1]["id"]); got != "2921" {
|
||||
t.Errorf("second payment id: got %s, want 2921", got)
|
||||
}
|
||||
want := map[string]string{
|
||||
"id": `2922`, "orderId": `9437`, "cardholderName": `"Amanowa guljemal"`, "maskedPan": `"993404**2453"`,
|
||||
"paymentYear": `2026`, "paymentMonth": `10`, "createdAt": `"2026-10-22T08:19:57Z"`,
|
||||
"amountUsd": `250`, "amountTmt": `1063.44`, "orderNumber": `"221026131957"`,
|
||||
"paid": `true`, "gatewayStatus": `"success"`, "gatewayResponse": `{"orderStatus":2,"actionCode":0}`,
|
||||
}
|
||||
if len(body.Data) != len(want) {
|
||||
t.Errorf("got fields %v, want exactly %d", body.Data, len(want))
|
||||
if len(body.Data[0]) != len(want) {
|
||||
t.Errorf("got fields %v, want exactly %d", body.Data[0], len(want))
|
||||
}
|
||||
for key, value := range want {
|
||||
if got := string(body.Data[key]); got != value {
|
||||
if got := string(body.Data[0][key]); got != value {
|
||||
t.Errorf("%s: got %s, want %s", key, got, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayNullsWhenHalkbankNeverAnswered(t *testing.T) {
|
||||
repo := fakeRepository{payments: map[int64]PaymentHistoryDetailDTO{
|
||||
7: {PaymentHistoryItemDTO: PaymentHistoryItemDTO{Id: 7, GatewayStatus: "failed"}},
|
||||
repo := fakeRepository{orders: map[int64][]PaymentHistoryDetailDTO{
|
||||
3: {{PaymentHistoryItemDTO: PaymentHistoryItemDTO{Id: 7, OrderId: 3, GatewayStatus: "failed"}}},
|
||||
}}
|
||||
rec := gatewayRequest(t, repo, "7/gateway")
|
||||
rec := gatewayRequest(t, repo, "3/gateway")
|
||||
var body struct {
|
||||
Data map[string]json.RawMessage `json:"data"`
|
||||
Data []map[string]json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(body.Data) != 1 {
|
||||
t.Fatalf("got %d payments, want 1", len(body.Data))
|
||||
}
|
||||
for _, key := range []string{"cardholderName", "maskedPan", "gatewayResponse"} {
|
||||
if got := string(body.Data[key]); got != "null" {
|
||||
if got := string(body.Data[0][key]); got != "null" {
|
||||
t.Errorf("%s: got %q, want null", key, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGatewayErrors(t *testing.T) {
|
||||
repo := fakeRepository{payments: map[int64]PaymentHistoryDetailDTO{}}
|
||||
for path, want := range map[string]int{"abc/gateway": http.StatusBadRequest, "999/gateway": http.StatusNotFound} {
|
||||
if rec := gatewayRequest(t, repo, path); rec.Code != want {
|
||||
t.Errorf("%s: got %d, want %d", path, rec.Code, want)
|
||||
}
|
||||
func TestGatewayEmptyOrderAndBadID(t *testing.T) {
|
||||
repo := fakeRepository{orders: map[int64][]PaymentHistoryDetailDTO{}}
|
||||
|
||||
rec := gatewayRequest(t, repo, "999/gateway")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("got %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := string(body.Data); got != "[]" {
|
||||
t.Errorf("data: got %s, want []", got)
|
||||
}
|
||||
|
||||
if rec := gatewayRequest(t, repo, "abc/gateway"); rec.Code != http.StatusBadRequest {
|
||||
t.Errorf("abc/gateway: got %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user