Просмотр исходного кода

Update project on billing and usage customer creation

Mauricio Araujo 2 лет назад
Родитель
Сommit
2d77e96847

+ 23 - 24
api/server/handlers/billing/list.go

@@ -115,25 +115,16 @@ func (c *CheckPaymentEnabledHandler) ensureBillingSetup(ctx context.Context, pro
 		}
 
 		// Create billing customer for project and set the billing ID if it doesn't exist
-		shouldUpdateBilling, err := c.ensureStripeCustomerExists(ctx, adminUser.Email, proj)
+		err = c.ensureStripeCustomerExists(ctx, adminUser.Email, proj)
 		if err != nil {
 			return telemetry.Error(ctx, span, err, "error ensuring Stripe customer exists")
 		}
 
 		// Create usage customer for project and set the usage ID if it doesn't exist
-		shouldUpdateUsage, err := c.ensureMetronomeCustomerExists(ctx, adminUser.Email, proj)
+		err = c.ensureMetronomeCustomerExists(ctx, adminUser.Email, proj)
 		if err != nil {
 			return telemetry.Error(ctx, span, err, "error ensuring Metronome customer exists")
 		}
-
-		if !shouldUpdateBilling && !shouldUpdateUsage {
-			return nil
-		}
-
-		_, err = c.Repo().Project().UpdateProject(proj)
-		if err != nil {
-			return telemetry.Error(ctx, span, err, "error updating project")
-		}
 	}
 
 	return nil
@@ -171,17 +162,17 @@ func (c *CheckPaymentEnabledHandler) getAdminUser(ctx context.Context, projectID
 	return adminUser, nil
 }
 
-func (c *CheckPaymentEnabledHandler) ensureStripeCustomerExists(ctx context.Context, adminUserEmail string, proj *models.Project) (shouldUpdate bool, err error) {
+func (c *CheckPaymentEnabledHandler) ensureStripeCustomerExists(ctx context.Context, adminUserEmail string, proj *models.Project) (err error) {
 	ctx, span := telemetry.NewSpan(ctx, "ensure-stripe-customer-exists")
 	defer span.End()
 
-	if proj.BillingID != "" {
-		return false, nil
+	if !c.Config().BillingManager.StripeEnabled || !proj.GetFeatureFlag(models.BillingEnabled, c.Config().LaunchDarklyClient) || proj.BillingID != "" {
+		return nil
 	}
 
 	billingID, err := c.Config().BillingManager.StripeClient.CreateCustomer(ctx, adminUserEmail, proj.ID, proj.Name)
 	if err != nil {
-		return false, telemetry.Error(ctx, span, err, "error creating billing customer")
+		return telemetry.Error(ctx, span, err, "error creating billing customer")
 	}
 
 	telemetry.WithAttributes(span,
@@ -189,24 +180,26 @@ func (c *CheckPaymentEnabledHandler) ensureStripeCustomerExists(ctx context.Cont
 	)
 
 	proj.BillingID = billingID
-	return true, nil
+
+	_, err = c.Repo().Project().UpdateProject(proj)
+	if err != nil {
+		return telemetry.Error(ctx, span, err, "error updating project")
+	}
+
+	return nil
 }
 
-func (c *CheckPaymentEnabledHandler) ensureMetronomeCustomerExists(ctx context.Context, adminUserEmail string, proj *models.Project) (shouldUpdate bool, err error) {
+func (c *CheckPaymentEnabledHandler) ensureMetronomeCustomerExists(ctx context.Context, adminUserEmail string, proj *models.Project) (err error) {
 	ctx, span := telemetry.NewSpan(ctx, "ensure-metronome-customer-exists")
 	defer span.End()
 
-	if proj.UsageID != uuid.Nil {
-		return false, nil
-	}
-
 	if !c.Config().BillingManager.MetronomeEnabled || !proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) || proj.UsageID != uuid.Nil {
-		return false, nil
+		return nil
 	}
 
 	customerID, customerPlanID, err := c.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(ctx, adminUserEmail, proj.Name, proj.ID, proj.BillingID, proj.EnableSandbox)
 	if err != nil {
-		return false, telemetry.Error(ctx, span, err, "error creating Metronome customer")
+		return telemetry.Error(ctx, span, err, "error creating Metronome customer")
 	}
 
 	telemetry.WithAttributes(span,
@@ -216,5 +209,11 @@ func (c *CheckPaymentEnabledHandler) ensureMetronomeCustomerExists(ctx context.C
 
 	proj.UsageID = customerID
 	proj.UsagePlanID = customerPlanID
-	return true, nil
+
+	_, err = c.Repo().Project().UpdateProject(proj)
+	if err != nil {
+		return telemetry.Error(ctx, span, err, "error updating project")
+	}
+
+	return nil
 }

+ 3 - 0
api/server/shared/config/loader/loader.go

@@ -335,11 +335,13 @@ func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
 
 	var (
 		stripeClient     billing.StripeClient
+		stripeEnabled    bool
 		metronomeClient  billing.MetronomeClient
 		metronomeEnabled bool
 	)
 	if sc.StripeSecretKey != "" {
 		stripeClient = billing.NewStripeClient(InstanceEnvConf.ServerConf.StripeSecretKey, InstanceEnvConf.ServerConf.StripePublishableKey)
+		stripeEnabled = true
 		res.Logger.Info().Msg("Stripe configuration loaded")
 	} else {
 		res.Logger.Info().Msg("STRIPE_SECRET_KEY not set, all Stripe functionality will be disabled")
@@ -359,6 +361,7 @@ func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
 	res.Logger.Info().Msg("Creating billing manager")
 	res.BillingManager = billing.Manager{
 		StripeClient:     stripeClient,
+		StripeEnabled:    stripeEnabled,
 		MetronomeClient:  metronomeClient,
 		MetronomeEnabled: metronomeEnabled,
 	}

+ 2 - 2
dashboard/src/main/home/Home.tsx

@@ -215,7 +215,7 @@ const Home: React.FC<Props> = (props) => {
       } else {
         setHasFinishedOnboarding(true);
       }
-    } catch (error) {}
+    } catch (error) { }
   };
 
   useEffect(() => {
@@ -423,7 +423,7 @@ const Home: React.FC<Props> = (props) => {
                     >
                       connect a valid payment method
                     </Link>
-                    . Your free trial is ending in{" "}
+                    . Your free trial is ending {" "}
                     {relativeDate(plan.trial_info.ending_before, true)}.
                   </GlobalBanner>
                 )}

+ 1 - 0
internal/billing/billing.go

@@ -3,6 +3,7 @@ package billing
 // Manager contains methods for managing billing for a project
 type Manager struct {
 	StripeClient     StripeClient
+	StripeEnabled    bool
 	MetronomeClient  MetronomeClient
 	MetronomeEnabled bool
 }