Skip to content

Commit

Permalink
Metronome custom fields (#4507)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo authored Apr 5, 2024
1 parent afe02f3 commit 6a59f32
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/server/handlers/billing/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion api/server/handlers/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
17 changes: 8 additions & 9 deletions internal/billing/metronome.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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 {
Expand Down Expand Up @@ -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()

Expand All @@ -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")
}
Expand Down

0 comments on commit 6a59f32

Please sign in to comment.