frontier icon indicating copy to clipboard operation
frontier copied to clipboard

GetUpcomingInvoice returns empty invoice object instead of 404 for offline billing accounts

Open whoAbhishekSah opened this issue 3 months ago • 0 comments

Problem

The GetUpcomingInvoice API handler currently returns a nil/empty invoice object when no billing customer provider is found or the account is offline. This results in an unhelpful response with all fields set to empty values:

{
    "invoice": {
        "id": "",
        "customer_id": "",
        "provider_id": "",
        "state": "",
        "currency": "",
        "amount": "0",
        "hosted_url": "",
        "due_date": null,
        "effective_at": null,
        "period_start_at": null,
        "period_end_at": null,
        "metadata": {},
        "created_at": null,
        "customer": null
    }
}

Expected Behavior

The API should return either:

  1. An empty response body {}, or
  2. A 404 Not Found status when the billing customer is not created or offline

Root Cause

In billing/invoice/service.go:322-325, when there's no provider ID, the service returns an empty Invoice{} instead of an error:

if custmr.ProviderID == "" {
    logger.Debug(fmt.Sprintf("no customer provider id found"))
    return Invoice{}, nil
}

This empty invoice is then transformed and returned by the handler, creating the confusing response.

Impact

This forces the UI to implement workarounds. For example, in sdks/js/packages/core/react/components/organization/billing/upcoming-billing-cycle.tsx:126, the code has to check for !!billingAccount?.providerId to prevent fetching the upcoming invoice for offline billing accounts:

enabled:
  !!activeOrganization?.id &&
  // This is to prevent fetching the upcoming invoice for offline billing accounts
  !!billingAccount?.providerId,

Once this is fixed, this UI workaround can be removed.

Files to Modify

  • billing/invoice/service.go:322-325 - Handle the case where provider ID is missing
  • internal/api/v1beta1connect/billing_invoice.go - Update handler to return appropriate response
  • sdks/js/packages/core/react/components/organization/billing/upcoming-billing-cycle.tsx:126 - Remove workaround check after API is fixed

whoAbhishekSah avatar Dec 05 '25 06:12 whoAbhishekSah