package visamasterpay import ( "errors" "net/http" "online-back/internal/appresult" "online-back/internal/handlers" "online-back/internal/identity/rbac" "online-back/internal/shared" "online-back/pkg/logging" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v4" ) const baseURL = "/visa-master-payment-orders" type handler struct { logger *logging.Logger repository Repository } func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler { return &handler{logger: logger, repository: repository} } 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) } // quote godoc // @Summary Quote a visa/master/sber payment // @Description Returns the current rate, configured fees, and max payable amount for an order's payout // @Tags visa-master-payment-orders // @Produce json // @Security BearerAuth // @Param id path int true "Visa/master payment order ID" // @Success 200 {object} PaymentQuoteDTO // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /visa-master-payment-orders/{id}/quote [get] func (h *handler) quote(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.Quote(c.Request.Context(), id) if h.handleQuoteOrPayError(c, err, "quote") { return } c.JSON(http.StatusOK, appresult.SuccessWithData(result)) } // pay godoc // @Summary Pay out a visa/master/sber order // @Description Charges the payment gateway and marks the order paid on success // @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 // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /visa-master-payment-orders/{id}/pay [post] func (h *handler) pay(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 } var dto PayOrderDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } result, err := h.repository.Pay(c.Request.Context(), id, dto) if h.handleQuoteOrPayError(c, err, "pay") { return } c.JSON(http.StatusOK, appresult.SuccessWithData(result)) } func (h *handler) guardOrder(c *gin.Context, id int64) bool { if rbac.SkipRecordGuard(c) { return true } userID, branchID, err := h.repository.OwnerInfo(c.Request.Context(), id) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return false } if err != nil { c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return false } 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 { if err == nil { return false } switch { case errors.Is(err, pgx.ErrNoRows): c.JSON(http.StatusNotFound, appresult.ErrNotFound) case errors.Is(err, ErrAlreadyPaid), errors.Is(err, ErrAmountNotSet), errors.Is(err, ErrAmountExceedsMax): c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable) default: h.logger.Errorf("%s visa master payment order failed: %v", action, err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) } return true }