@@ -67,6 +67,37 @@ func (s *BillingService) UpdateInvoices(ctx context.Context, in *v1.UpdateInvoic
67
67
return & v1.UpdateInvoicesResponse {}, nil
68
68
}
69
69
70
+ func (s * BillingService ) ReconcileInvoices (ctx context.Context , in * v1.ReconcileInvoicesRequest ) (* v1.ReconcileInvoicesResponse , error ) {
71
+ balances , err := db .ListBalance (ctx , s .conn )
72
+ if err != nil {
73
+ log .WithError (err ).Errorf ("Failed to reconcile invoices." )
74
+ return nil , status .Errorf (codes .Internal , "Failed to reconcile invoices." )
75
+ }
76
+
77
+ creditSummaryForTeams := map [string ]stripe.CreditSummary {}
78
+ for _ , balance := range balances {
79
+ entity , id := balance .AttributionID .Values ()
80
+
81
+ // TODO: Support updating of user attribution IDs
82
+ if entity != db .AttributionEntity_Team {
83
+ continue
84
+ }
85
+
86
+ creditSummaryForTeams [id ] = stripe.CreditSummary {
87
+ Credits : int64 (math .Ceil (balance .CreditCents .ToCredits ())),
88
+ ReportID : "no-report" ,
89
+ }
90
+ }
91
+
92
+ err = s .stripeClient .UpdateUsage (ctx , creditSummaryForTeams )
93
+ if err != nil {
94
+ log .WithError (err ).Errorf ("Failed to udpate usage in stripe." )
95
+ return nil , status .Errorf (codes .Internal , "Failed to update usage in stripe" )
96
+ }
97
+
98
+ return & v1.ReconcileInvoicesResponse {}, nil
99
+ }
100
+
70
101
func (s * BillingService ) FinalizeInvoice (ctx context.Context , in * v1.FinalizeInvoiceRequest ) (* v1.FinalizeInvoiceResponse , error ) {
71
102
logger := log .WithField ("invoice_id" , in .GetInvoiceId ())
72
103
0 commit comments