Skip to content

Commit 6e06398

Browse files
committed
[stripe] Handle invoice finalization
1 parent 75cf6ff commit 6e06398

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

components/usage/pkg/apiv1/billing.go

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ func (s *BillingService) UpdateInvoices(ctx context.Context, in *v1.UpdateInvoic
5656
func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInvoiceRequest) (*v1.FinalizeInvoiceResponse, error) {
5757
log.Infof("Finalizing invoice for invoice %q", in.GetInvoiceId())
5858

59+
if in.GetInvoiceId() == "" {
60+
return nil, status.Errorf(codes.InvalidArgument, "Missing InvoiceID")
61+
}
62+
63+
invoiceID := in.GetInvoiceId()
64+
65+
invoice, err := s.stripeClient.GetInvoice(ctx, invoiceID)
66+
if err != nil {
67+
return nil, status.Errorf(codes.NotFound, "Failed to get invoice with ID %s: %s", invoiceID, err.Error())
68+
}
69+
70+
reportID := invoice.Metadata["reportId"]
71+
fmt.Println(reportID)
72+
5973
return &v1.FinalizeInvoiceResponse{}, nil
6074
}
6175

components/usage/pkg/stripe/stripe.go

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package stripe
77
import (
88
"context"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"os"
1213
"strings"
@@ -16,6 +17,10 @@ import (
1617
"github.com/stripe/stripe-go/v72/client"
1718
)
1819

20+
var (
21+
NotFoundError = errors.New("not found")
22+
)
23+
1924
type Client struct {
2025
sc *client.API
2126
}
@@ -205,6 +210,22 @@ func (c *Client) GetUpcomingInvoice(ctx context.Context, customerID string) (*In
205210
}, nil
206211
}
207212

213+
func (c *Client) GetInvoice(ctx context.Context, invoiceID string) (*stripe.Invoice, error) {
214+
if invoiceID == "" {
215+
return nil, fmt.Errorf("no invoice ID specified")
216+
}
217+
218+
invoice, err := c.sc.Invoices.Get(invoiceID, &stripe.InvoiceParams{
219+
Params: stripe.Params{
220+
Context: ctx,
221+
},
222+
})
223+
if err != nil {
224+
return nil, fmt.Errorf("failed to get invoice %s: %w", invoiceID, err)
225+
}
226+
return invoice, nil
227+
}
228+
208229
// queriesForCustomersWithTeamIds constructs Stripe query strings to find the Stripe Customer for each teamId
209230
// It returns multiple queries, each being a big disjunction of subclauses so that we can process multiple teamIds in one query.
210231
// `clausesPerQuery` is a limit enforced by the Stripe API.

0 commit comments

Comments
 (0)