Skip to content

Commit

Permalink
Fixing loop
Browse files Browse the repository at this point in the history
  • Loading branch information
AlfieJones committed Mar 6, 2024
1 parent 9f802c0 commit 04281c8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
48 changes: 28 additions & 20 deletions apps/backend/app/billing/customer_billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ func (c *CustomerBilling) getLatestSubscription(ctx context.Context, team models
// We will only ever have 1 active subscription
if list.Next() {
return list.Subscription(), nil
}

if list.Err() != nil {
} else if list.Err() != nil {
if list.Err().(*stripe.Error).Code == stripe.ErrorCodeResourceMissing && list.Err().(*stripe.Error).Param == "customer" {
// Customer has been deleted in stripe, we should delete the customer id from the team
team.CustomerID = ""
Expand All @@ -135,22 +133,30 @@ func (c *CustomerBilling) getLatestSubscription(ctx context.Context, team models
}
}

// We might have a mismatch between the price and the subscription, we should update the subscription
if err := c.UpdateTeamPlan(ctx, team); err != nil {
return nil, err
}

list = c.API.Subscriptions.List(&stripe.SubscriptionListParams{
Customer: stripe.String(team.CustomerID),
Price: stripe.String(price),
})

if list.Err() != nil {
return nil, list.Err()
if list.Next() {
sub := list.Subscription()

if err := c.UpdateTeamPlan(ctx, team, sub); err != nil {
return nil, err
}

return sub, nil
}

if list.Next() {
return list.Subscription(), nil
team.CustomerID = ""
team.SubscriptionID = ""

db, err := database.OpenDBConnection()
if err != nil {
return nil, err
}

if err := db.UpdateTeamBilling(ctx, team); err != nil {
return nil, err
}

return nil, nil
Expand Down Expand Up @@ -183,19 +189,22 @@ func (c *CustomerBilling) CanCreateNewSubscription(team models.Team) (bool, int,
return true, 0, nil
}

func (c *CustomerBilling) UpdateTeamPlan(ctx context.Context, team models.Team) error {
func (c *CustomerBilling) UpdateTeamPlan(ctx context.Context, team models.Team, sub *stripe.Subscription) error {
price := os.Getenv("STRIPE_PRICE_ID_5000")
if team.Referrals == 1 {
price = os.Getenv("STRIPE_PRICE_ID_6250")
} else if team.Referrals == 2 {
price = os.Getenv("STRIPE_PRICE_ID_7500")
}

sub, err := c.GetCurrentSubscription(ctx, team)
if err != nil {
return err
} else if sub == nil {
return nil
var err error
if sub == nil {
sub, err = c.GetCurrentSubscription(ctx, team)
if err != nil {
return err
} else if sub == nil {
return nil
}
}

_, err = c.API.Subscriptions.Update(sub.ID, &stripe.SubscriptionParams{
Expand All @@ -221,7 +230,6 @@ func (c *CustomerBilling) GetCurrentSubscription(ctx context.Context, team model
if team.SubscriptionID == "" {
var err error
sub, err = c.getLatestSubscription(ctx, team)

if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/app/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func ReferUser(c echo.Context) error {
}

paymentClient := payments.NewPaymentClient()
if err := paymentClient.UpdateTeamPlan(c.Request().Context(), userTeam); err != nil {
if err := paymentClient.UpdateTeamPlan(c.Request().Context(), userTeam, nil); err != nil {
return err
}

if err := paymentClient.UpdateTeamPlan(c.Request().Context(), referralTeam); err != nil {
if err := paymentClient.UpdateTeamPlan(c.Request().Context(), referralTeam, nil); err != nil {
return err
}

Expand Down

0 comments on commit 04281c8

Please sign in to comment.