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

Add porter standard trial days (#4574)

Mauricio Araujo 2 лет назад
Родитель
Сommit
89c1b620f0
2 измененных файлов с 20 добавлено и 2 удалено
  1. 7 0
      api/types/billing_metronome.go
  2. 13 2
      internal/billing/metronome.go

+ 7 - 0
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.

+ 13 - 2
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"`