Files
at-container-registry/pkg/billing/handlers.go

58 lines
1.6 KiB
Go

//go:build billing
package billing
import (
"encoding/json"
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
)
// RegisterRoutes registers billing HTTP routes on the router.
// These routes handle subscription management and Stripe webhooks.
func (m *Manager) RegisterRoutes(r chi.Router) {
if !m.Enabled() {
slog.Info("Billing routes disabled (not configured)")
return
}
slog.Info("Registering billing routes")
// Stripe webhook (public, verified by Stripe signature)
r.Post("/api/stripe/webhook", m.handleStripeWebhook)
}
// handleStripeWebhook processes incoming Stripe webhook events.
func (m *Manager) handleStripeWebhook(w http.ResponseWriter, r *http.Request) {
if err := m.HandleWebhook(r); err != nil {
slog.Error("Stripe webhook error", "error", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"received": true}`)); err != nil {
slog.Error("Failed to write webhook response", "error", err)
}
}
// HandleGetSubscription is an HTTP handler that returns subscription info as JSON.
// Used by the settings page HTMX endpoint.
func (m *Manager) HandleGetSubscription(w http.ResponseWriter, r *http.Request, userDID string) {
info, err := m.GetSubscriptionInfo(userDID)
if err != nil {
w.WriteHeader(http.StatusOK)
if _, writeErr := w.Write([]byte("")); writeErr != nil {
slog.Error("Failed to write empty response", "error", writeErr)
}
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(info); err != nil {
slog.Error("Failed to encode subscription info", "error", err)
}
}