package handlers import ( "Yimaru-Backend/internal/domain" "Yimaru-Backend/internal/services/chapa" "errors" "fmt" "github.com/gofiber/fiber/v2" ) type initiateChapaDirectPaymentReq struct { PlanID int64 `json:"plan_id" validate:"required"` Phone string `json:"phone" validate:"required"` Email string `json:"email" validate:"required,email"` PaymentMethod string `json:"payment_method" validate:"required"` } type authorizeChapaDirectPaymentReq struct { SessionID string `json:"session_id" validate:"required"` RequestID string `json:"request_id" validate:"required"` OTP string `json:"otp" validate:"required"` } // InitiateChapaDirectPayment godoc // @Summary Initiate Chapa direct charge subscription payment // @Description Starts a Chapa Direct Charge (USSD push or OTP) for a subscription plan without hosted checkout redirect. // @Tags payments // @Accept json // @Produce json // @Param body body initiateChapaDirectPaymentReq true "Chapa direct charge request" // @Success 200 {object} domain.Response // @Failure 400 {object} domain.ErrorResponse // @Failure 500 {object} domain.ErrorResponse // @Router /api/v1/payments/chapa/direct [post] func (h *Handler) InitiateChapaDirectPayment(c *fiber.Ctx) error { userID, ok := c.Locals("user_id").(int64) if !ok { return c.Status(fiber.StatusUnauthorized).JSON(domain.ErrorResponse{Message: "Unauthorized"}) } var req initiateChapaDirectPaymentReq if err := c.BodyParser(&req); err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid request body", Error: err.Error(), }) } result, err := h.chapaSvc.InitiateDirectChargeSubscription(c.Context(), userID, domain.InitiateChapaDirectChargeRequest{ PlanID: req.PlanID, Phone: req.Phone, Email: req.Email, PaymentMethod: req.PaymentMethod, }) if err != nil { return c.Status(chapaDirectPaymentStatus(err)).JSON(domain.ErrorResponse{ Message: "Failed to initiate Chapa direct payment", Error: err.Error(), }) } paymentID := result.PaymentID h.recordActivity(c, domain.ActionPaymentInitiated, domain.ResourcePayment, &paymentID, fmt.Sprintf("Initiated Chapa direct payment ID: %d", paymentID), nil) return c.JSON(domain.Response{ Message: "Chapa direct payment initiated successfully", Data: result, }) } // AuthorizeChapaDirectPayment godoc // @Summary Authorize Chapa direct charge with OTP // @Description Completes OTP authorization for Chapa direct charge methods that require it. // @Tags payments // @Accept json // @Produce json // @Param body body authorizeChapaDirectPaymentReq true "Chapa direct charge authorization request" // @Success 200 {object} domain.Response // @Failure 400 {object} domain.ErrorResponse // @Failure 404 {object} domain.ErrorResponse // @Router /api/v1/payments/chapa/direct/authorize [post] func (h *Handler) AuthorizeChapaDirectPayment(c *fiber.Ctx) error { userID, ok := c.Locals("user_id").(int64) if !ok { return c.Status(fiber.StatusUnauthorized).JSON(domain.ErrorResponse{Message: "Unauthorized"}) } var req authorizeChapaDirectPaymentReq if err := c.BodyParser(&req); err != nil { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: "Invalid request body", Error: err.Error(), }) } result, err := h.chapaSvc.AuthorizeDirectChargeSubscription(c.Context(), userID, domain.AuthorizeChapaDirectChargeRequest{ SessionID: req.SessionID, RequestID: req.RequestID, OTP: req.OTP, }) if err != nil { status := fiber.StatusInternalServerError if errors.Is(err, chapa.ErrPaymentNotFound) { status = fiber.StatusNotFound } if err.Error() == "unauthorized" { status = fiber.StatusForbidden } return c.Status(status).JSON(domain.ErrorResponse{ Message: "Failed to authorize Chapa direct payment", Error: err.Error(), }) } if !result.Success { return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{ Message: result.Message, }) } var paymentID *int64 if result.PaymentID > 0 { paymentID = &result.PaymentID } h.recordActivity(c, domain.ActionPaymentVerified, domain.ResourcePayment, paymentID, "Chapa direct payment authorized", nil) return c.JSON(domain.Response{ Message: result.Message, Data: result, }) } // GetChapaDirectPaymentMethods godoc // @Summary Get Chapa direct charge payment methods // @Description Returns wallet methods supported by Chapa Direct Charge API (USSD push and OTP flows). // @Tags payments // @Produce json // @Success 200 {object} domain.Response // @Router /api/v1/payments/chapa/direct/methods [get] func (h *Handler) GetChapaDirectPaymentMethods(c *fiber.Ctx) error { return c.JSON(domain.Response{ Message: "Chapa direct payment methods retrieved successfully", Data: h.chapaSvc.GetDirectChargeMethods(), }) } func chapaDirectPaymentStatus(err error) int { switch { case errors.Is(err, chapa.ErrChapaNotConfigured): return fiber.StatusServiceUnavailable case errors.Is(err, chapa.ErrInvalidChapaDirectChargeMethod): return fiber.StatusBadRequest case err.Error() == "user already has an active subscription": return fiber.StatusConflict case err.Error() == "subscription plan is not active", err.Error() == "phone is required for direct charge": return fiber.StatusBadRequest default: return fiber.StatusInternalServerError } }