Mauricio Araujo před 2 roky
rodič
revize
84c8ecde27

+ 2 - 6
api/server/handlers/billing/create.go

@@ -42,11 +42,7 @@ func (c *CreateBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 
 	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
 
-<<<<<<< HEAD
-	clientSecret, err := c.Config().BillingManager.CreatePaymentMethod(ctx, proj.BillingID)
-=======
-	clientSecret, err := c.Config().BillingManager.StripeClient.CreatePaymentMethod(ctx, proj)
->>>>>>> b8c4273a5 (Add Metronome business logic)
+	clientSecret, err := c.Config().BillingManager.StripeClient.CreatePaymentMethod(ctx, proj.BillingID)
 	if err != nil {
 		err := telemetry.Error(ctx, span, err, "error creating payment method")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating payment method: %w", err)))
@@ -85,7 +81,7 @@ func (c *SetDefaultBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		return
 	}
 
-	err := c.Config().BillingManager.StripeClient.SetDefaultPaymentMethod(ctx, paymentMethodID, proj)
+	err := c.Config().BillingManager.StripeClient.SetDefaultPaymentMethod(ctx, paymentMethodID, proj.BillingID)
 	if err != nil {
 		err := telemetry.Error(ctx, span, err, "error setting default payment method")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error setting default payment method: %w", err)))

+ 0 - 121
api/server/handlers/billing/customer.go

@@ -1,121 +0,0 @@
-package billing
-
-import (
-	"fmt"
-	"net/http"
-
-	"github.com/google/uuid"
-	"github.com/porter-dev/porter/api/server/handlers"
-	"github.com/porter-dev/porter/api/server/shared"
-	"github.com/porter-dev/porter/api/server/shared/apierrors"
-	"github.com/porter-dev/porter/api/server/shared/config"
-	"github.com/porter-dev/porter/api/types"
-	"github.com/porter-dev/porter/internal/models"
-	"github.com/porter-dev/porter/internal/telemetry"
-)
-
-// CreateBillingCustomerHandler will create a new handler
-// for creating customers in the billing provider
-type CreateBillingCustomerHandler struct {
-	handlers.PorterHandlerReadWriter
-}
-
-// NewCreateBillingCustomerIfNotExists will create a new CreateBillingCustomerIfNotExists
-func NewCreateBillingCustomerIfNotExists(
-	config *config.Config,
-	writer shared.ResultWriter,
-) *CreateBillingCustomerHandler {
-	return &CreateBillingCustomerHandler{
-		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
-	}
-}
-
-func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	ctx, span := telemetry.NewSpan(r.Context(), "create-billing-customer-endpoint")
-	defer span.End()
-
-	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
-	user, _ := r.Context().Value(types.UserScope).(*models.User)
-
-	var shouldUpdate bool
-	if proj.BillingID == "" {
-		// Create customer in Stripe
-		customerID, err := c.Config().BillingManager.StripeClient.CreateCustomer(ctx, user.Email, proj)
-		if err != nil {
-			err := telemetry.Error(ctx, span, err, "error creating billing customer")
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating billing customer: %w", err)))
-			return
-		}
-
-		telemetry.WithAttributes(span,
-			telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
-			telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
-			telemetry.AttributeKV{Key: "user-email", Value: user.Email},
-		)
-		proj.BillingID = customerID
-		shouldUpdate = true
-	}
-
-	if proj.UsageID == uuid.Nil {
-		// Create Metronome customer and add to starter plan
-		if c.Config().ServerConf.MetronomeAPIKey != "" && c.Config().ServerConf.PorterCloudPlanID != "" && proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) {
-			customerPlanID, err := c.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(user.CompanyName, 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))
-			}
-			proj.UsagePlanID = customerPlanID
-			telemetry.WithAttributes(span,
-				telemetry.AttributeKV{Key: "usage-id", Value: proj.UsageID},
-				telemetry.AttributeKV{Key: "usage-plan-id", Value: proj.UsagePlanID},
-			)
-		}
-	}
-
-	if shouldUpdate {
-		// Update the project record with the customer ID
-		_, err := c.Repo().Project().UpdateProject(proj)
-		if err != nil {
-			err := telemetry.Error(ctx, span, err, "error updating project")
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error updating project: %w", err)))
-			return
-		}
-	}
-
-	c.WriteResult(w, r, "")
-}
-
-// GetPublishableKeyHandler will return the configured publishable key
-type GetPublishableKeyHandler struct {
-	handlers.PorterHandlerReadWriter
-}
-
-// NewGetPublishableKeyHandler will return the publishable key
-func NewGetPublishableKeyHandler(
-	config *config.Config,
-	decoderValidator shared.RequestDecoderValidator,
-	writer shared.ResultWriter,
-) *GetPublishableKeyHandler {
-	return &GetPublishableKeyHandler{
-		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
-	}
-}
-
-func (c *GetPublishableKeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	ctx, span := telemetry.NewSpan(r.Context(), "get-publishable-key-endpoint")
-	defer span.End()
-
-	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
-
-	// There is no easy way to pass environment variables to the frontend,
-	// so for now pass via the backend. This is acceptable because the key is
-	// meant to be public
-	publishableKey := c.Config().BillingManager.StripeClient.GetPublishableKey(ctx)
-
-	telemetry.WithAttributes(span,
-		telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
-		telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
-	)
-
-	c.WriteResult(w, r, publishableKey)
-}

