Skip to content

[usage] Fix stripe invoice finalization #13039

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 16, 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
13 changes: 7 additions & 6 deletions components/usage/pkg/apiv1/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInv
return nil, status.Errorf(codes.InvalidArgument, "Missing InvoiceID")
}

invoice, err := s.stripeClient.GetInvoice(ctx, in.GetInvoiceId())
invoice, err := s.stripeClient.GetInvoiceWithCustomer(ctx, in.GetInvoiceId())
if err != nil {
logger.WithError(err).Error("Failed to retrieve invoice from Stripe.")
return nil, status.Errorf(codes.NotFound, "Failed to get invoice with ID %s: %s", in.GetInvoiceId(), err.Error())
}

subscription := invoice.Subscription
if subscription == nil {
logger.Error("No subscription information available for invoice.")
return nil, status.Errorf(codes.Internal, "Failed to retrieve subscription details from invoice.")
customer := invoice.Customer
if customer == nil {
logger.Error("No customer information available for invoice.")
return nil, status.Errorf(codes.Internal, "Failed to retrieve customer details from invoice.")
}
logger = logger.WithField("stripe_customer", customer.ID).WithField("stripe_customer_name", customer.Name)

teamID, found := subscription.Metadata[stripe.AttributionIDMetadataKey]
teamID, found := customer.Metadata[stripe.AttributionIDMetadataKey]
if !found {
logger.Error("Failed to find teamID from subscription metadata.")
return nil, status.Errorf(codes.Internal, "Failed to extra teamID from Stripe subscription.")
Expand Down
4 changes: 2 additions & 2 deletions components/usage/pkg/stripe/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ func (c *Client) GetUpcomingInvoice(ctx context.Context, customerID string) (*In
}, nil
}

func (c *Client) GetInvoice(ctx context.Context, invoiceID string) (*stripe.Invoice, error) {
func (c *Client) GetInvoiceWithCustomer(ctx context.Context, invoiceID string) (*stripe.Invoice, error) {
if invoiceID == "" {
return nil, fmt.Errorf("no invoice ID specified")
}

invoice, err := c.sc.Invoices.Get(invoiceID, &stripe.InvoiceParams{
Params: stripe.Params{
Context: ctx,
Expand: []*string{stripe.String("data.subscriptions")},
Expand: []*string{stripe.String("customer")},
},
})
if err != nil {
Expand Down