-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{} | ||
for _, balance := range balances { | ||
entity, id := balance.AttributionID.Values() | ||
|
||
// TODO: Support updating of user attribution IDs | ||
if entity != db.AttributionEntity_Team { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this exclusion here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropping this part as
|
||
return &UsageRecord{ | ||
SubscriptionItemID: subscriptionItemId, | ||
Quantity: summary.Credits, | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 theteamId
we set now