|
|
@@ -13,14 +13,22 @@ import (
|
|
|
"github.com/stripe/stripe-go/v76/setupintent"
|
|
|
)
|
|
|
|
|
|
-// StripeBillingManager interacts with the Stripe API to manage payment methods
|
|
|
+// StripeClient interacts with the Stripe API to manage payment methods
|
|
|
// and customers
|
|
|
-type StripeBillingManager struct {
|
|
|
- StripeSecretKey string
|
|
|
- StripePublishableKey string
|
|
|
+type StripeClient struct {
|
|
|
+ SecretKey string
|
|
|
+ PublishableKey string
|
|
|
+}
|
|
|
+
|
|
|
+func NewStripeClient(secretKey string, publishableKey string) *StripeClient {
|
|
|
+ return &StripeClient{
|
|
|
+ SecretKey: secretKey,
|
|
|
+ PublishableKey: publishableKey,
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
ctx, span := telemetry.NewSpan(ctx, "create-stripe-customer")
|
|
|
defer span.End()
|
|
|
@@ -30,6 +38,13 @@ func (s *StripeBillingManager) CreateCustomer(ctx context.Context, userEmail str
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
@@ -60,6 +75,7 @@ func (s *StripeBillingManager) CreateCustomer(ctx context.Context, userEmail str
|
|
|
}
|
|
|
|
|
|
// DeleteCustomer will delete the customer from the billing provider
|
|
|
+<<<<<<< HEAD
|
|
|
func (s *StripeBillingManager) DeleteCustomer(ctx context.Context, customerID string) (err error) {
|
|
|
ctx, span := telemetry.NewSpan(ctx, "delete-stripe-customer")
|
|
|
defer span.End()
|
|
|
@@ -69,6 +85,13 @@ func (s *StripeBillingManager) DeleteCustomer(ctx context.Context, customerID st
|
|
|
}
|
|
|
|
|
|
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},
|
|
|
@@ -84,6 +107,7 @@ func (s *StripeBillingManager) DeleteCustomer(ctx context.Context, customerID st
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
_, span := telemetry.NewSpan(ctx, "check-stripe-payment-enabled")
|
|
|
defer span.End()
|
|
|
@@ -93,6 +117,13 @@ func (s *StripeBillingManager) CheckPaymentEnabled(ctx context.Context, customer
|
|
|
}
|
|
|
|
|
|
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),
|
|
|
@@ -104,6 +135,7 @@ func (s *StripeBillingManager) CheckPaymentEnabled(ctx context.Context, customer
|
|
|
}
|
|
|
|
|
|
// ListPaymentMethod will return all payment methods for the project
|
|
|
+<<<<<<< HEAD
|
|
|
func (s *StripeBillingManager) ListPaymentMethod(ctx context.Context, customerID string) (paymentMethods []types.PaymentMethod, err error) {
|
|
|
ctx, span := telemetry.NewSpan(ctx, "list-stripe-payment-method")
|
|
|
defer span.End()
|
|
|
@@ -113,6 +145,13 @@ func (s *StripeBillingManager) ListPaymentMethod(ctx context.Context, customerID
|
|
|
}
|
|
|
|
|
|
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{
|
|
|
@@ -157,6 +196,7 @@ func (s *StripeBillingManager) ListPaymentMethod(ctx context.Context, customerID
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
ctx, span := telemetry.NewSpan(ctx, "create-stripe-payment-method")
|
|
|
defer span.End()
|
|
|
@@ -166,6 +206,13 @@ func (s *StripeBillingManager) CreatePaymentMethod(ctx context.Context, customer
|
|
|
}
|
|
|
|
|
|
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),
|
|
|
@@ -184,6 +231,7 @@ func (s *StripeBillingManager) CreatePaymentMethod(ctx context.Context, customer
|
|
|
}
|
|
|
|
|
|
// 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) {
|
|
|
ctx, span := telemetry.NewSpan(ctx, "set-default-stripe-payment-method")
|
|
|
defer span.End()
|
|
|
@@ -193,6 +241,13 @@ func (s *StripeBillingManager) SetDefaultPaymentMethod(ctx context.Context, paym
|
|
|
}
|
|
|
|
|
|
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{
|
|
|
@@ -209,15 +264,19 @@ func (s *StripeBillingManager) SetDefaultPaymentMethod(ctx context.Context, paym
|
|
|
}
|
|
|
|
|
|
// DeletePaymentMethod will remove a payment method for the project in Stripe
|
|
|
-func (s *StripeBillingManager) DeletePaymentMethod(ctx context.Context, paymentMethodID string) (err error) {
|
|
|
+func (s *StripeClient) DeletePaymentMethod(ctx context.Context, paymentMethodID string) (err error) {
|
|
|
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 {
|
|
|
@@ -228,14 +287,14 @@ func (s *StripeBillingManager) DeletePaymentMethod(ctx context.Context, paymentM
|
|
|
}
|
|
|
|
|
|
// GetPublishableKey returns the Stripe publishable key
|
|
|
-func (s *StripeBillingManager) GetPublishableKey(ctx context.Context) (key string) {
|
|
|
+func (s *StripeClient) GetPublishableKey(ctx context.Context) (key string) {
|
|
|
_, span := telemetry.NewSpan(ctx, "get-stripe-publishable-key")
|
|
|
defer span.End()
|
|
|
|
|
|
- return s.StripePublishableKey
|
|
|
+ return s.PublishableKey
|
|
|
}
|
|
|
|
|
|
-func (s *StripeBillingManager) checkDefaultPaymentMethod(customerID string) (defaultPaymentExists bool, defaultPaymentID string, err error) {
|
|
|
+func (s *StripeClient) checkDefaultPaymentMethod(customerID string) (defaultPaymentExists bool, defaultPaymentID string, err error) {
|
|
|
// Get customer to check default payment method
|
|
|
customer, err := customer.Get(customerID, nil)
|
|
|
if err != nil {
|