Skip to content

fix(stripe): skip Stripe delete for invoices without synced Stripe ID - #5444

Merged
turip merged 1 commit into
openmeterio:mainfrom
Tyagiquamar:fix/delete-unsynced-stripe-invoice
Nov 5, 2026
Merged

turip merged 1 commit into
openmeterio:mainfrom
Tyagiquamar:fix/delete-unsynced-stripe-invoice

Conversation

@Tyagiquamar

@Tyagiquamar Tyagiquamar commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

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.

Fixes #5409.

Problem & Root Cause

In openmeter/app/stripe/appinvoice.go, DeleteStandardInvoice invokes stripeClient.DeleteInvoice with invoice.ExternalIDs.Invoicing:

return stripeClient.DeleteInvoice(ctx, stripeclient.DeleteInvoiceInput{
    StripeInvoiceID: invoice.ExternalIDs.Invoicing,
})

In openmeter/app/stripe/client/invoice.go, DeleteInvoiceInput.Validate enforces that StripeInvoiceID must not be empty:

func (i DeleteInvoiceInput) Validate() error {
    if i.StripeInvoiceID == "" {
        return errors.New("stripe invoice id is required")
    }
    return nil
}

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:
    if invoice.ExternalIDs.Invoicing == "" {
        return nil
    }
  • 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.

RetriggerView in GreptileConfidence Score: 4/5

Handle partially completed Stripe creates before merging, so deletion cannot report success while a Stripe draft remains.

Fix All in Claude CodeFindings

  1. P1 Deletion leaves Stripe drafts behind ▶
Fix with agent prompt
### 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.

Reviews (1) · Last reviewed commit: "fix(stripe): skip Stripe delete for invo..." · Reviewed by Greptile

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.
@Tyagiquamar
Tyagiquamar requested a review from a team as a code owner November 5, 2026 06:25
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

DeleteStandardInvoice now returns successfully when an invoice has no Stripe invoicing external ID. A test covers this case.

Changes: Standard invoice deletion

Layer / File(s) Summary
Skip Stripe deletion when the external ID is absent
openmeter/app/stripe/appinvoice.go, openmeter/app/stripe/app_test.go
DeleteStandardInvoice returns nil when the invoice has no Stripe invoicing external ID. A test checks that the call returns no error.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: kind/bug, release-note/bug-fix, area/billing

Suggested reviewers: turip, galexihu, borosr

Merge Risk: Minimal · up to 4a6e0

Invoices that were never synced to Stripe can now be deleted locally without a remote deletion call.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: skipping Stripe deletion when an invoice has no synced Stripe ID.
Linked Issues check Passed 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 Passed 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.

Share

Recent review info

Run configuration

Commits

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.

Comment on lines +76 to +78
// If the invoice has not been synced to Stripe yet, there is no Stripe invoice to delete.
if invoice.ExternalIDs.Invoicing == "" {
return nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Fix in Claude Code Fix in Codex

@turip turip self-assigned this Nov 5, 2026
@turip turip added the release-note/bug-fix Release note: Bug Fixes label Nov 5, 2026

@turip turip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@turip
turip merged commit 74cb4ed into openmeterio:main Nov 5, 2026
32 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting an invoice not yet synced to Stripe fails

2 participants