gohorsejobs/backend/internal/handlers/subscription_handler.go
Tiago Yamamoto b23393bf35 feat: implement stripe subscriptions, google analytics, and user crud
- Backend:
  - Add Stripe subscription fields to companies (migration 019)
  - Implement Stripe Checkout and Webhook handlers
  - Add Metrics API (view count, recording)
  - Update Company and Job models
- Frontend:
  - Add Google Analytics component
  - Implement User CRUD in Backoffice (Dashboard)
  - Add 'Featured' badge to JobCard
- Docs: Update Roadmap and artifacts
2025-12-27 12:06:54 -03:00

63 lines
2.1 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"github.com/rede5/gohorsejobs/backend/internal/services"
)
type SubscriptionHandler struct {
Service *services.SubscriptionService
}
func NewSubscriptionHandler(service *services.SubscriptionService) *SubscriptionHandler {
return &SubscriptionHandler{Service: service}
}
// CheckoutRequest defines the request body for creating a checkout session
type CheckoutRequest struct {
PlanID string `json:"planId"`
CompanyID int `json:"companyId"`
}
// CreateCheckoutSession creates a Stripe checkout session for a subscription
// @Summary Create Checkout Session
// @Description Create a Stripe Checkout Session for subscription
// @Tags Subscription
// @Accept json
// @Produce json
// @Param request body CheckoutRequest true "Checkout Request"
// @Success 200 {object} map[string]string
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /api/v1/subscription/checkout [post]
func (h *SubscriptionHandler) CreateCheckoutSession(w http.ResponseWriter, r *http.Request) {
var req CheckoutRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// In a real app, we should validate the company belongs to the user or user is admin
// For now getting user from context (if available) or assuming middleware checked it
// Extract user email from context (set by AuthMiddleware)
userEmail := "customer@example.com" // Placeholder if auth not fully wired for email
// Try to get user claims from context if implemented
// claims, ok := r.Context().Value("user").(*utils.UserClaims) ...
url, err := h.Service.CreateCheckoutSession(req.CompanyID, req.PlanID, userEmail)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(map[string]string{"url": url})
}
// HandleWebhook handles Stripe webhooks
func (h *SubscriptionHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
// Webhook logic
w.WriteHeader(http.StatusOK)
}