From 6dd611091483c3b42a64c11d62f207ea2689fc64 Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Sat, 24 Oct 2026 20:10:07 +0500 Subject: [PATCH] payment-history: bring back GET /payments/{id}/gateway for the frontend The frontend still calls the single-payment gateway view, so it's back as it was (payment ID -> one PaymentGatewayDTO), alongside the new order-wide /{id}/gateway. Co-Authored-By: Claude Opus 5.5 --- docs/docs.go | 58 +++++++++++++++++++ docs/swagger.json | 58 +++++++++++++++++++ docs/swagger.yaml | 42 ++++++++++++++ internal/payment/paymenthistory/handler.go | 42 ++++++++++++-- .../payment/paymenthistory/handler_test.go | 44 +++++++++++++- 5 files changed, 238 insertions(+), 6 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 79b54cf..9813233 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -5430,6 +5430,64 @@ const docTemplate = `{ } } }, + "/payment-history/payments/{id}/gateway": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Returns one payment the way the per-order payment table shows it - paid or not, the card it was paid from (cardholderName/maskedPan, null until GBÜS reports them), its time (createdAt), payment year/month, amounts and reference (orderNumber) - together with the last GBÜS (HALKBANK) response stored for it (gatewayResponse, null if GBÜS never answered). Every payment of an order at once is at /payment-history/{id}/gateway.", + "produces": [ + "application/json" + ], + "tags": [ + "payment-history" + ], + "summary": "Get a payment with its HALKBANK response", + "parameters": [ + { + "type": "integer", + "description": "Payment ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/paymenthistory.PaymentGatewayDTO" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "403": { + "description": "Forbidden", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + } + } + } + }, "/payment-history/{id}": { "get": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index b4fb896..3d130eb 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5424,6 +5424,64 @@ } } }, + "/payment-history/payments/{id}/gateway": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "Returns one payment the way the per-order payment table shows it - paid or not, the card it was paid from (cardholderName/maskedPan, null until GBÜS reports them), its time (createdAt), payment year/month, amounts and reference (orderNumber) - together with the last GBÜS (HALKBANK) response stored for it (gatewayResponse, null if GBÜS never answered). Every payment of an order at once is at /payment-history/{id}/gateway.", + "produces": [ + "application/json" + ], + "tags": [ + "payment-history" + ], + "summary": "Get a payment with its HALKBANK response", + "parameters": [ + { + "type": "integer", + "description": "Payment ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/paymenthistory.PaymentGatewayDTO" + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "403": { + "description": "Forbidden", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/appresult.AppError" + } + } + } + } + }, "/payment-history/{id}": { "get": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index e9fa8a2..4904340 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -5797,6 +5797,48 @@ paths: summary: Get a single payment attempt tags: - payment-history + /payment-history/payments/{id}/gateway: + get: + description: Returns one payment the way the per-order payment table shows it + - paid or not, the card it was paid from (cardholderName/maskedPan, null until + GBÜS reports them), its time (createdAt), payment year/month, amounts and + reference (orderNumber) - together with the last GBÜS (HALKBANK) response + stored for it (gatewayResponse, null if GBÜS never answered). Every payment + of an order at once is at /payment-history/{id}/gateway. + parameters: + - description: Payment ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/paymenthistory.PaymentGatewayDTO' + "400": + description: Bad Request + schema: + $ref: '#/definitions/appresult.AppError' + "403": + description: Forbidden + schema: + $ref: '#/definitions/appresult.AppError' + "404": + description: Not Found + schema: + $ref: '#/definitions/appresult.AppError' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/appresult.AppError' + security: + - BearerAuth: [] + summary: Get a payment with its HALKBANK response + tags: + - payment-history /payment-settings: get: description: Returns the admin-configurable bank and GBÜS commission fees used diff --git a/internal/payment/paymenthistory/handler.go b/internal/payment/paymenthistory/handler.go index 758c8b0..e0cfbb2 100644 --- a/internal/payment/paymenthistory/handler.go +++ b/internal/payment/paymenthistory/handler.go @@ -31,6 +31,7 @@ func (h *handler) Register(router *gin.RouterGroup) { group := rbac.Group(router, baseURL, rbac.RequireStaff(perm.Cards)) group.GET("", h.list) group.GET("/payments/:id", h.get) + group.GET("/payments/:id/gateway", h.paymentGateway) group.GET("/:id/gateway", h.gateway) group.GET("/:id", h.orderPayments) group.GET("/:id/logs", h.logs) @@ -86,27 +87,58 @@ func (h *handler) list(c *gin.Context) { // @Failure 500 {object} appresult.AppError // @Router /payment-history/payments/{id} [get] func (h *handler) get(c *gin.Context) { + result, ok := h.loadPayment(c) + if !ok { + return + } + c.JSON(http.StatusOK, appresult.SuccessWithData(result)) +} + +// paymentGateway godoc +// @Summary Get a payment with its HALKBANK response +// @Description Returns one payment the way the per-order payment table shows it - paid or not, the card it was paid from (cardholderName/maskedPan, null until GBÜS reports them), its time (createdAt), payment year/month, amounts and reference (orderNumber) - together with the last GBÜS (HALKBANK) response stored for it (gatewayResponse, null if GBÜS never answered). Every payment of an order at once is at /payment-history/{id}/gateway. +// @Tags payment-history +// @Produce json +// @Security BearerAuth +// @Param id path int true "Payment ID" +// @Success 200 {object} PaymentGatewayDTO +// @Failure 400 {object} appresult.AppError +// @Failure 403 {object} appresult.AppError +// @Failure 404 {object} appresult.AppError +// @Failure 500 {object} appresult.AppError +// @Router /payment-history/payments/{id}/gateway [get] +func (h *handler) paymentGateway(c *gin.Context) { + result, ok := h.loadPayment(c) + if !ok { + return + } + c.JSON(http.StatusOK, appresult.SuccessWithData(NewPaymentGatewayDTO(result))) +} + +// loadPayment loads the :id payment and runs the order guard on it, +// answering the request itself when that fails. +func (h *handler) loadPayment(c *gin.Context) (PaymentHistoryDetailDTO, bool) { paymentID, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) - return + return PaymentHistoryDetailDTO{}, false } result, err := h.repository.Get(c.Request.Context(), paymentID) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) - return + return PaymentHistoryDetailDTO{}, false } if err != nil { h.logger.Errorf("get payment history failed (payment %d): %v", paymentID, err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) - return + return PaymentHistoryDetailDTO{}, false } if !h.guardOrder(c, result.OrderId) { - return + return PaymentHistoryDetailDTO{}, false } - c.JSON(http.StatusOK, appresult.SuccessWithData(result)) + return result, true } // gateway godoc diff --git a/internal/payment/paymenthistory/handler_test.go b/internal/payment/paymenthistory/handler_test.go index 9572f48..5ae9929 100644 --- a/internal/payment/paymenthistory/handler_test.go +++ b/internal/payment/paymenthistory/handler_test.go @@ -12,11 +12,21 @@ import ( "online-back/pkg/logging" "github.com/gin-gonic/gin" + "github.com/jackc/pgx/v4" ) type fakeRepository struct { Repository - orders map[int64][]PaymentHistoryDetailDTO + orders map[int64][]PaymentHistoryDetailDTO + payments 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) { @@ -35,6 +45,7 @@ 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.paymentGateway) router.GET("/payment-history/:id/gateway", h.gateway) rec := httptest.NewRecorder() @@ -131,3 +142,34 @@ func TestGatewayEmptyOrderAndBadID(t *testing.T) { t.Errorf("abc/gateway: got %d, want %d", rec.Code, http.StatusBadRequest) } } + +func TestPaymentGatewayReturnsOnePayment(t *testing.T) { + repo := fakeRepository{payments: map[int64]PaymentHistoryDetailDTO{ + 2922: { + PaymentHistoryItemDTO: PaymentHistoryItemDTO{Id: 2922, OrderId: 9437, Paid: true, GatewayStatus: "success"}, + GatewayResponse: json.RawMessage(`{"orderStatus":2}`), + }, + }} + + rec := gatewayRequest(t, repo, "payments/2922/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"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatal(err) + } + for key, value := range map[string]string{"id": `2922`, "orderId": `9437`, "gatewayResponse": `{"orderStatus":2}`} { + if got := string(body.Data[key]); got != value { + t.Errorf("%s: got %s, want %s", key, got, value) + } + } + + for path, want := range map[string]int{"payments/abc/gateway": http.StatusBadRequest, "payments/999/gateway": http.StatusNotFound} { + if rec := gatewayRequest(t, repo, path); rec.Code != want { + t.Errorf("%s: got %d, want %d", path, rec.Code, want) + } + } +}