Initial commit
This commit is contained in:
72
internal/api/payment/handler.go
Normal file
72
internal/api/payment/handler.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"stream.api/internal/config"
|
||||
"stream.api/internal/database/model"
|
||||
"stream.api/internal/database/query"
|
||||
"stream.api/pkg/logger"
|
||||
"stream.api/pkg/response"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
logger logger.Logger
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func NewHandler(l logger.Logger, cfg *config.Config) PaymentHandler {
|
||||
return &Handler{
|
||||
logger: l,
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary Create Payment
|
||||
// @Description Create a new payment
|
||||
// @Tags payment
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body CreatePaymentRequest true "Payment Info"
|
||||
// @Success 201 {object} response.Response
|
||||
// @Failure 400 {object} response.Response
|
||||
// @Failure 401 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /payments [post]
|
||||
// @Security BearerAuth
|
||||
func (h *Handler) CreatePayment(c *gin.Context) {
|
||||
var req CreatePaymentRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
userID := c.GetString("userID")
|
||||
if userID == "" {
|
||||
response.Error(c, http.StatusUnauthorized, "Unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
// In a real scenario, we would contact Stripe/PayPal here to create a session
|
||||
// For now, we just create a "PENDING" payment record.
|
||||
|
||||
payment := &model.Payment{
|
||||
ID: uuid.New().String(),
|
||||
UserID: userID,
|
||||
PlanID: req.PlanID,
|
||||
Amount: req.Amount,
|
||||
Status: "PENDING",
|
||||
Provider: "STRIPE", // Defaulting to Stripe for this example
|
||||
}
|
||||
|
||||
p := query.Payment
|
||||
if err := p.WithContext(c.Request.Context()).Create(payment); err != nil {
|
||||
h.logger.Error("Failed to create payment", "error", err)
|
||||
response.Error(c, http.StatusInternalServerError, "Failed to create payment")
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, gin.H{"payment": payment, "message": "Payment initiated"})
|
||||
}
|
||||
14
internal/api/payment/interface.go
Normal file
14
internal/api/payment/interface.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package payment
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// PaymentHandler defines the interface for payment operations
|
||||
type PaymentHandler interface {
|
||||
CreatePayment(c *gin.Context)
|
||||
}
|
||||
|
||||
// CreatePaymentRequest defines the payload for creating a payment
|
||||
type CreatePaymentRequest struct {
|
||||
PlanID string `json:"plan_id" binding:"required"`
|
||||
Amount float64 `json:"amount" binding:"required"`
|
||||
}
|
||||
Reference in New Issue
Block a user