164 lines
4.8 KiB
Go
164 lines
4.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// InitiateDeposit godoc
|
|
// @Summary Initiate a deposit
|
|
// @Description Starts a new deposit process using Chapa payment gateway
|
|
// @Tags Chapa
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body domain.ChapaDepositRequestPayload true "Deposit request"
|
|
// @Success 200 {object} domain.ChapaDepositResponse
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/chapa/payments/deposit [post]
|
|
func (h *Handler) InitiateDeposit(c *fiber.Ctx) error {
|
|
// Get user ID from context (set by your auth middleware)
|
|
userID, ok := c.Locals("user_id").(int64)
|
|
if !ok {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Error: "invalid user ID",
|
|
})
|
|
}
|
|
|
|
var req domain.ChapaDepositRequestPayload
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
fmt.Sprintln("We first first are here init Chapa payment")
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
amount := domain.Currency(req.Amount * 100)
|
|
|
|
fmt.Sprintln("We are here init Chapa payment")
|
|
|
|
checkoutURL, err := h.chapaSvc.InitiateDeposit(c.Context(), userID, amount)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Error: err.Error(),
|
|
Message: checkoutURL,
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(domain.ChapaDepositResponse{
|
|
CheckoutURL: checkoutURL,
|
|
})
|
|
}
|
|
|
|
// WebhookCallback godoc
|
|
// @Summary Chapa payment webhook callback (used by Chapa)
|
|
// @Description Handles payment notifications from Chapa
|
|
// @Tags Chapa
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body domain.ChapaWebhookPayload true "Webhook payload"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/chapa/payments/webhook/verify [post]
|
|
func (h *Handler) WebhookCallback(c *fiber.Ctx) error {
|
|
// Verify webhook signature first
|
|
// signature := c.Get("Chapa-Signature")
|
|
// if !verifySignature(signature, c.Body()) {
|
|
// return c.Status(fiber.StatusUnauthorized).JSON(ErrorResponse{
|
|
// Error: "invalid signature",
|
|
// })
|
|
// }
|
|
|
|
var payload struct {
|
|
TxRef string `json:"tx_ref"`
|
|
Amount float64 `json:"amount"`
|
|
Currency string `json:"currency"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
if err := c.BodyParser(&payload); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := h.chapaSvc.VerifyDeposit(c.Context(), payload.TxRef); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
|
StatusCode: 200,
|
|
Message: "payment verified successfully",
|
|
Data: payload.TxRef,
|
|
Success: true,
|
|
})
|
|
}
|
|
|
|
// VerifyPayment godoc
|
|
// @Summary Verify a payment manually
|
|
// @Description Manually verify a payment using Chapa's API
|
|
// @Tags Chapa
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param tx_ref path string true "Transaction Reference"
|
|
// @Success 200 {object} domain.ChapaVerificationResponse
|
|
// @Failure 400 {object} domain.ErrorResponse
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /api/v1/chapa/payments/manual/verify/{tx_ref} [get]
|
|
func (h *Handler) ManualVerifyPayment(c *fiber.Ctx) error {
|
|
txRef := c.Params("tx_ref")
|
|
if txRef == "" {
|
|
return c.Status(fiber.StatusBadRequest).JSON(domain.ErrorResponse{
|
|
Message: "Failed to verify Chapa transaction",
|
|
Error: "Transaction reference is required",
|
|
})
|
|
}
|
|
|
|
verification, err := h.chapaSvc.ManualVerifyPayment(c.Context(), txRef)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Message: "Failed to verify Chapa transaction",
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(domain.ChapaVerificationResponse{
|
|
Status: string(verification.Status),
|
|
Amount: verification.Amount,
|
|
Currency: verification.Currency,
|
|
TxRef: txRef,
|
|
})
|
|
}
|
|
|
|
// GetSupportedBanks godoc
|
|
// @Summary Get supported banks
|
|
// @Description Get list of banks supported by Chapa
|
|
// @Tags Chapa
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {array} domain.Bank
|
|
// @Failure 500 {object} domain.ErrorResponse
|
|
// @Router /banks [get]
|
|
func (h *Handler) GetSupportedBanks(c *fiber.Ctx) error {
|
|
banks, err := h.chapaSvc.GetSupportedBanks(c.Context())
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(domain.ErrorResponse{
|
|
Error: err.Error(),
|
|
Message: "Failed to fetch banks",
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(domain.Response{
|
|
Message: "Banks fetched successfully",
|
|
StatusCode: 200,
|
|
Success: true,
|
|
Data: banks,
|
|
})
|
|
}
|