add pay
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-19 03:54:40 +05:00
parent 848a07324f
commit 6c7c402390
16 changed files with 891 additions and 324 deletions

View File

@@ -2,6 +2,7 @@ package visamasterpay
import (
"errors"
"fmt"
"net/http"
"online-back/internal/appresult"
@@ -29,6 +30,12 @@ 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)
// 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
// its own group with no JwtTokenCheck. See rbac.isPublicAdminPath,
// which must also list this path.
router.Group(baseURL).GET("/callback", h.callback)
}
// quote godoc
@@ -55,7 +62,7 @@ func (h *handler) quote(c *gin.Context) {
}
result, err := h.repository.Quote(c.Request.Context(), id)
if h.handleQuoteOrPayError(c, err, "quote") {
if h.handleError(c, err, "quote") {
return
}
@@ -63,15 +70,15 @@ func (h *handler) quote(c *gin.Context) {
}
// pay godoc
// @Summary Pay out a visa/master/sber order
// @Description Charges the payment gateway and marks the order paid on success
// @Summary Start a visa/master/sber payout
// @Description Registers the payment with GBÜS and returns the hosted-payment-page URL to complete it on. The final outcome is only known once GBÜS redirects back to the callback, which verifies and finalizes it.
// @Tags visa-master-payment-orders
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "Visa/master payment order ID"
// @Param body body PayOrderDTO true "Payment payload"
// @Success 200 {object} PaymentResultDTO
// @Success 200 {object} PaymentInitResultDTO
// @Failure 400 {object} appresult.AppError
// @Failure 404 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
@@ -92,15 +99,52 @@ func (h *handler) pay(c *gin.Context) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
if userID, ok := rbac.CurrentUserID(c); ok {
dto.CreatedBy = &userID
}
result, err := h.repository.Pay(c.Request.Context(), id, dto)
if h.handleQuoteOrPayError(c, err, "pay") {
if h.handleError(c, err, "pay") {
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
// @Param paymentId query int true "Payment attempt ID"
// @Success 200 {string} string "html"
// @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.")))
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ň.")))
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.")))
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)
}
func (h *handler) guardOrder(c *gin.Context, id int64) bool {
if rbac.SkipRecordGuard(c) {
return true
@@ -117,10 +161,10 @@ func (h *handler) guardOrder(c *gin.Context, id int64) bool {
return rbac.GuardOrder(c, userID, branchID)
}
// handleQuoteOrPayError writes the appropriate response for a Quote/Pay
// error and reports whether the caller should stop (true) or continue with
// a successful result (false).
func (h *handler) handleQuoteOrPayError(c *gin.Context, err error, action string) bool {
// handleError writes the appropriate response for a Quote/Pay error and
// reports whether the caller should stop (true) or continue with a
// successful result (false).
func (h *handler) handleError(c *gin.Context, err error, action string) bool {
if err == nil {
return false
}