You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(stripe): skip Stripe delete for invoices without synced Stripe ID - #5444
When deleting an invoice that has not yet been synced to Stripe (e.g. pending invoice lines deleted immediately before sync), DeleteStandardInvoice passed an empty Stripe invoice ID to stripeClient.DeleteInvoice. This triggered a validation error from DeleteInvoiceInput.Validate (which requires a non-empty StripeInvoiceID), causing the billing workflow to transition the invoice to delete.failed.
This PR skips the remote Stripe deletion call when invoice.ExternalIDs.Invoicing == "", allowing unsynced invoices to be cleanly deleted locally.
In openmeter/app/stripe/client/invoice.go, DeleteInvoiceInput.Validate enforces that StripeInvoiceID must not be empty:
func (iDeleteInvoiceInput) Validate() error {
ifi.StripeInvoiceID=="" {
returnerrors.New("stripe invoice id is required")
}
returnnil
}
If an invoice was created locally but has not yet synced to Stripe, its invoice.ExternalIDs.Invoicing is empty (""). Attempting to delete the invoice invokes DeleteInvoice, fails input validation with a 400 error, and the billing state machine transitions the invoice to delete.failed instead of deleting it.
Technical Solution
In openmeter/app/stripe/appinvoice.go:
Guard DeleteStandardInvoice with an early return:
ifinvoice.ExternalIDs.Invoicing=="" {
returnnil
}
If the invoice was never synced to Stripe, there is no remote Stripe invoice to delete. Returning nil allows the local billing workflow state machine to delete the invoice without failing on provider sync.
Test Plan
Added TestDeleteStandardInvoice in openmeter/app/stripe/app_test.go verifying that DeleteStandardInvoice returns nil when called with an invoice having an empty ExternalIDs.Invoicing.
Ran targeted unit tests:
go test -v ./openmeter/app/stripe -run "TestDeleteStandardInvoice" (PASS, 0.171s)
go test -v ./openmeter/app/stripe (PASS, 0.377s)
Summary by CodeRabbit
Bug Fixes
Deleting a standard invoice without a Stripe invoice reference now completes successfully without contacting Stripe. This prevents an unnecessary failure when the invoice is not represented in Stripe. Invoices with a Stripe reference continue to follow the existing deletion flow, and their handling remains unchanged.
Confidence Score: 4/5
Handle partially completed Stripe creates before merging, so deletion cannot report success while a Stripe draft remains.
### Issue 1
openmeter/app/stripe/appinvoice.go:76-78
An empty `ExternalIDs.Invoicing` does not always mean no Stripe invoice exists. `createInvoice` creates the Stripe invoice before adding its lines, but returns no result if `AddInvoiceLines` fails. The local invoice keeps an empty ID, and `DraftSyncFailed` allows deletion. This new return then reports deletion as complete while the Stripe draft remains. Previously, deletion stayed failed rather than reporting success.
Save the Stripe ID before the remaining sync steps, or distinguish a never-started sync from a partially completed one before skipping deletion.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Summary
Skips Stripe deletion when the local invoice has no saved Stripe ID.
Stripe invoice deletion skips invoices that have not synced.
When an invoice has not yet been synced to Stripe (e.g. pending lines deleted immediately), its Stripe invoice ID is empty.
Previously, DeleteStandardInvoice passed an empty Stripe invoice ID to stripeClient.DeleteInvoice, which failed validation (DeleteInvoiceInput.Validate requires a non-empty ID) and caused the invoice workflow to transition to delete.failed.
This commit checks if invoice.ExternalIDs.Invoicing is empty in DeleteStandardInvoice, skipping the remote deletion call and returning nil so unsynced invoices can be cleanly deleted locally.
Invoices that were never synced to Stripe can now be deleted locally without a remote deletion call.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title clearly and concisely describes the main change: skipping Stripe deletion when an invoice has no synced Stripe ID.
Linked Issues check
Only #5409 is linked, as a direct link. Its coding requirement: deleting a standard invoice that was never synced to Stripe must succeed, and the invoice must end up deleted without a Stripe call. At …
Out of Scope Changes check
The whole-PR diff against the merge base (675c3b6) contains two changes. (1) A 5-line early return in DeleteStandardInvoice in openmeter/app/stripe/appinvoice.go. This directly implements #5409. (…
Full details: Linked Issues check
Explanation
Only #5409 is linked, as a direct link. Its coding requirement: deleting a standard invoice that was never synced to Stripe must succeed, and the invoice must end up deleted without a Stripe call. At the reviewed head, DeleteStandardInvoice in openmeter/app/stripe/appinvoice.go returns nil early when invoice.ExternalIDs.Invoicing == "". This runs before getStripeClient and before stripeClient.DeleteInvoice, so the empty-ID validation error ("stripe invoice id is required") can no longer occur on this path. Invoices with a Stripe ID still use the existing client lookup and deletion flow. The reproduction case (draft invoice deleted before sync) and the zero-total teardown case both have an empty Stripe invoice ID, so both are covered. TestDeleteStandardInvoice in openmeter/app/stripe/app_test.go checks the no-error result for an unsynced invoice. The PR description's test run results are not verified here, but they are not needed for this verdict. No non-coding requirements exist in #5409.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff against the merge base (675c3b6) contains two changes. (1) A 5-line early return in DeleteStandardInvoice in openmeter/app/stripe/appinvoice.go. This directly implements #5409. (2) TestDeleteStandardInvoice in openmeter/app/stripe/app_test.go. This test covers the new early-return behavior, so it supports the same objective. The existing deleted-app test and other Stripe invoice flows are not changed in a way that conflicts with the objective. I found no unrelated changes.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between 675c3b6 and 4a6e051.
Files selected for processing: 2
openmeter/app/stripe/app_test.go
openmeter/app/stripe/appinvoice.go
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Deletion leaves Stripe drafts behind
An empty ExternalIDs.Invoicing does not always mean no Stripe invoice exists. createInvoice creates the Stripe invoice before adding its lines, but returns no result if AddInvoiceLines fails. The local invoice keeps an empty ID, and DraftSyncFailed allows deletion. This new return then reports deletion as complete while the Stripe draft remains. Previously, deletion stayed failed rather than reporting success.
Save the Stripe ID before the remaining sync steps, or distinguish a never-started sync from a partially completed one before skipping deletion.
Prompt To Fix With AI
This is a comment left during a code review.
Path: openmeter/app/stripe/appinvoice.go
Line: 76-78
Comment:
**Deletion leaves Stripe drafts behind**
An empty `ExternalIDs.Invoicing` does not always mean no Stripe invoice exists. `createInvoice` creates the Stripe invoice before adding its lines, but returns no result if `AddInvoiceLines` fails. The local invoice keeps an empty ID, and `DraftSyncFailed` allows deletion. This new return then reports deletion as complete while the Stripe draft remains. Previously, deletion stayed failed rather than reporting success.
Save the Stripe ID before the remaining sync steps, or distinguish a never-started sync from a partially completed one before skipping deletion.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Summary
When deleting an invoice that has not yet been synced to Stripe (e.g. pending invoice lines deleted immediately before sync),
DeleteStandardInvoicepassed an empty Stripe invoice ID tostripeClient.DeleteInvoice. This triggered a validation error fromDeleteInvoiceInput.Validate(which requires a non-emptyStripeInvoiceID), causing the billing workflow to transition the invoice todelete.failed.This PR skips the remote Stripe deletion call when
invoice.ExternalIDs.Invoicing == "", allowing unsynced invoices to be cleanly deleted locally.Fixes #5409.
Problem & Root Cause
In
openmeter/app/stripe/appinvoice.go,DeleteStandardInvoiceinvokesstripeClient.DeleteInvoicewithinvoice.ExternalIDs.Invoicing:In
openmeter/app/stripe/client/invoice.go,DeleteInvoiceInput.Validateenforces thatStripeInvoiceIDmust not be empty:If an invoice was created locally but has not yet synced to Stripe, its
invoice.ExternalIDs.Invoicingis empty (""). Attempting to delete the invoice invokesDeleteInvoice, fails input validation with a 400 error, and the billing state machine transitions the invoice todelete.failedinstead of deleting it.Technical Solution
In
openmeter/app/stripe/appinvoice.go:DeleteStandardInvoicewith an early return:nilallows the local billing workflow state machine to delete the invoice without failing on provider sync.Test Plan
TestDeleteStandardInvoiceinopenmeter/app/stripe/app_test.goverifying thatDeleteStandardInvoicereturnsnilwhen called with an invoice having an emptyExternalIDs.Invoicing.go test -v ./openmeter/app/stripe -run "TestDeleteStandardInvoice"(PASS, 0.171s)go test -v ./openmeter/app/stripe(PASS, 0.377s)Summary by CodeRabbit
Handle partially completed Stripe creates before merging, so deletion cannot report success while a Stripe draft remains.
Fix with agent prompt
Summary
Skips Stripe deletion when the local invoice has no saved Stripe ID.
Reviews (1) · Last reviewed commit: "fix(stripe): skip Stripe delete for invo..." · Reviewed by Greptile