Parcourir la source

Delete customer on project deletion

Mauricio Araujo il y a 2 ans
Parent
commit
586e32cb8a
3 fichiers modifiés avec 22 ajouts et 1 suppressions
  1. 3 0
      internal/billing/billing.go
  2. 15 0
      internal/billing/stripe.go
  3. 4 1
      internal/models/project.go

+ 3 - 0
internal/billing/billing.go

@@ -11,6 +11,9 @@ type BillingManager interface {
 	// mapping with projects and billing customers, because billing and usage are set per project.
 	CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error)
 
+	// DeleteCustomer will delete the customer from the billing provider
+	DeleteCustomer(proj *models.Project) (err error)
+
 	// ListPaymentMethod will return all payment methods for the project
 	ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error)
 

+ 15 - 0
internal/billing/stripe.go

@@ -41,6 +41,21 @@ func (s *StripeBillingManager) CreateCustomer(userEmail string, proj *models.Pro
 	return customerID, nil
 }
 
+// DeleteCustomer will delete the customer from the billing provider
+func (s *StripeBillingManager) DeleteCustomer(proj *models.Project) (err error) {
+	stripe.Key = s.StripeSecretKey
+
+	if proj.BillingID == "" {
+		params := &stripe.CustomerParams{}
+		_, err := customer.Del(proj.BillingID, params)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 // ListPaymentMethod will return all payment methods for the project
 func (s *StripeBillingManager) ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error) {
 	stripe.Key = s.StripeSecretKey

+ 4 - 1
internal/models/project.go

@@ -134,7 +134,8 @@ type Project struct {
 	Roles []Role `json:"roles"`
 
 	// BillingID corresponds to the id generated by the billing provider
-	BillingID string
+	BillingID      string
+	BillingEnabled bool
 
 	ProjectUsageID      uint
 	ProjectUsageCacheID uint
@@ -227,6 +228,8 @@ func (p *Project) GetFeatureFlag(flagName FeatureFlagLabel, launchDarklyClient *
 			return p.AzureEnabled
 		case "capi_provisioner_enabled":
 			return p.CapiProvisionerEnabled
+		case "billing_enabled":
+			return p.BillingEnabled
 		case "db_enabled":
 			return false
 		case "enable_reprovision":