|
|
@@ -13,15 +13,23 @@ 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
|
|
|
+}
|
|
|
+
|
|
|
+// NewStripeClient creates a new client to call the Stripe API
|
|
|
+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
|
|
|
-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()
|
|
|
|
|
|
@@ -29,7 +37,7 @@ func (s *StripeBillingManager) CreateCustomer(ctx context.Context, userEmail str
|
|
|
return "", fmt.Errorf("invalid project id or name")
|
|
|
}
|
|
|
|
|
|
- stripe.Key = s.StripeSecretKey
|
|
|
+ stripe.Key = s.SecretKey
|
|
|
|
|
|
// Create customer if not exists
|
|
|
customerName := fmt.Sprintf("project_%s", projectName)
|
|
|
@@ -60,7 +68,7 @@ func (s *StripeBillingManager) CreateCustomer(ctx context.Context, userEmail str
|
|
|
}
|
|
|
|
|
|
// DeleteCustomer will delete the customer from the billing provider
|
|
|
-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()
|
|
|
|
|
|
@@ -68,7 +76,7 @@ func (s *StripeBillingManager) DeleteCustomer(ctx context.Context, customerID st
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- stripe.Key = s.StripeSecretKey
|
|
|
+ stripe.Key = s.SecretKey
|
|
|
|
|
|
telemetry.WithAttributes(span,
|
|
|
telemetry.AttributeKV{Key: "billing-id", Value: customerID},
|
|
|
@@ -84,7 +92,7 @@ func (s *StripeBillingManager) DeleteCustomer(ctx context.Context, customerID st
|
|
|
}
|
|
|
|
|
|
// CheckPaymentEnabled will return true if the project has a payment method added, false otherwise
|
|
|
-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()
|
|
|
|
|
|
@@ -92,7 +100,7 @@ func (s *StripeBillingManager) CheckPaymentEnabled(ctx context.Context, customer
|
|
|
return false, fmt.Errorf("customer id cannot be empty")
|
|
|
}
|
|
|
|
|
|
- stripe.Key = s.StripeSecretKey
|
|
|
+ stripe.Key = s.SecretKey
|
|
|
|
|
|
params := &stripe.PaymentMethodListParams{
|
|
|
Customer: stripe.String(customerID),
|
|
|
@@ -104,7 +112,7 @@ func (s *StripeBillingManager) CheckPaymentEnabled(ctx context.Context, customer
|
|
|
}
|
|
|
|
|
|
// ListPaymentMethod will return all payment methods for the project
|
|
|
-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()
|
|
|
|
|
|
@@ -112,7 +120,7 @@ func (s *StripeBillingManager) ListPaymentMethod(ctx context.Context, customerID
|
|
|
return paymentMethods, fmt.Errorf("customer id cannot be empty")
|
|
|
}
|
|
|
|
|
|
- stripe.Key = s.StripeSecretKey
|
|
|
+ stripe.Key = s.SecretKey
|
|
|
|
|
|
// Get configured payment methods
|
|
|
params := &stripe.PaymentMethodListParams{
|
|
|
@@ -157,7 +165,7 @@ func (s *StripeBillingManager) ListPaymentMethod(ctx context.Context, customerID
|
|
|
}
|
|
|
|
|
|
// CreatePaymentMethod will add a new payment method to the project in Stripe
|
|
|
-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()
|
|
|
|
|
|
@@ -165,7 +173,7 @@ func (s *StripeBillingManager) CreatePaymentMethod(ctx context.Context, customer
|
|
|
return "", fmt.Errorf("customer id cannot be empty")
|
|
|
}
|
|
|
|
|
|
- stripe.Key = s.StripeSecretKey
|
|
|
+ stripe.Key = s.SecretKey
|
|
|
|
|
|
params := &stripe.SetupIntentParams{
|
|
|
Customer: stripe.String(customerID),
|
|
|
@@ -184,7 +192,7 @@ func (s *StripeBillingManager) CreatePaymentMethod(ctx context.Context, customer
|
|
|
}
|
|
|
|
|
|
// SetDefaultPaymentMethod will add a new payment method to the project in Stripe
|
|
|
-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()
|
|
|
|
|
|
@@ -192,7 +200,7 @@ func (s *StripeBillingManager) SetDefaultPaymentMethod(ctx context.Context, paym
|
|
|
return fmt.Errorf("empty customer id or payment method id")
|
|
|
}
|
|
|
|
|
|
- stripe.Key = s.StripeSecretKey
|
|
|
+ stripe.Key = s.SecretKey
|
|
|
|
|
|
params := &stripe.CustomerParams{
|
|
|
InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
|
|
|
@@ -209,7 +217,7 @@ 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()
|
|
|
|
|
|
@@ -217,7 +225,7 @@ func (s *StripeBillingManager) DeletePaymentMethod(ctx context.Context, paymentM
|
|
|
return fmt.Errorf("payment method id cannot be empty")
|
|
|
}
|
|
|
|
|
|
- stripe.Key = s.StripeSecretKey
|
|
|
+ stripe.Key = s.SecretKey
|
|
|
|
|
|
_, err = paymentmethod.Detach(paymentMethodID, nil)
|
|
|
if err != nil {
|
|
|
@@ -228,14 +236,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 {
|