Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stripe] Finalized invoce debits, do not set credits to negative when updating #12787

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd do it the otherway around. Maybe we can create a PR with a migration that negates all credits and changes the respective code as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually thinking about it again, it might be fine as is. After all the table is called usage so positive means used negative means gained. 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can still make the change and drop the usage records manually

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spoke directly, we'll be keeping it as is to more closely align with Stripe which also doesn't want negative credits, but positive credits for usage

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