Skip to content

Commit

Permalink
Add porter standard trial days (#4574)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo authored Apr 26, 2024
1 parent 6faaf53 commit 89c1b62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions api/types/billing_metronome.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions internal/billing/metronome.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,21 +54,25 @@ 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)
if err != nil {
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
}
Expand Down Expand Up @@ -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()

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

0 comments on commit 89c1b62

Please sign in to comment.