Skip to content

Commit

Permalink
[stripe] Finalized invoce debits, do not set credits to negative when…
Browse files Browse the repository at this point in the history
… updating
  • Loading branch information
easyCZ committed Sep 8, 2022
1 parent c2d4b28 commit 27f05d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion components/usage/pkg/apiv1/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInv
ID: uuid.New(),
AttributionID: attributionID,
Description: fmt.Sprintf("Invoice %s finalized in Stripe", invoice.ID),
CreditCents: db.NewCreditCents(float64(creditsOnInvoice)),
// Apply negative value of credits to reduce accrued credit usage
CreditCents: db.NewCreditCents(float64(-creditsOnInvoice)),
EffectiveTime: db.NewVarcharTime(finalizedAt),
Kind: db.InvoiceUsageKind,
Draft: false,
Expand Down
10 changes: 10 additions & 0 deletions components/usage/pkg/stripe/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ func (c *Client) findCustomers(ctx context.Context, query string) ([]*stripe.Cus
}

func (c *Client) updateUsageForCustomer(ctx context.Context, customer *stripe.Customer, credits int64) (*UsageRecord, error) {
if credits < 0 {
log.WithField("customer_id", customer.ID).
WithField("customer_name", customer.Name).
WithField("credits", credits).
Infof("Received request to update customer %s usage to negative value, updating to 0 instead.", customer.ID)

// nullify any existing usage, but do not set it to negative value - negative invoice doesn't make sense...
credits = 0
}

subscriptions := customer.Subscriptions.Data
if len(subscriptions) != 1 {
return nil, fmt.Errorf("customer has an unexpected number of subscriptions %v (expected 1, got %d)", subscriptions, len(subscriptions))
Expand Down

0 comments on commit 27f05d9

Please sign in to comment.