Skip to content

Commit 8bf8561

Browse files
committed
[stripe] Finalized invoce debits, do not set credits to negative when updating
1 parent a1df2b9 commit 8bf8561

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

components/usage/pkg/apiv1/billing.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInv
108108
ID: uuid.New(),
109109
AttributionID: attributionID,
110110
Description: fmt.Sprintf("Invoice %s finalized in Stripe", invoice.ID),
111-
CreditCents: db.NewCreditCents(float64(creditsOnInvoice)),
111+
// Apply negative value of credits to reduce accrued credit usage
112+
CreditCents: db.NewCreditCents(float64(-creditsOnInvoice)),
112113
EffectiveTime: db.NewVarcharTime(finalizedAt),
113114
Kind: db.InvoiceUsageKind,
114115
Draft: false,

components/usage/pkg/stripe/stripe.go

+10
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ func (c *Client) findCustomers(ctx context.Context, query string) ([]*stripe.Cus
131131
}
132132

133133
func (c *Client) updateUsageForCustomer(ctx context.Context, customer *stripe.Customer, credits int64) (*UsageRecord, error) {
134+
if credits < 0 {
135+
log.WithField("customer_id", customer.ID).
136+
WithField("customer_name", customer.Name).
137+
WithField("credits", credits).
138+
Infof("Received request to update customer %s usage to negative value, updating to 0 instead.", customer.ID)
139+
140+
// nullify any existing usage, but do not set it to negative value - negative invoice doesn't make sense...
141+
credits = 0
142+
}
143+
134144
subscriptions := customer.Subscriptions.Data
135145
if len(subscriptions) != 1 {
136146
return nil, fmt.Errorf("customer has an unexpected number of subscriptions %v (expected 1, got %d)", subscriptions, len(subscriptions))

0 commit comments

Comments
 (0)