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

Remove unnecessary pointers, custom payment method type

Mauricio Araujo 2 лет назад
Родитель
Сommit
5611334451

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

@@ -33,7 +33,7 @@ func (c *CreateBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 	ctx, span := telemetry.NewSpan(r.Context(), "auth-endpoint-api-token")
 	defer span.End()
 
-	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
+	proj, _ := ctx.Value(types.ProjectScope).(models.Project)
 
 	clientSecret, err := c.Config().BillingManager.CreatePaymentMethod(proj)
 	if err != nil {

+ 1 - 1
api/server/handlers/billing/list.go

@@ -32,7 +32,7 @@ func (c *ListBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	ctx, span := telemetry.NewSpan(r.Context(), "auth-endpoint-api-token")
 	defer span.End()
 
-	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
+	proj, _ := ctx.Value(types.ProjectScope).(models.Project)
 
 	paymentMethods, err := c.Config().BillingManager.ListPaymentMethod(proj)
 	if err != nil {

+ 7 - 5
api/types/billing.go

@@ -1,12 +1,14 @@
 package types
 
-import "github.com/stripe/stripe-go/v76"
-
 // CreateBillingCustomerRequest is a request for creating a new billing customer.
 type CreateBillingCustomerRequest struct {
 	UserEmail string `json:"user_email" form:"required"`
 }
 
-// PaymentMethod is a wrapper for the Stripe type, but it may be changed to include only
-// the necessary fields.
-type PaymentMethod = *stripe.PaymentMethod
+// PaymentMethod is a subset of the Stripe PaymentMethod type,
+// with only the fields used on the dashboard
+type PaymentMethod = struct {
+	ID           string `json:"id"`
+	DisplayBrand string `json:"display_brand"`
+	Last4        string `json:"last4"`
+}

+ 1 - 1
internal/billing/billing.go

@@ -19,7 +19,7 @@ type BillingManager interface {
 
 	// CreateCustomer registers a project in the billing provider. This is currently a one-to-one
 	// mapping with projects and billing customers, because billing and usage are set per project.
-	CreateCustomer(userEmail string, proj models.Project) (customerID string, err error)
+	CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error)
 
 	// ListPaymentMethod will return all payment methods for the project
 	ListPaymentMethod(proj models.Project) (paymentMethods []types.PaymentMethod, err error)

+ 8 - 2
internal/billing/stripe..go → internal/billing/stripe.go

@@ -18,7 +18,7 @@ type StripeBillingManager struct {
 }
 
 // CreateCustomer will create a customer in Stripe only if the project doesn't have a BillingID
-func (s *StripeBillingManager) CreateCustomer(userEmail string, proj models.Project) (customerID string, err error) {
+func (s *StripeBillingManager) CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error) {
 	stripe.Key = s.StripeSecretKey
 
 	if proj.BillingID == "" {
@@ -52,7 +52,13 @@ func (s *StripeBillingManager) ListPaymentMethod(proj models.Project) (paymentMe
 	result := paymentmethod.List(params)
 
 	for result.Next() {
-		paymentMethods = append(paymentMethods, result.PaymentMethod())
+		stripePaymentMethod := result.PaymentMethod()
+
+		paymentMethods = append(paymentMethods, types.PaymentMethod{
+			ID:           stripePaymentMethod.ID,
+			DisplayBrand: stripePaymentMethod.Card.DisplayBrand,
+			Last4:        stripePaymentMethod.Card.Last4,
+		})
 	}
 
 	return paymentMethods, nil