package api import ( "github.com/gin-gonic/gin" "veza-backend-api/internal/core/subscription" "veza-backend-api/internal/handlers" "veza-backend-api/internal/services/hyperswitch" ) // setupSubscriptionRoutes configures routes for subscription plans management (v0.12.1). // // v1.0.9 item G — when Hyperswitch is configured, the subscription // service is built with a PaymentProvider. Without it, paid-plan // subscribe attempts fail with HTTP 503 "payment provider not // configured" (replaces the v1.0.6.2 silent fantôme creation). func (r *APIRouter) setupSubscriptionRoutes(router *gin.RouterGroup) { opts := []subscription.ServiceOption{} if r.config.HyperswitchEnabled && r.config.HyperswitchAPIKey != "" && r.config.HyperswitchURL != "" { hsClient := hyperswitch.NewClient(r.config.HyperswitchURL, r.config.HyperswitchAPIKey) hsProvider := hyperswitch.NewProvider(hsClient) opts = append(opts, subscription.WithPaymentProvider(hsProvider)) } svc := subscription.NewService(r.db.GormDB, r.logger, opts...) handler := handlers.NewSubscriptionHandler(svc, r.logger) group := router.Group("/subscriptions") // Public: list available plans group.GET("/plans", handler.ListPlans) group.GET("/plans/:id", handler.GetPlan) // Protected: user subscription management if r.config.AuthMiddleware != nil { protected := group.Group("") protected.Use(r.config.AuthMiddleware.RequireAuth()) r.applyCSRFProtection(protected) protected.GET("/me", handler.GetMySubscription) protected.POST("/subscribe", handler.Subscribe) protected.POST("/cancel", handler.CancelSubscription) protected.POST("/reactivate", handler.ReactivateSubscription) protected.PUT("/billing-cycle", handler.ChangeBillingCycle) protected.GET("/invoices", handler.GetInvoices) protected.GET("/history", handler.GetSubscriptionHistory) } }