Skip to content

fix(billing): retain non-zero overage lines settled via credits - #4882

Merged
turip merged 3 commits into
mainfrom
fix/do-not-delete-non-zero-overage-lines
Aug 7, 2026
Merged

turip merged 3 commits into
mainfrom
fix/do-not-delete-non-zero-overage-lines

Conversation

@turip

@turip turip commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Retain invoice lines for non-zero custom-currency overage even when no fiat transaction is required. This supports overage that is settled via credits: the billable line remains available for invoice settlement while payment handling can still be skipped.

Root cause

Line omission was based on NoFiatTransactionRequired, which describes payment handling rather than whether converted fiat overage is zero. As a result, non-zero overage settled via credits could be deleted from the invoice.

Changes

  • Determine zero-overage line omission from converted post-allocation fiat overage.
  • Propagate conversion failures before invoice-line omission or cleanup; stateless guards fail closed.
  • Cover flat-fee and usage-based mapping at gathering preview and collection completion.

Tracking

OM-435

Validation

  • Focused flat-fee and usage-based service unit tests.
  • Focused PostgreSQL-backed lifecycle cases for flat fee, usage based, and progressive usage based.
  • make lint-go-fast (before test-only additions).
  • git diff --check.

Summary by CodeRabbit

  • Bug Fixes

    • Improved custom-currency overage handling based on converted fiat amounts.
    • Correctly omits invoice lines with zero converted overage while retaining billable lines with positive overage.
    • Separates invoice-line visibility from payment requirements.
    • Propagates conversion errors during invoice processing.
  • Documentation

    • Clarified the distinction between invoice settlement and payment requirements.

Greptile Summary

The PR separates invoice-line omission from payment handling so non-zero custom-currency overage remains invoiceable when credits eliminate the required fiat transaction.

  • Calculates omission from converted post-allocation fiat overage for flat-fee and usage-based charges.
  • Propagates conversion errors through line mapping, cleanup, and state updates.
  • Adds focused coverage for zero overage, credit-settled positive overage, and conversion failures.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/billing/charges/flatfee/service/linemapper.go Centralizes converted fiat-overage calculation and uses the converted amount, rather than the payment flag, to control line omission.
openmeter/billing/charges/flatfee/service/creditheninvoice.go Updates zero-overage state transitions to use converted post-allocation fiat overage and propagate conversion errors from transition actions.
openmeter/billing/charges/usagebased/service/linemapper.go Applies the same converted-overage mapping and omission semantics to usage-based invoice lines.
openmeter/billing/charges/usagebased/service/creditheninvoice.go Carries conversion-aware settlement checks through completion, shrink, and zero-overage transitions.
openmeter/billing/charges/usagebased/service/payments.go Makes settlement classification conversion-aware and returns conversion failures to callers.
openmeter/billing/charges/flatfee/service/linemapper_test.go Adds focused flat-fee tests for zero overage, retained credit-settled overage, and conversion failure.
openmeter/billing/charges/usagebased/service/linemapper_test.go Expands usage-based mapping coverage across both line-deletion-capable lifecycle stages.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  R[Realization run totals] --> C[Convert custom-currency overage to fiat]
  C --> Z{Converted fiat overage is zero?}
  Z -->|Yes| O[Omit invoice line and bypass invoice settlement]
  Z -->|No| K[Retain billable invoice line]
  K --> P{Fiat transaction required?}
  P -->|Yes| T[Process fiat payment]
  P -->|No| S[Skip payment handling]
Loading

Reviews (3): Last reviewed commit: "test(billing): assert converted overage ..." | Re-trigger Greptile

Context used:

@coderabbitai

coderabbitai Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Flat-fee and usage-based credit-then-invoice flows now classify zero overage from converted fiat amounts. Invoice-line omission no longer depends on whether a fiat transaction is required. Conversion errors propagate through settlement and line-mapping paths. Tests and documentation cover the updated behavior.

Changes

Custom-currency overage handling

