From 89c1b620f0eb99554e680ba9470334cb70fc645d Mon Sep 17 00:00:00 2001 From: Mauricio Araujo Date: Fri, 26 Apr 2024 18:19:22 -0400 Subject: [PATCH] Add porter standard trial days (#4574) --- api/types/billing_metronome.go | 7 +++++++ internal/billing/metronome.go | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/api/types/billing_metronome.go b/api/types/billing_metronome.go index f53ecc6318..d321786b35 100644 --- a/api/types/billing_metronome.go +++ b/api/types/billing_metronome.go @@ -35,6 +35,13 @@ type AddCustomerPlanRequest struct { EndingBeforeUTC string `json:"ending_before,omitempty"` // NetPaymentTermDays is the number of days after issuance of invoice after which the invoice is due NetPaymentTermDays int `json:"net_payment_terms_days,omitempty"` + // Trial is the trial period for the plan + Trial TrialSpec `json:"trial_spec,omitempty"` +} + +// TrialSpec is the trial period for the plan +type TrialSpec struct { + LengthInDays int64 `json:"length_in_days"` } // EndCustomerPlanRequest represents a request to end the plan for a specific customer. diff --git a/internal/billing/metronome.go b/internal/billing/metronome.go index 67631d334c..d6ac4f649f 100644 --- a/internal/billing/metronome.go +++ b/internal/billing/metronome.go @@ -19,6 +19,7 @@ const ( metronomeBaseUrl = "https://api.metronome.com/v1/" defaultCollectionMethod = "charge_automatically" defaultMaxRetries = 10 + porterStandardTrialDays = 15 ) // MetronomeClient is the client used to call the Metronome API @@ -53,13 +54,17 @@ func (m MetronomeClient) CreateCustomerWithPlan(ctx context.Context, userEmail s ctx, span := telemetry.NewSpan(ctx, "add-metronome-customer-plan") defer span.End() + var trialDays uint planID := m.PorterStandardPlanID projID := strconv.FormatUint(uint64(projectID), 10) + if sandboxEnabled { planID = m.PorterCloudPlanID // This is necessary to avoid conflicts with Porter standard projects projID = fmt.Sprintf("porter-cloud-%s", projID) + } else { + trialDays = porterStandardTrialDays } customerID, err = m.createCustomer(ctx, userEmail, projectName, projID, billingID) @@ -67,7 +72,7 @@ func (m MetronomeClient) CreateCustomerWithPlan(ctx context.Context, userEmail s return customerID, customerPlanID, telemetry.Error(ctx, span, err, fmt.Sprintf("error while creating customer with plan %s", planID)) } - customerPlanID, err = m.addCustomerPlan(ctx, customerID, planID) + customerPlanID, err = m.addCustomerPlan(ctx, customerID, planID, trialDays) return customerID, customerPlanID, err } @@ -107,7 +112,7 @@ func (m MetronomeClient) createCustomer(ctx context.Context, userEmail string, p } // addCustomerPlan will start the customer on the given plan -func (m MetronomeClient) addCustomerPlan(ctx context.Context, customerID uuid.UUID, planID uuid.UUID) (customerPlanID uuid.UUID, err error) { +func (m MetronomeClient) addCustomerPlan(ctx context.Context, customerID uuid.UUID, planID uuid.UUID, trialDays uint) (customerPlanID uuid.UUID, err error) { ctx, span := telemetry.NewSpan(ctx, "add-metronome-customer-plan") defer span.End() @@ -127,6 +132,12 @@ func (m MetronomeClient) addCustomerPlan(ctx context.Context, customerID uuid.UU StartingOnUTC: startOn, } + if trialDays != 0 { + req.Trial = types.TrialSpec{ + LengthInDays: int64(trialDays), + } + } + var result struct { Data struct { CustomerPlanID uuid.UUID `json:"id"`