+ 2 - 2
api/server/handlers/billing/key.go

@@ -37,7 +37,7 @@ func (c *GetPublishableKeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 
 	// Create billing customer for project and set the billing ID if it doesn't exist
 	if proj.BillingID == "" {
-		billingID, err := c.Config().BillingManager.CreateCustomer(ctx, user.Email, proj.ID, proj.Name)
+		billingID, err := c.Config().BillingManager.StripeClient.CreateCustomer(ctx, user.Email, proj.ID, proj.Name)
 		if err != nil {
 			err = telemetry.Error(ctx, span, err, "error creating billing customer")
 			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
@@ -56,7 +56,7 @@ func (c *GetPublishableKeyHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 	// There is no easy way to pass environment variables to the frontend,
 	// so for now pass via the backend. This is acceptable because the key is
 	// meant to be public
-	publishableKey := c.Config().BillingManager.GetPublishableKey(ctx)
+	publishableKey := c.Config().BillingManager.StripeClient.GetPublishableKey(ctx)
 
 	telemetry.WithAttributes(span,
 		telemetry.AttributeKV{Key: "project-id", Value: proj.ID},

+ 3 - 7
api/server/handlers/billing/list.go

@@ -42,7 +42,7 @@ func (c *ListBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
 	// Create billing customer for project and set the billing ID if it doesn't exist
 	if proj.BillingID == "" {
-		billingID, err := c.Config().BillingManager.CreateCustomer(ctx, user.Email, proj.ID, proj.Name)
+		billingID, err := c.Config().BillingManager.StripeClient.CreateCustomer(ctx, user.Email, proj.ID, proj.Name)
 		if err != nil {
 			err = telemetry.Error(ctx, span, err, "error creating billing customer")
 			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
@@ -58,7 +58,7 @@ func (c *ListBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		}
 	}
 
-	paymentMethods, err := c.Config().BillingManager.StripeClient.ListPaymentMethod(ctx, proj)
+	paymentMethods, err := c.Config().BillingManager.StripeClient.ListPaymentMethod(ctx, proj.BillingID)
 	if err != nil {
 		err := telemetry.Error(ctx, span, err, "error listing payment method")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error listing payment method: %w", err)))
@@ -84,11 +84,7 @@ func (c *CheckPaymentEnabledHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
 
 	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
 
-<<<<<<< HEAD
-	paymentEnabled, err := c.Config().BillingManager.CheckPaymentEnabled(ctx, proj.BillingID)
-=======
-	paymentEnabled, err := c.Config().BillingManager.StripeClient.CheckPaymentEnabled(ctx, proj)
->>>>>>> b8c4273a5 (Add Metronome business logic)
+	paymentEnabled, err := c.Config().BillingManager.StripeClient.CheckPaymentEnabled(ctx, proj.BillingID)
 	if err != nil {
 		err := telemetry.Error(ctx, span, err, "error checking if payment enabled")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error checking if payment enabled: %w", err)))

+ 1 - 1
api/server/handlers/project/create.go

@@ -82,7 +82,7 @@ func (p *ProjectCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 	// Create Stripe Customer
 	if p.Config().ServerConf.StripeSecretKey != "" && p.Config().ServerConf.StripePublishableKey != "" {
 		// Create billing customer for project and set the billing ID
-		billingID, err := p.Config().BillingManager.StripeClient.CreateCustomer(ctx, user.Email, proj)
+		billingID, err := p.Config().BillingManager.StripeClient.CreateCustomer(ctx, user.Email, proj.ID, proj.Name)
 		if err != nil {
 			err = telemetry.Error(ctx, span, err, "error creating billing customer")
 			p.HandleAPIError(w, r, apierrors.NewErrInternal(err))

+ 6 - 58
internal/billing/stripe.go

@@ -29,8 +29,7 @@ func NewStripeClient(secretKey string, publishableKey string) *StripeClient {
 }
 
 // CreateCustomer will create a customer in Stripe only if the project doesn't have a BillingID
-<<<<<<< HEAD
-func (s *StripeBillingManager) CreateCustomer(ctx context.Context, userEmail string, projectID uint, projectName string) (customerID string, err error) {
+func (s *StripeClient) CreateCustomer(ctx context.Context, userEmail string, projectID uint, projectName string) (customerID string, err error) {
 	ctx, span := telemetry.NewSpan(ctx, "create-stripe-customer")
 	defer span.End()
 
@@ -38,14 +37,7 @@ func (s *StripeBillingManager) CreateCustomer(ctx context.Context, userEmail str
 		return "", fmt.Errorf("invalid project id or name")
 	}
 
-	stripe.Key = s.StripeSecretKey
-=======
-func (s *StripeClient) CreateCustomer(ctx context.Context, userEmail string, proj *models.Project) (customerID string, err error) {
-	ctx, span := telemetry.NewSpan(ctx, "create-stripe-customer")
-	defer span.End()
-
 	stripe.Key = s.SecretKey
->>>>>>> b8c4273a5 (Add Metronome business logic)
 
 	// Create customer if not exists
 	customerName := fmt.Sprintf("project_%s", projectName)
@@ -76,8 +68,7 @@ func (s *StripeClient) CreateCustomer(ctx context.Context, userEmail string, pro
 }
 
 // DeleteCustomer will delete the customer from the billing provider
-<<<<<<< HEAD
-func (s *StripeBillingManager) DeleteCustomer(ctx context.Context, customerID string) (err error) {
+func (s *StripeClient) DeleteCustomer(ctx context.Context, customerID string) (err error) {
 	ctx, span := telemetry.NewSpan(ctx, "delete-stripe-customer")
 	defer span.End()
 
@@ -85,14 +76,7 @@ func (s *StripeBillingManager) DeleteCustomer(ctx context.Context, customerID st
 		return nil
 	}
 
-	stripe.Key = s.StripeSecretKey
-=======
-func (s *StripeClient) DeleteCustomer(ctx context.Context, proj *models.Project) (err error) {
-	ctx, span := telemetry.NewSpan(ctx, "delete-stripe-customer")
-	defer span.End()
-
 	stripe.Key = s.SecretKey
->>>>>>> b8c4273a5 (Add Metronome business logic)
 
 	telemetry.WithAttributes(span,
 		telemetry.AttributeKV{Key: "billing-id", Value: customerID},
@@ -108,8 +92,7 @@ func (s *StripeClient) DeleteCustomer(ctx context.Context, proj *models.Project)
 }
 
 // CheckPaymentEnabled will return true if the project has a payment method added, false otherwise
-<<<<<<< HEAD
-func (s *StripeBillingManager) CheckPaymentEnabled(ctx context.Context, customerID string) (paymentEnabled bool, err error) {
+func (s *StripeClient) CheckPaymentEnabled(ctx context.Context, customerID string) (paymentEnabled bool, err error) {
 	_, span := telemetry.NewSpan(ctx, "check-stripe-payment-enabled")
 	defer span.End()
 
@@ -117,14 +100,7 @@ func (s *StripeBillingManager) CheckPaymentEnabled(ctx context.Context, customer
 		return false, fmt.Errorf("customer id cannot be empty")
 	}
 
-	stripe.Key = s.StripeSecretKey
-=======
-func (s *StripeClient) CheckPaymentEnabled(ctx context.Context, proj *models.Project) (paymentEnabled bool, err error) {
-	_, span := telemetry.NewSpan(ctx, "check-stripe-payment-enabled")
-	defer span.End()
-
 	stripe.Key = s.SecretKey
->>>>>>> b8c4273a5 (Add Metronome business logic)
 
 	params := &stripe.PaymentMethodListParams{
 		Customer: stripe.String(customerID),
@@ -136,8 +112,7 @@ func (s *StripeClient) CheckPaymentEnabled(ctx context.Context, proj *models.Pro
 }
 
 // ListPaymentMethod will return all payment methods for the project
-<<<<<<< HEAD
-func (s *StripeBillingManager) ListPaymentMethod(ctx context.Context, customerID string) (paymentMethods []types.PaymentMethod, err error) {
+func (s *StripeClient) ListPaymentMethod(ctx context.Context, customerID string) (paymentMethods []types.PaymentMethod, err error) {
 	ctx, span := telemetry.NewSpan(ctx, "list-stripe-payment-method")
 	defer span.End()
 
@@ -145,14 +120,7 @@ func (s *StripeBillingManager) ListPaymentMethod(ctx context.Context, customerID
 		return paymentMethods, fmt.Errorf("customer id cannot be empty")
 	}
 
-	stripe.Key = s.StripeSecretKey
-=======
-func (s *StripeClient) ListPaymentMethod(ctx context.Context, proj *models.Project) (paymentMethods []types.PaymentMethod, err error) {
-	ctx, span := telemetry.NewSpan(ctx, "list-stripe-payment-method")
-	defer span.End()
-
 	stripe.Key = s.SecretKey
->>>>>>> b8c4273a5 (Add Metronome business logic)
 
 	// Get configured payment methods
 	params := &stripe.PaymentMethodListParams{
@@ -197,8 +165,7 @@ func (s *StripeClient) ListPaymentMethod(ctx context.Context, proj *models.Proje
 }
 
 // CreatePaymentMethod will add a new payment method to the project in Stripe
-<<<<<<< HEAD
-func (s *StripeBillingManager) CreatePaymentMethod(ctx context.Context, customerID string) (clientSecret string, err error) {
+func (s *StripeClient) CreatePaymentMethod(ctx context.Context, customerID string) (clientSecret string, err error) {
 	ctx, span := telemetry.NewSpan(ctx, "create-stripe-payment-method")
 	defer span.End()
 
@@ -206,14 +173,7 @@ func (s *StripeBillingManager) CreatePaymentMethod(ctx context.Context, customer
 		return "", fmt.Errorf("customer id cannot be empty")
 	}
 
-	stripe.Key = s.StripeSecretKey
-=======
-func (s *StripeClient) CreatePaymentMethod(ctx context.Context, proj *models.Project) (clientSecret string, err error) {
-	ctx, span := telemetry.NewSpan(ctx, "create-stripe-payment-method")
-	defer span.End()
-
 	stripe.Key = s.SecretKey
->>>>>>> b8c4273a5 (Add Metronome business logic)
 
 	params := &stripe.SetupIntentParams{
 		Customer: stripe.String(customerID),
@@ -232,8 +192,7 @@ func (s *StripeClient) CreatePaymentMethod(ctx context.Context, proj *models.Pro
 }
 
 // SetDefaultPaymentMethod will add a new payment method to the project in Stripe
-<<<<<<< HEAD
-func (s *StripeBillingManager) SetDefaultPaymentMethod(ctx context.Context, paymentMethodID string, customerID string) (err error) {
+func (s *StripeClient) SetDefaultPaymentMethod(ctx context.Context, paymentMethodID string, customerID string) (err error) {
 	ctx, span := telemetry.NewSpan(ctx, "set-default-stripe-payment-method")
 	defer span.End()
 
@@ -241,14 +200,7 @@ func (s *StripeBillingManager) SetDefaultPaymentMethod(ctx context.Context, paym
 		return fmt.Errorf("empty customer id or payment method id")
 	}
 
-	stripe.Key = s.StripeSecretKey
-=======
-func (s *StripeClient) SetDefaultPaymentMethod(ctx context.Context, paymentMethodID string, proj *models.Project) (err error) {
-	ctx, span := telemetry.NewSpan(ctx, "set-default-stripe-payment-method")
-	defer span.End()
-
 	stripe.Key = s.SecretKey
->>>>>>> b8c4273a5 (Add Metronome business logic)
 
 	params := &stripe.CustomerParams{
 		InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
@@ -269,15 +221,11 @@ func (s *StripeClient) DeletePaymentMethod(ctx context.Context, paymentMethodID
 	ctx, span := telemetry.NewSpan(ctx, "delete-stripe-payment-method")
 	defer span.End()
 
-<<<<<<< HEAD
 	if paymentMethodID == "" {
 		return fmt.Errorf("payment method id cannot be empty")
 	}
 
-	stripe.Key = s.StripeSecretKey
-=======
 	stripe.Key = s.SecretKey
->>>>>>> b8c4273a5 (Add Metronome business logic)
 
 	_, err = paymentmethod.Detach(paymentMethodID, nil)
 	if err != nil {