Skip to content

Commit

Permalink
cancel subscribtion
Browse files Browse the repository at this point in the history
  • Loading branch information
TeisNP committed Feb 4, 2024
1 parent d3e61a0 commit 83823e7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
24 changes: 24 additions & 0 deletions internal/billing/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Service interface {
PostConfig() StripeConfig
CreateCheckoutSession(team *entities.Team, priceLookupKey string) (*stripe.CheckoutSession, error)
UpdateSubscribtion(team *entities.Team, priceLookupKey string) (*stripe.Subscription, error)
CancelSubscribtion(team *entities.Team) (*stripe.Subscription, error)
GetCheckoutSession(sessionID string) (*stripe.CheckoutSession, error)
GetLineItems(sessionID string) *session.LineItemIter
GetCustomerSubscribtion(customerID string) *subscription.Iter
Expand Down Expand Up @@ -103,6 +104,29 @@ func (s *ServiceImpl) UpdateSubscribtion(team *entities.Team, priceLookupKey str
return result, nil
}

func (s *ServiceImpl) CancelSubscribtion(team *entities.Team) (*stripe.Subscription, error) {
// Set Customer on session if already a customer

sub := s.GetCustomerSubscribtion(*team.StripeCustomerID)
sub.Next()
teamSubscription := sub.Subscription()

params := &stripe.SubscriptionParams{
Items: []*stripe.SubscriptionItemsParams{
{
ID: stripe.String(teamSubscription.Items.Data[0].ID),
Deleted: stripe.Bool(true),
},
},
}
result, err := subscription.Update(teamSubscription.ID, params)
if err != nil {
return nil, errors.Wrap(err, "failed to update subscription")
}

return result, nil
}

func (s *ServiceImpl) GetCheckoutSession(sessionID string) (*stripe.CheckoutSession, error) {
return session.Get(sessionID, &stripe.CheckoutSessionParams{})
}
Expand Down
12 changes: 9 additions & 3 deletions internal/rest/controllers/teams/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ func (h *Handlers) PostCreateCheckoutSession(c hs.AuthenticatedContext) error {

teamSubscription := h.BillingService.GetCustomerSubscribtion(*team.StripeCustomerID)
if teamSubscription != nil {
// if req.PriceLookupKey == "" {
// TODO CANCEL SUBSCRIPTION
// }
if req.PriceLookupKey == "" {
_, 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)
if err != nil {
c.Log.WithError(err).Debug("update subscription")
Expand Down

0 comments on commit 83823e7

Please sign in to comment.