Skip to content

Commit

Permalink
create sessions update handling stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
TeisNP committed Feb 12, 2024
1 parent e4641a3 commit 6882ebd
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions internal/rest/controllers/teams/billing.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package teams

import (
"fmt"
"net/http"

"github.com/labstack/echo/v4"
Expand All @@ -25,8 +24,6 @@ func (h *Handlers) PostCreateCheckoutSession(c hs.AuthenticatedContext) error {

return echo.ErrBadRequest
}
c.Log.Error(req.PriceLookupKey)
c.Log.Info(req.TeamID)

team, err := h.TeamService.GetByID(c.Request().Context(), req.TeamID)
if err != nil {
Expand All @@ -35,36 +32,51 @@ func (h *Handlers) PostCreateCheckoutSession(c hs.AuthenticatedContext) error {
return echo.ErrInternalServerError
}

fmt.Println(team)
if team.PaymentPlan == req.PriceLookupKey {
return c.JSON(http.StatusOK, "")
}

// teamSubscription := h.BillingService.GetCustomerSubscribtion(*team.StripeCustomerID)
if team.StripeCustomerID != nil {
if team.StripeCustomerID == nil {
if req.PriceLookupKey == "FREE" {
_, err := h.BillingService.CancelSubscribtion(team)
if err != nil {
c.Log.WithError(err).Debug("cancel subscription")

return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, "")
}
_, err := h.BillingService.UpdateSubscribtion(team, req.PriceLookupKey)
s, err := h.BillingService.CreateCheckoutSession(team, req.PriceLookupKey)
if err != nil {
c.Log.WithError(err).Debug("update subscription")
c.Log.WithError(err).Debug("create stripe checkout session")

return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, s.URL)
}

if req.PriceLookupKey == "FREE" {
_, err := h.BillingService.CancelSubscribtion(team)
if err != nil {
c.Log.WithError(err).Debug("cancel subscription")

return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, "")
}

s, err := h.BillingService.CreateCheckoutSession(team, req.PriceLookupKey)
if team.PaymentPlan == "FREE" {
s, err := h.BillingService.CreateCheckoutSession(team, req.PriceLookupKey)
if err != nil {
c.Log.WithError(err).Debug("create stripe checkout session")

return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, s.URL)
}

_, err = h.BillingService.UpdateSubscribtion(team, req.PriceLookupKey)
if err != nil {
c.Log.WithError(err).Debug("create stripe checkout session")
c.Log.WithError(err).Debug("update subscription")

return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, "")

return c.JSON(http.StatusOK, s.URL)
}

type GetCheckoutSession struct {
Expand Down

0 comments on commit 6882ebd

Please sign in to comment.