add status api
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
2026-09-22 12:42:31 +05:00
parent ac94e0dd8a
commit f2b55f9896
4 changed files with 76 additions and 99 deletions

View File

@@ -2,7 +2,6 @@ package visamasterpay
import (
"errors"
"fmt"
"net/http"
"online-back/internal/appresult"
@@ -30,6 +29,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
group := router.Group(baseURL, appresult.JwtTokenCheck())
group.GET("/:id/quote", h.quote)
group.POST("/:id/pay", h.pay)
group.GET("/:id/status", h.status)
// callback is hit by the customer's/operator's browser after GBÜS
// redirects back - it carries no Bearer token, so it's registered on
@@ -111,38 +111,64 @@ func (h *handler) pay(c *gin.Context) {
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
}
// status godoc
// @Summary Re-check a visa/master/sber order's latest payment attempt
// @Description For when the frontend can't rely on GBÜS's redirect having reached the callback (e.g. the user closed the hosted payment page first) - re-verifies the latest attempt with GBÜS server-to-server, the same way the callback does, and finalizes it if needed. Safe to call repeatedly.
// @Tags visa-master-payment-orders
// @Produce json
// @Security BearerAuth
// @Param id path int true "Visa/master payment order ID"
// @Success 200 {object} PaymentResultDTO
// @Failure 400 {object} appresult.AppError
// @Failure 404 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /visa-master-payment-orders/{id}/status [get]
func (h *handler) status(c *gin.Context) {
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
if !h.guardOrder(c, id) {
return
}
result, err := h.repository.Status(c.Request.Context(), id)
if h.handleError(c, err, "status") {
return
}
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
}
// callback godoc
// @Summary GBÜS payment callback
// @Description GBÜS redirects the browser here after payment; the real outcome is verified server-to-server before anything is finalized
// @Tags visa-master-payment-orders
// @Produce html
// @Produce json
// @Param paymentId query int true "Payment attempt ID"
// @Success 200 {string} string "html"
// @Success 200 {object} CallbackResultDTO
// @Router /visa-master-payment-orders/callback [get]
func (h *handler) callback(c *gin.Context) {
paymentID, ok := shared.ParseID(c.Query("paymentId"))
if !ok {
c.Data(http.StatusBadRequest, "text/html; charset=utf-8", []byte(callbackPage("Ýalňyşlyk", "Töleg maglumaty tapylmady.")))
c.JSON(http.StatusBadRequest, CallbackResultDTO{Paid: false, Data: "Töleg maglumaty tapylmady."})
return
}
result, err := h.repository.HandleCallback(c.Request.Context(), paymentID)
if err != nil {
h.logger.Errorf("visa master payment callback failed (paymentId=%d): %v", paymentID, err)
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(callbackPage("Ýalňyşlyk", "Tölegiň netijesini barlap bolmady. Operator bilen habarlaşyň.")))
c.JSON(http.StatusOK, CallbackResultDTO{Paid: false, Data: "Tölegiň netijesini barlap bolmady. Operator bilen habarlaşyň."})
return
}
if result.Paid {
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(callbackPage("Töleg üstünlikli", "Töleg tamamlandy. Bu penjiräni ýapyp bilersiňiz.")))
c.JSON(http.StatusOK, CallbackResultDTO{Paid: true, Data: "Töleg amala aşyryldy."})
return
}
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(callbackPage("Töleg amala aşmady", "Töleg tamamlanmady ýa-da ret edildi. Operator bilen habarlaşyň.")))
}
func callbackPage(title, message string) string {
return fmt.Sprintf(`<!DOCTYPE html><html lang="tk"><head><meta charset="utf-8"><title>%s</title></head>
<body style="font-family:sans-serif;text-align:center;padding:48px;"><h2>%s</h2><p>%s</p></body></html>`, title, title, message)
c.JSON(http.StatusOK, CallbackResultDTO{Paid: false, Data: "Töleg amala aşyrylmady."})
}
func (h *handler) guardOrder(c *gin.Context, id int64) bool {
@@ -169,7 +195,7 @@ func (h *handler) handleError(c *gin.Context, err error, action string) bool {
return false
}
switch {
case errors.Is(err, pgx.ErrNoRows):
case errors.Is(err, pgx.ErrNoRows), errors.Is(err, ErrPaymentNotFound):
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
case errors.Is(err, ErrAlreadyPaid), errors.Is(err, ErrAmountExceedsMax), errors.Is(err, ErrPaymentWindowClosed):
c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable)