Przeglądaj źródła

Address review comments

Mauricio Araujo 2 lat temu
rodzic
commit
e3897f48aa

+ 9 - 7
api/server/shared/config/loader/loader.go

@@ -62,13 +62,6 @@ func sharedInit() {
 	if err != nil {
 		panic(err)
 	}
-
-	stripeClient := billing.NewStripeClient(InstanceEnvConf.ServerConf.StripeSecretKey, InstanceEnvConf.ServerConf.StripePublishableKey)
-	metronomeClient := billing.NewMetronomeClient(InstanceEnvConf.ServerConf.MetronomeAPIKey)
-	InstanceBillingManager = billing.Manager{
-		StripeClient:    stripeClient,
-		MetronomeClient: metronomeClient,
-	}
 }
 
 func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
@@ -349,6 +342,15 @@ func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
 		CollectorURL: sc.TelemetryCollectorURL,
 	}
 
+	res.Logger.Info().Msg("Creating billing manager")
+	stripeClient := billing.NewStripeClient(InstanceEnvConf.ServerConf.StripeSecretKey, InstanceEnvConf.ServerConf.StripePublishableKey)
+	metronomeClient := billing.NewMetronomeClient(InstanceEnvConf.ServerConf.MetronomeAPIKey)
+	InstanceBillingManager = billing.Manager{
+		StripeClient:    stripeClient,
+		MetronomeClient: metronomeClient,
+	}
+	res.Logger.Info().Msg("Created billing manager")
+
 	return res, nil
 }
 

+ 0 - 113
api/types/billing.go

@@ -1,113 +0,0 @@
-package types
-
-import "github.com/google/uuid"
-
-// Stripe types
-
-// PaymentMethod is a subset of the Stripe PaymentMethod type,
-// with only the fields used in the dashboard
-type PaymentMethod = struct {
-	ID           string `json:"id"`
-	DisplayBrand string `json:"display_brand"`
-	Last4        string `json:"last4"`
-	ExpMonth     int64  `json:"exp_month"`
-	ExpYear      int64  `json:"exp_year"`
-	Default      bool   `json:"is_default"`
-}
-
-// Metronome Types
-
-// Customer represents a customer in Metronome
-type Customer struct {
-	ID            uuid.UUID         `json:"id"`
-	Name          string            `json:"name"`           // Required. Name of the customer
-	Aliases       []string          `json:"ingest_aliases"` // Aliases that can be used to refer to this customer in usage events
-	BillingConfig BillingConfig     `json:"billing_config,omitempty"`
-	CustomFields  map[string]string `json:"custom_fields,omitempty"`
-}
-
-// CustomerArchiveRequest will archive the customer in Metronome.
-type CustomerArchiveRequest struct {
-	CustomerID uuid.UUID `json:"id"`
-}
-
-// BillingConfig is the configuration for the billing provider (Stripe, etc.)
-type BillingConfig struct {
-	BillingProviderType       string `json:"billing_provider_type"` // Required. Can be any of "aws_marketplace", "stripe", "netsuite", "custom", "azure_marketplace", "quickbooks_online", or "workday"
-	BillingProviderCustomerID string `json:"billing_provider_customer_id"`
-	StripeCollectionMethod    string `json:"stripe_collection_method"` // Can be any of "charge_automatically" or "send_invoice"
-}
-
-// AddCustomerPlanRequest represents a request to add a customer plan with specific details.
-type AddCustomerPlanRequest struct {
-	PlanID              uuid.UUID `json:"plan_id"`                          // Required. The customer ID, plan ID, and date range for the plan to be applied.
-	StartingOn          string    `json:"starting_on"`                      // Required. RFC 3339 timestamp for when the plan becomes active for this customer. Must be at 0:00 UTC (midnight).
-	EndingBefore        string    `json:"ending_before,omitempty"`          // Optional. RFC 3339 timestamp for when the plan ends (exclusive) for this customer. Must be at 0:00 UTC (midnight).
-	NetPaymentTermsDays int       `json:"net_payment_terms_days,omitempty"` // Number of days after issuance of invoice after which the invoice is due (e.g., Net 30).
-}
-
-// AddCustomerPlanResponse is a response to the add customer plan request. Returns customer-plan relationship id.
-type AddCustomerPlanResponse struct {
-	Data struct {
-		CustomerPlanID uuid.UUID `json:"id"`
-	} `json:"data"`
-}
-
-// EndCustomerPlanRequest represents a request to end the plan for a specific customer.
-type EndCustomerPlanRequest struct {
-	EndingBefore       string `json:"ending_before,omitempty"` // RFC 3339 timestamp for when the plan ends (exclusive) for this customer. Must be at 0:00 UTC (midnight).
-	VoidInvoices       bool   `json:"void_invoices"`           // If true, plan end date can be before the last finalized invoice date. Any invoices generated after the plan end date will be voided.
-	VoidStripeInvoices bool   `json:"void_stripe_invoices"`    // Will void Stripe invoices if VoidInvoices is set to true. Drafts will be deleted.
-}
-
-// ListCreditGrantsRequest is the request to list a user's credit grants
-type ListCreditGrantsRequest struct {
-	// An array of credit type IDs. This must not be specified if
-	// credit_grant_ids is specified.
-	CreditTypeIDs []uuid.UUID `json:"credit_type_ids,omitempty"`
-	// An array of Metronome customer IDs. This must not be specified if
-	// credit_grant_ids is specified.
-	CustomerIDs []uuid.UUID `json:"customer_ids,omitempty"`
-	// An array of credit grant IDs. If this is specified, neither
-	// credit_type_ids nor customer_ids may be specified.
-	CreditGrantIDs []uuid.UUID `json:"credit_grant_ids,omitempty"`
-	// Only return credit grants that expire at or after this RFC 3339 timestamp.
-	NotExpiringBefore string `json:"not_expiring_before,omitempty"`
-	// Only return credit grants that are effective before this RFC 3339 timestamp
-	// (exclusive).
-	EffectiveBefore string `json:"effective_before,omitempty"`
-}
-
-// ListCreditGrantsResponse is the response returned by the list credit grants request
-type ListCreditGrantsResponse struct {
-	Data []CreditGrant `json:"data"`
-}
-
-// CreditType is the type of the credit used in the credit grant
-type CreditType struct {
-	Name string `json:"name"` // The name of the credit type
-	ID   string `json:"id"`   // The UUID of the credit type
-}
-
-// GrantAmount represents the amount of credits granted
-type GrantAmount struct {
-	Amount     int64      `json:"amount"`      // The amount of credits granted
-	CreditType CreditType `json:"credit_type"` // The credit type for the amount granted
-}
-
-// Balance represents the effective balance of the grant as of the end of the customer's
-// current billing period.
-type Balance struct {
-	ExcludingPending int64  `json:"excluding_pending"` // The grant's current balance excluding all pending deductions.
-	IncludingPending int64  `json:"including_pending"` // The grant's current balance including all posted and pending deductions.
-	EffectiveAt      string `json:"effective_at"`      // The end date of the customer's current billing period in RFC 3339 format.
-}
-
-// CreditGrant is a grant given to a specific user on a specific plan
-type CreditGrant struct {
-	ID          uuid.UUID `json:"id"`
-	Name        string
-	CustomerID  uuid.UUID
-	GrantAmount GrantAmount
-	Balance     Balance
-}

+ 93 - 0
api/types/billing_metronome.go

@@ -0,0 +1,93 @@
+package types
+
+import "github.com/google/uuid"
+
+// Customer represents a customer in Metronome
+type Customer struct {
+	ID   uuid.UUID `json:"id"`
+	Name string    `json:"name"`
+	// Aliases are alternative ids that can be used to refer to this customer in usage events
+	Aliases       []string          `json:"ingest_aliases"`
+	BillingConfig BillingConfig     `json:"billing_config,omitempty"`
+	CustomFields  map[string]string `json:"custom_fields,omitempty"`
+}
+
+// CustomerArchiveRequest will archive the customer in Metronome.
+type CustomerArchiveRequest struct {
+	CustomerID uuid.UUID `json:"id"`
+}
+
+// BillingConfig is the configuration for the billing provider (Stripe, etc.)
+type BillingConfig struct {
+	// BillingProviderType is the name of the billing provider (e.g. )
+	BillingProviderType       string `json:"billing_provider_type"`
+	BillingProviderCustomerID string `json:"billing_provider_customer_id"`
+	// StripeCollectionMethod defines if invoices are charged automatically or sent to customers
+	StripeCollectionMethod string `json:"stripe_collection_method"`
+}
+
+// AddCustomerPlanRequest represents a request to add a customer plan with specific details.
+type AddCustomerPlanRequest struct {
+	PlanID uuid.UUID `json:"plan_id"`
+	// StartingOn is a RFC3339 timestamp for when the plan becomes active for this customer. Must be at 0:00 UTC (midnight)
+	StartingOnUTC string `json:"starting_on"`
+	// EndingBeforeUTC is a RFC 3339 timestamp for when the plan ends (exclusive) for this customer. Must be at 0:00 UTC (midnight)
+	EndingBeforeUTC string `json:"ending_before,omitempty"`
+	// NetPaymentTermsDays is the number of days after issuance of invoice after which the invoice is due
+	NetPaymentTermsDays int `json:"net_payment_terms_days,omitempty"`
+}
+
+// EndCustomerPlanRequest represents a request to end the plan for a specific customer.
+type EndCustomerPlanRequest struct {
+	// EndingBeforeUTC is a RFC 3339 timestamp for when the plan ends (exclusive) for this customer. Must be at 0:00 UTC (midnight).
+	EndingBeforeUTC string `json:"ending_before,omitempty"`
+	// VoidInvoices determines if Metronome invoices are voided. If set to true, the plan end date can be before the last finalized invoice date.
+	// and any invoices generated after the plan end date will be voided.
+	VoidInvoices bool `json:"void_invoices"`
+	// VoidStripeInvoices determines if Stripe invoices are void (if VoidInvoices is set to true). Drafts will be deleted.
+	VoidStripeInvoices bool `json:"void_stripe_invoices"`
+}
+
+// ListCreditGrantsRequest is the request to list a user's credit grants. Note that only one of
+// CreditTypeIDs, CustomerIDs, or CreditGrantIDs must be specified.
+type ListCreditGrantsRequest struct {
+	CreditTypeIDs  []uuid.UUID `json:"credit_type_ids,omitempty"`
+	CustomerIDs    []uuid.UUID `json:"customer_ids,omitempty"`
+	CreditGrantIDs []uuid.UUID `json:"credit_grant_ids,omitempty"`
+	// NotExpiringBefore will return grants that expire at or after this RFC 3339 timestamp.
+	NotExpiringBefore string `json:"not_expiring_before,omitempty"`
+	// EffectiveBefore will return grants that are effective before this RFC 3339 timestamp (exclusive).
+	EffectiveBefore string `json:"effective_before,omitempty"`
+}
+
+// CreditType is the type of the credit used in the credit grant
+type CreditType struct {
+	Name string `json:"name"`
+	ID   string `json:"id"`
+}
+
+// GrantAmount represents the amount of credits granted
+type GrantAmount struct {
+	Amount     int64      `json:"amount"`
+	CreditType CreditType `json:"credit_type"`
+}
+
+// Balance represents the effective balance of the grant as of the end of the customer's
+// current billing period.
+type Balance struct {
+	// ExcludingPending is the grant's current balance excluding pending deductions
+	ExcludingPending int64 `json:"excluding_pending"`
+	// IncludingPending is the grant's current balance including pending deductions
+	IncludingPending int64 `json:"including_pending"`
+	// EffectiveAt is a RFC3339 timestamp that can be used to filter credit grants by effective date
+	EffectiveAt string `json:"effective_at"`
+}
+
+// CreditGrant is a grant given to a specific user on a specific plan
+type CreditGrant struct {
+	ID          uuid.UUID `json:"id"`
+	Name        string
+	CustomerID  uuid.UUID
+	GrantAmount GrantAmount
+	Balance     Balance
+}

+ 12 - 0
api/types/billing_stripe.go

@@ -0,0 +1,12 @@
+package types
+
+// PaymentMethod is a subset of the Stripe PaymentMethod type,
+// with only the fields used in the dashboard
+type PaymentMethod = struct {
+	ID           string `json:"id"`
+	DisplayBrand string `json:"display_brand"`
+	Last4        string `json:"last4"`
+	ExpMonth     int64  `json:"exp_month"`
+	ExpYear      int64  `json:"exp_year"`
+	Default      bool   `json:"is_default"`
+}

+ 2 - 2
internal/billing/billing.go

@@ -2,6 +2,6 @@ package billing
 
 // Manager contains methods for managing billing for a project
 type Manager struct {
-	StripeClient    *StripeClient
-	MetronomeClient *MetronomeClient
+	StripeClient    StripeClient
+	MetronomeClient MetronomeClient
 }

+ 14 - 7
internal/billing/metronome.go

@@ -27,8 +27,8 @@ type MetronomeClient struct {
 }
 
 // NewMetronomeClient returns a new Metronome client
-func NewMetronomeClient(metronomeApiKey string) *MetronomeClient {
-	return &MetronomeClient{
+func NewMetronomeClient(metronomeApiKey string) MetronomeClient {
+	return MetronomeClient{
 		ApiKey: metronomeApiKey,
 	}
 }
@@ -75,11 +75,15 @@ func (m *MetronomeClient) addCustomerPlan(customerID uuid.UUID, planID uuid.UUID
 	startOn := midnightUTC.Format(time.RFC3339)
 
 	req := types.AddCustomerPlanRequest{
-		PlanID:     planID,
-		StartingOn: startOn,
+		PlanID:        planID,
+		StartingOnUTC: startOn,
 	}
 
-	var result types.AddCustomerPlanResponse
+	var result struct {
+		Data struct {
+			CustomerPlanID uuid.UUID `json:"id"`
+		} `json:"data"`
+	}
 
 	err = post(path, m.ApiKey, req, &result)
 	if err != nil {
@@ -118,7 +122,7 @@ func (m *MetronomeClient) EndCustomerPlan(customerID uuid.UUID, customerPlanID u
 	endBefore := midnightUTC.Format(time.RFC3339)
 
 	req := types.EndCustomerPlanRequest{
-		EndingBefore: endBefore,
+		EndingBeforeUTC: endBefore,
 	}
 
 	err = post(path, m.ApiKey, req, nil)
@@ -143,7 +147,10 @@ func (m *MetronomeClient) GetCustomerCredits(customerID uuid.UUID) (credits int6
 		},
 	}
 
-	var result types.ListCreditGrantsResponse
+	var result struct {
+		Data []types.CreditGrant `json:"data"`
+	}
+
 	err = post(path, m.ApiKey, req, &result)
 	if err != nil {
 		return credits, err

+ 2 - 2
internal/billing/stripe.go

@@ -21,8 +21,8 @@ type StripeClient struct {
 }
 
 // NewStripeClient creates a new client to call the Stripe API
-func NewStripeClient(secretKey string, publishableKey string) *StripeClient {
-	return &StripeClient{
+func NewStripeClient(secretKey string, publishableKey string) StripeClient {
+	return StripeClient{
 		SecretKey:      secretKey,
 		PublishableKey: publishableKey,
 	}