Fix invoice.upcoming webhook handler failure - generate unique document ID for invoices without id #706
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
invoice.upcoming
webhook event was failing with the error:This occurred because upcoming invoices from Stripe don't have an
id
field, causing the Firestore document path to be invalid when trying to create the invoice document.Root Cause
In the
insertInvoiceRecord
function inutils.ts
, the code was attempting to useinvoice.id
directly as the Firestore document ID:For
invoice.upcoming
events, Stripe doesn't provide anid
field since these are preview/draft invoices that haven't been finalized yet.Solution
The fix generates a unique document ID for upcoming invoices when
invoice.id
is null/undefined:Additionally, the fix prevents creating unnecessary payment records for upcoming invoices when they don't have a payment intent, since upcoming invoices are previews and don't represent actual payments:
Impact
invoice.upcoming
eventsTest Cases Handled
upcoming_{subscription}_{timestamp}
), skips payment record if no payment_intentFixes #616
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.