Skip to content

test(stripe): reproduce omitted draft invoice recreation - #5540

Draft
turip wants to merge 9 commits into
mainfrom
feat/stripe-omitted-draft-regression
Draft

turip wants to merge 9 commits into
mainfrom
feat/stripe-omitted-draft-regression

Conversation

@turip

@turip turip commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

This draft adds an intentionally failing regression test for the Stripe idempotency concern in #5534. It is stacked on feat/skip-empty-invoices and contains only the reproducer.

Issue

With empty_invoice_mode = omit, an editable draft can become billable, empty, and then billable again:

  1. A draft contains a synthetic $10 fee and a usage line with zero usage. OpenMeter creates Stripe invoice in_A using invoice-create-<OpenMeter invoice ID>.
  2. Removing the fee leaves the zero-usage line and an editable, zero-total OpenMeter draft. Omission deletes in_A and clears its Stripe references.
  3. Adding a synthetic $20 fee makes the same OpenMeter invoice billable again.
  4. CreateInvoice reuses the original creation key. While Stripe retains that key, API v1 replays the original response referencing deleted invoice in_A.
  5. Adding the new line fails with No such invoice; retries repeat the failure while the cached response remains.

Expected: recreation creates a fresh Stripe invoice, while ordinary retries of the same creation continue to reuse their invoice.

Stripe documents that API v1 replays the saved response for a reused idempotency key.

Reproducer

TestRecreateOmittedDraftInvoice calls the production Stripe client against a local HTTP fake that retains creation responses after deletion. It first verifies that an ordinary creation retry reuses the original invoice, then deletes and recreates the draft and expects a new billable line to succeed.

This is a client-level reproduction of the HTTP sequence; it does not drive the billing state machine or call live Stripe. The existing lifecycle tests mock CreateInvoice above this boundary and therefore miss the cached response.

Validation

  • Focused regression test: fails as intended when adding the recreated invoice's line, with No such invoice: 'in_1'; the recreated ID is still in_1.
  • Scoped fast lint: passed.
direnv exec . go test -tags=dynamic ./openmeter/app/stripe/client \
  -run '^TestRecreateOmittedDraftInvoice$' -count=1 -v

The test remains red to demonstrate the issue; this draft does not include a production fix.

RetriggerView in GreptileConfidence Score: 5/5

No additional issues were found beyond the intentionally failing reproducer acknowledged by `turip`.

Summary

Adds a local HTTP test for recreating an omitted Stripe draft.

  • A local Stripe client test covers invoice recreation after omission.

Reviews (1) · Last reviewed commit: "test(stripe): reproduce omitted draft in..." · Reviewed by Greptile

borosr and others added 9 commits November 16, 2026 10:52
Store whether empty invoices (no lines or zero total) should be created in
the invoicing app. The setting defaults to create and is exposed on the v3
billing profile API; an update that does not send it keeps the stored value.

The invoicing apps do not act on the setting yet.
The empty invoice mode checks returned plain errors. They now return generic
validation errors, like the other validators in the repository.
When the workflow uses the omit empty invoice mode and the invoicing app
reports the invoice as empty, billing deletes the app's invoice instead of
syncing or finalizing it, clears the invoice's invoicing reference and records
the outcome as skipped with reason empty_invoice. Nothing is collected for such
an invoice, so billing books authorization and settlement itself and the
invoice ends up paid.
…s omitted

Omitting an empty invoice deletes it in the invoicing app, but only the
invoice's own reference was cleared. Lines, detailed lines and their discounts
kept pointing at the deleted objects, removed lines included. The line upserts
keep a stored reference when a line arrives without one, so the references are
now cleared with a dedicated adapter update next to the in-memory clear.
An invoice that was omitted from Stripe gets a new Stripe invoice once an edit
gives it something to bill, without stale line references, and is then issued
like any other invoice.
Deleting a Stripe invoice that no longer exists failed, which left the
OpenMeter invoice stuck: after a successful delete, a rolled back transaction
restores the Stripe reference and every retry deletes again. A draft removed by
hand in Stripe has the same effect.

The Stripe client now treats a 404 on delete as success and logs a warning.
Every other error still fails, in particular deleting a non-draft invoice.

Open for discussion: the 404 is handled in the client, following DeleteWebhook.
Handling it in the app layer instead would keep the client a thin wrapper and
let the app decide. Also note that a 404 is returned as well when the API key
belongs to another account or mode, which the client cannot tell apart.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

@borosr
borosr force-pushed the feat/skip-empty-invoices branch from 0fe1cd5 to 951b43a Compare November 16, 2026 10:52
@borosr
borosr force-pushed the feat/skip-empty-invoices branch 3 times, most recently from 382d90d to ccd4bed Compare November 17, 2026 11:17
Base automatically changed from feat/skip-empty-invoices to main November 17, 2026 11:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants