Skip to content

[billing] Implement ReconcileInvoices #12712

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

Merged
merged 1 commit into from
Sep 7, 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
31 changes: 31 additions & 0 deletions components/usage/pkg/apiv1/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ func (s *BillingService) UpdateInvoices(ctx context.Context, in *v1.UpdateInvoic
return &v1.UpdateInvoicesResponse{}, nil
}

func (s *BillingService) ReconcileInvoices(ctx context.Context, in *v1.ReconcileInvoicesRequest) (*v1.ReconcileInvoicesResponse, error) {
balances, err := db.ListBalance(ctx, s.conn)
if err != nil {
log.WithError(err).Errorf("Failed to reconcile invoices.")
return nil, status.Errorf(codes.Internal, "Failed to reconcile invoices.")
}

creditSummaryForTeams := map[string]stripe.CreditSummary{}
Copy link
Member

Choose a reason for hiding this comment

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

Can we generalize and talk about attributeId instead of teams?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this requires a couple of changes. For one we need to start setting the attributionId: <value> on stripe customers rather than the teamId we set now

for _, balance := range balances {
entity, id := balance.AttributionID.Values()

// TODO: Support updating of user attribution IDs
if entity != db.AttributionEntity_Team {
Copy link
Member

Choose a reason for hiding this comment

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

why do we need this exclusion here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Legacy of the focus on team based UBP first.

continue
}

creditSummaryForTeams[id] = stripe.CreditSummary{
Credits: int64(math.Ceil(balance.CreditCents.ToCredits())),
ReportID: "no-report",
Copy link
Member Author

Choose a reason for hiding this comment

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

This will be removed later, once we remove the other usage of the ReportID, for now it doesn't hurt us in any way as we do not use it during the UsageUpdate invoked below.

}
}

err = s.stripeClient.UpdateUsage(ctx, creditSummaryForTeams)
Copy link
Member

Choose a reason for hiding this comment

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

If I read correctly here, we might fail to update if the very first query is for a costCenter without stripe account, no?

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 fail if we fail to lookup customers from stripe. But we then iterate over the stripe customers which actually exist rather than over all the attribution IDs so in this case it would not fail on the first which doesn't have a stripe account.

if err != nil {
log.WithError(err).Errorf("Failed to udpate usage in stripe.")
return nil, status.Errorf(codes.Internal, "Failed to update usage in stripe")
}

return &v1.ReconcileInvoicesResponse{}, nil
}

func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInvoiceRequest) (*v1.FinalizeInvoiceResponse, error) {
logger := log.WithField("invoice_id", in.GetInvoiceId())

Expand Down
12 changes: 0 additions & 12 deletions components/usage/pkg/stripe/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,6 @@ func (c *Client) updateUsageForCustomer(ctx context.Context, customer *stripe.Cu
return nil, fmt.Errorf("failed to register usage for customer %q on subscription item %s", customer.Name, subscriptionItemId)
}

invoice, err := c.GetUpcomingInvoice(ctx, customer.ID)
if err != nil {
return nil, fmt.Errorf("failed to find upcoming invoice for customer %s: %w", customer.ID, err)
}

_, err = c.UpdateInvoiceMetadata(ctx, invoice.ID, map[string]string{
ReportIDMetadataKey: summary.ReportID,
})
if err != nil {
return nil, fmt.Errorf("failed to udpate invoice %s metadata with report ID: %w", invoice.ID, err)
}

Comment on lines -155 to -166
Copy link
Member Author

Choose a reason for hiding this comment

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

Dropping this part as

  1. It already doesn't work as we can't update an UpcomingInvoice in Stripe with metadata
  2. We're no longer planning to use the metadata for sharing the report

return &UsageRecord{
SubscriptionItemID: subscriptionItemId,
Quantity: summary.Credits,
Expand Down