Layer / File(s) Summary
Converted overage mapping
openmeter/billing/charges/flatfee/service/linemapper.go, openmeter/billing/charges/usagebased/service/linemapper.go
Shared helpers convert eligible overage to fiat and provide the currency, amount, and invoice-line omission decision. Mappers use these values for validation, pricing, metadata, and line deletion.
Settlement and cleanup decisions
openmeter/billing/charges/README.md, openmeter/billing/charges/flatfee/service/creditheninvoice.go, openmeter/billing/charges/flatfee/service/lineengine.go, openmeter/billing/charges/usagebased/service/creditheninvoice.go, openmeter/billing/charges/usagebased/service/lineengine.go, openmeter/billing/charges/usagebased/service/payments.go
State guards, payment settlement, shrink handling, and cleanup use converted overage results. Conversion errors propagate. The README documents the distinction between converted overage and required fiat transactions.
Flat-fee behavior validation
openmeter/billing/charges/flatfee/service/linemapper_test.go
Tests cover zero and positive converted overage, conversion errors, and line deletion or retention across preview and collection stages.
Usage-based behavior validation
openmeter/billing/charges/usagebased/service/creditheninvoice_test.go, openmeter/billing/charges/usagebased/service/linemapper_test.go
Tests cover settlement detection, persistence resume, transaction-requirement independence, line deletion, line retention, and missing cost-basis errors.

Estimated code review effort: 3 (Moderate) | ~30 minutes

Possibly related PRs

Suggested reviewers: chrisgacsal

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main fix: retaining non-zero overage invoice lines when credits settle the charge.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/do-not-delete-non-zero-overage-lines

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

Comment @coderabbitai help to get the list of available commands.

@turip turip added release-note/bug-fix Release note: Bug Fixes and removed Bug labels Aug 7, 2026
@turip
turip marked this pull request as ready for review August 7, 2026 13:40
@turip
turip requested a review from a team as a code owner August 7, 2026 13:40

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openmeter/billing/charges/flatfee/service/linemapper_test.go`:
- Around line 35-52: Add conversion-failure test coverage for
isZeroFiatAmountOverageRun and line deletion handling: in
openmeter/billing/charges/flatfee/service/linemapper_test.go:35-52 and
openmeter/billing/charges/usagebased/service/creditheninvoice_test.go:83-100,
assert conversion failure returns false; in
openmeter/billing/charges/flatfee/service/linemapper_test.go:85-131 and
openmeter/billing/charges/usagebased/service/linemapper_test.go:377-415, assert
the conversion error is exposed while the affected line remains undeleted.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 9a31cbab-1887-4175-8f20-98f6d5c3d6f5

📥 Commits

Reviewing files that changed from the base of the PR and between 2ecb2f7 and eb96e3b.

📒 Files selected for processing (6)
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go
  • openmeter/billing/charges/flatfee/service/linemapper_test.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice_test.go
  • openmeter/billing/charges/usagebased/service/linemapper_test.go

@coderabbitai coderabbitai Bot left a comment

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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
openmeter/billing/charges/usagebased/service/linemapper_test.go (1)

372-435: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the converted fiat amount.

The new cases only assert line deletion or retention. A mapper that produces an incorrect nonzero fiat amount still passes. Assert that the flat price and credit-purchase detailed-line fiat amount equal the expected converted value, such as 6 for amount 3 at rate 2.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@openmeter/billing/charges/usagebased/service/linemapper_test.go` around lines
372 - 435, Extend the new subtests around populateStandardLineFromRun to assert
the converted fiat amount on the flat-price and credit-purchase detailed lines,
not only DeletedAt. For the positive converted overage, verify both
detailed-line amounts equal the expected conversion (for example, 6 from amount
3 at rate 2), while preserving the existing deletion and error assertions.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@openmeter/billing/charges/usagebased/service/linemapper_test.go`:
- Around line 372-435: Extend the new subtests around
populateStandardLineFromRun to assert the converted fiat amount on the
flat-price and credit-purchase detailed lines, not only DeletedAt. For the
positive converted overage, verify both detailed-line amounts equal the expected
conversion (for example, 6 from amount 3 at rate 2), while preserving the
existing deletion and error assertions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: b80ba427-c3e8-46f4-8b78-e3b0ea2ff994

📥 Commits

Reviewing files that changed from the base of the PR and between eb96e3b and 1d7762f.

📒 Files selected for processing (10)
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go
  • openmeter/billing/charges/flatfee/service/lineengine.go
  • openmeter/billing/charges/flatfee/service/linemapper.go
  • openmeter/billing/charges/flatfee/service/linemapper_test.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice_test.go
  • openmeter/billing/charges/usagebased/service/lineengine.go
  • openmeter/billing/charges/usagebased/service/linemapper.go
  • openmeter/billing/charges/usagebased/service/linemapper_test.go
  • openmeter/billing/charges/usagebased/service/payments.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • openmeter/billing/charges/flatfee/service/linemapper_test.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice_test.go
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go

@turip
turip enabled auto-merge (squash) August 7, 2026 15:00
@turip
turip merged commit ddf123c into main Aug 7, 2026
27 checks passed
@turip
turip deleted the fix/do-not-delete-non-zero-overage-lines branch August 7, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants