Yimaru-BackEnd/internal/web_server/handlers/telebirr.go

99 lines
3.3 KiB
Go

package handlers
import (
"strconv"
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
"github.com/gofiber/fiber/v2"
)
// CreateTelebirrPaymentHandler initializes a payment session with Telebirr.
//
// @Summary Create Telebirr Payment Session
// @Description Generates a payment URL using Telebirr and returns it to the client.
// @Tags Telebirr
// @Accept json
// @Produce json
// @Param request body domain.GeneratePaymentURLRequest true "Telebirr payment request payload"
// @Success 200 {object} domain.Response
// @Failure 400 {object} domain.ErrorResponse
// @Failure 500 {object} domain.ErrorResponse
// @Router /api/v1/telebirr/payment [post]
func (h *Handler) CreateTelebirrPaymentHandler(c *fiber.Ctx) error {
var req domain.TelebirrPreOrderRequestPayload
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Error: err.Error(),
Message: "Invalid request payload",
})
}
totalAmount, err := strconv.ParseFloat(req.BizContent.TotalAmount, 32)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Error: err.Error(),
Message: "TotalAmount must be a valid number",
})
}
userID, ok := c.Locals("user_id").(int64)
if !ok {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Error: "Invalid user_id type",
Message: "user_id must be an int64",
})
}
paymentURL, err := h.telebirrSvc.CreateTelebirrOrder(req.BizContent.Title, float32(totalAmount), userID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Error: err.Error(),
Message: "Failed to create Telebirr payment session",
})
}
return c.Status(fiber.StatusOK).JSON(domain.Response{
Message: "Telebirr payment URL generated successfully",
Data: paymentURL,
Success: true,
StatusCode: fiber.StatusOK,
})
}
// HandleTelebirrCallbackHandler handles the Telebirr payment callback.
//
// @Summary Handle Telebirr Payment Callback
// @Description Processes the Telebirr payment result and updates wallet balance.
// @Tags Telebirr
// @Accept json
// @Produce json
// @Param payload body domain.TelebirrPaymentCallbackPayload true "Callback payload from Telebirr"
// @Success 200 {object} domain.Response
// @Failure 400 {object} domain.ErrorResponse
// @Failure 500 {object} domain.ErrorResponse
// @Router /api/v1/telebirr/callback [post]
func (h *Handler) HandleTelebirrCallback(c *fiber.Ctx) error {
var payload domain.TelebirrPaymentCallbackPayload
if err := c.BodyParser(&payload); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
Error: err.Error(),
Message: "Invalid callback payload",
})
}
ctx := c.Context()
err := h.telebirrSvc.HandleTelebirrPaymentCallback(ctx, &payload)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
Error: err.Error(),
Message: "Failed to handle Telebirr payment callback",
})
}
return c.Status(fiber.StatusOK).JSON(domain.Response{
Message: "Telebirr payment processed successfully",
Data: nil,
Success: true,
StatusCode: fiber.StatusOK,
})
}