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

[billing] Implement ReconcileInvoices #12712

Merged
merged 1 commit into from
Sep 7, 2022
Merged

Conversation

easyCZ
Copy link
Member

@easyCZ easyCZ commented Sep 6, 2022

Description

Implements reconciliation of usage balances against Stripe.

Related Issue(s)

How to test

Release Notes

NONE

Documentation

Werft options:

  • /werft with-preview

Comment on lines -155 to -166
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)
}

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

@easyCZ easyCZ force-pushed the mp/billing-reconcile-invoices branch from 7d9e602 to 5a2c563 Compare September 7, 2022 06:50
@easyCZ easyCZ changed the base branch from main to mp/billing-reconcile-invoices-proto September 7, 2022 06:51
@easyCZ easyCZ force-pushed the mp/billing-reconcile-invoices-proto branch from 5eb6411 to 33d67f5 Compare September 7, 2022 06:51
@easyCZ easyCZ force-pushed the mp/billing-reconcile-invoices branch from 5a2c563 to 559b00a Compare September 7, 2022 06:52
@roboquat roboquat added size/M and removed size/XXL labels Sep 7, 2022
Base automatically changed from mp/billing-reconcile-invoices-proto to main September 7, 2022 07:55
@roboquat roboquat added size/XXL and removed size/M labels Sep 7, 2022
@easyCZ easyCZ force-pushed the mp/billing-reconcile-invoices branch from 559b00a to 34ef9cb Compare September 7, 2022 07:57
@easyCZ easyCZ marked this pull request as ready for review September 7, 2022 07:57
@easyCZ easyCZ requested a review from a team September 7, 2022 07:57
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Sep 7, 2022

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.

Copy link
Member

@svenefftinge svenefftinge left a comment

Choose a reason for hiding this comment

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

LGTM. My concern regarding generalization to attributionIds (or accounts, or costcenters) can be discussed and tackled in a follow-up PR.

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

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.

@roboquat roboquat merged commit 52279b1 into main Sep 7, 2022
@roboquat roboquat deleted the mp/billing-reconcile-invoices branch September 7, 2022 12:34
@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed: webapp Meta team change is running in production deployed Change is completely running in production release-note-none size/M team: webapp Issue belongs to the WebApp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants