diff --git a/internal/rest/controllers/webhooks/stripe.go b/internal/rest/controllers/webhooks/stripe.go index 7428c08..8541d89 100644 --- a/internal/rest/controllers/webhooks/stripe.go +++ b/internal/rest/controllers/webhooks/stripe.go @@ -96,6 +96,31 @@ func (h *Handlers) handleWebhook(c hs.StripeContext) error { return c.NoContent(http.StatusInternalServerError) } + case "customer.subscription.deleted": + var subscription stripe.Subscription + err := json.Unmarshal(event.Data.Raw, &subscription) + if err != nil { + c.Log.WithError(err).Debug("Error parsing webhook JSON") + return echo.ErrBadRequest + } + + team, err := h.TeamService.GetByStripeID(c.Request().Context(), subscription.Customer.ID) + if err != nil { + c.Log.WithError(err).Debug("Error getting team by stripe id") + return c.NoContent(http.StatusInternalServerError) + } + team.PaymentPlan = subscription.Items.Data[0].Price.LookupKey + + if subscription.Status == "canceled" { + team.PaymentPlan = "FREE" + } + + err = h.TeamService.UpdateTeam(c.Request().Context(), team) + if err != nil { + c.Log.WithError(err).Debug("Error updating team") + return c.NoContent(http.StatusInternalServerError) + } + case "customer.deleted": var customer stripe.Customer err := json.Unmarshal(event.Data.Raw, &customer)