Преглед изворни кода

Add user email instead or orgname, set project name as customer name

Mauricio Araujo пре 2 година
родитељ
комит
d299be2601

+ 1 - 1
api/server/handlers/billing/list.go

@@ -86,7 +86,7 @@ func (c *CheckPaymentEnabledHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
 
 	if c.Config().ServerConf.MetronomeAPIKey != "" && c.Config().ServerConf.PorterCloudPlanID != "" &&
 		proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) && proj.UsageID == uuid.Nil {
-		customerID, customerPlanID, err := c.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(ctx, user.CompanyName, proj.Name, proj.ID, proj.BillingID, c.Config().ServerConf.PorterCloudPlanID)
+		customerID, customerPlanID, err := c.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(ctx, user.Email, proj.Name, proj.ID, proj.BillingID, c.Config().ServerConf.PorterCloudPlanID)
 		if err != nil {
 			err = telemetry.Error(ctx, span, err, "error creating Metronome customer")
 			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))

+ 1 - 1
api/server/handlers/project/create.go

@@ -99,7 +99,7 @@ func (p *ProjectCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 
 	// Create Metronome customer and add to starter plan
 	if p.Config().ServerConf.MetronomeAPIKey != "" && p.Config().ServerConf.PorterCloudPlanID != "" && proj.GetFeatureFlag(models.MetronomeEnabled, p.Config().LaunchDarklyClient) {
-		customerID, customerPlanID, err := p.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(ctx, user.CompanyName, proj.Name, proj.ID, proj.BillingID, p.Config().ServerConf.PorterCloudPlanID)
+		customerID, customerPlanID, err := p.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(ctx, user.Email, proj.Name, proj.ID, proj.BillingID, p.Config().ServerConf.PorterCloudPlanID)
 		if err != nil {
 			err = telemetry.Error(ctx, span, err, "error creating Metronome customer")
 			p.HandleAPIError(w, r, apierrors.NewErrInternal(err))

+ 8 - 9
internal/billing/metronome.go

@@ -36,20 +36,15 @@ func NewMetronomeClient(metronomeApiKey string) MetronomeClient {
 }
 
 // createCustomer will create the customer in Metronome
-func (m MetronomeClient) createCustomer(ctx context.Context, orgName string, projectName string, projectID uint, billingID string) (customerID uuid.UUID, err error) {
+func (m MetronomeClient) createCustomer(ctx context.Context, userEmail string, projectName string, projectID uint, billingID string) (customerID uuid.UUID, err error) {
 	ctx, span := telemetry.NewSpan(ctx, "create-metronome-customer")
 	defer span.End()
 
 	path := "customers"
 	projIDStr := strconv.FormatUint(uint64(projectID), 10)
 
-	prefix := "Project"
-	if orgName != "" {
-		prefix = orgName
-	}
-
 	customer := types.Customer{
-		Name: fmt.Sprintf("%s - %s", prefix, projectName),
+		Name: projectName,
 		Aliases: []string{
 			projIDStr,
 		},
@@ -58,6 +53,10 @@ func (m MetronomeClient) createCustomer(ctx context.Context, orgName string, pro
 			BillingProviderCustomerID: billingID,
 			StripeCollectionMethod:    defaultCollectionMethod,
 		},
+		CustomFields: map[string]string{
+			"project_id": projIDStr,
+			"user_email": userEmail,
+		},
 	}
 
 	var result struct {
@@ -107,7 +106,7 @@ func (m MetronomeClient) addCustomerPlan(ctx context.Context, customerID uuid.UU
 }
 
 // CreateCustomerWithPlan will create the customer in Metronome and immediately add it to the plan
-func (m MetronomeClient) CreateCustomerWithPlan(ctx context.Context, orgName string, projectName string, projectID uint, billingID string, planID string) (customerID uuid.UUID, customerPlanID uuid.UUID, err error) {
+func (m MetronomeClient) CreateCustomerWithPlan(ctx context.Context, userEmail string, projectName string, projectID uint, billingID string, planID string) (customerID uuid.UUID, customerPlanID uuid.UUID, err error) {
 	ctx, span := telemetry.NewSpan(ctx, "add-metronome-customer-plan")
 	defer span.End()
 
@@ -116,7 +115,7 @@ func (m MetronomeClient) CreateCustomerWithPlan(ctx context.Context, orgName str
 		return customerID, customerPlanID, telemetry.Error(ctx, span, err, "error parsing starter plan id")
 	}
 
-	customerID, err = m.createCustomer(ctx, orgName, projectName, projectID, billingID)
+	customerID, err = m.createCustomer(ctx, userEmail, projectName, projectID, billingID)
 	if err != nil {
 		return customerID, customerPlanID, telemetry.Error(ctx, span, err, "error while creatinc customer with plan")
 	}