Skip to content

feat(flatfee): settle custom currency overage with fiat credits - #4935

Merged
turip merged 1 commit into
mainfrom
feat/flatfee-fiat-overage-credit-settlement
Aug 14, 2026
Merged

turip merged 1 commit into
mainfrom
feat/flatfee-fiat-overage-credit-settlement

Conversation

@turip

@turip turip commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • reconcile flat-fee charge-currency credits while the realization run is mutable
  • allocate settlement-fiat credits once against the final converted overage during invoice finalization
  • persist fiat-overage realizations and project them onto the invoice line without removing a fully credited positive overage
  • preserve allocation lineage and avoid reallocating fiat credits when invoice issuing is retried
  • keep production ledger-backed fiat allocation and correction handlers explicitly unsupported

Behavior

Custom-currency flat-fee credit_then_invoice runs now keep charge-currency and settlement-fiat credit facts separate. Draft invoice lines show the gross converted overage. At invoice finalization, eligible fiat credits are allocated once and applied to the existing line, leaving only the uncovered fiat total collectible. Mutable rerating settles the final charge-currency overage before fiat allocation, and pre-finalization deletion corrects charge-currency realizations while fiat history remains empty.

Jira: OM-435

Validation

  • POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/billing/charges/flatfee/...
  • POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/billing/charges/service -run 'TestInvoicableCharges/TestFlatFeeCustomCurrency' -count=1
  • POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./openmeter/ledger/chargeadapter
  • nix develop --impure .#ci -c make lint-go-fast

Summary by CodeRabbit

  • New Features

    • Added settlement-currency credit allocation for eligible custom-currency flat-fee overages during invoice finalization.
    • Invoice totals now reflect converted overages, applied credits, and only the remaining collectible amount.
    • Added support for correcting overage credits when charges or realization runs change.
  • Bug Fixes

    • Improved retry behavior for invoice synchronization and deferred allocations during rerating.
    • Corrected credit handling for deleted or modified runs.

Greptile Summary

This PR separates charge-currency and settlement-fiat credit handling for custom-currency flat-fee charges, allocating fiat credits during invoice finalization while preserving realization lineage and retry behavior.

  • Adds settlement-fiat allocation and correction contracts for flat-fee handlers.
  • Reconciles mutable runs in charge currency before converting their final overage.
  • Projects gross converted overage and applied fiat credits onto finalized invoice lines.
  • Adds persistence, test-handler, and lifecycle coverage for fiat-overage realizations.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/billing/charges/flatfee/service/realizations/credits.go Adds separate settlement-fiat allocation and correction handlers with persisted realization lineage.
openmeter/billing/charges/flatfee/service/lineengine.go Allocates fiat credits at invoice finalization and projects their effect onto the existing invoice line.
openmeter/billing/charges/flatfee/service/realizations/reconcile.go Centralizes rated-run reconciliation so mutable rerating settles charge-currency facts before fiat conversion.
openmeter/billing/charges/flatfee/service/linemapper.go Maps gross converted overage and settlement-fiat credits into invoice totals without dropping fully credited positive lines.
openmeter/ledger/chargeadapter/flatfee.go Keeps production ledger-backed flat-fee fiat allocation and correction explicitly unsupported.

Sequence Diagram

sequenceDiagram
  participant Billing as Invoice finalization
  participant Engine as Flat-fee line engine
  participant Realizations as Realization service
  participant Credits as Fiat credit handler
  participant Invoice as Finalized invoice line
  Billing->>Engine: Finalize custom-currency line
  Engine->>Realizations: Reconcile charge-currency run
  Realizations-->>Engine: Final custom-currency overage
  Engine->>Engine: Convert overage to settlement fiat
  Engine->>Realizations: Allocate fiat-overage credits once
  Realizations->>Credits: Allocate eligible fiat credits
  Credits-->>Realizations: Allocation realizations
  Realizations-->>Engine: Persisted fiat allocation
  Engine->>Invoice: Gross overage + credits applied
  Invoice-->>Billing: Uncovered collectible total
Loading

Reviews (2): Last reviewed commit: "feat(flatfee): settle custom currency ov..." | Re-trigger Greptile

Context used:

@turip
turip requested a review from a team as a code owner August 14, 2026 08:25
@turip turip added area/billing release-note/feature Release note: Exciting New Features labels Aug 14, 2026
@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 833d087c-9fd7-4bbb-977c-90da5b406446

📥 Commits

Reviewing files that changed from the base of the PR and between 402b590 and b32e4d1.

📒 Files selected for processing (17)
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/flatfee/handler.go
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go
  • openmeter/billing/charges/flatfee/service/creditsonly.go
  • openmeter/billing/charges/flatfee/service/lineengine.go
  • openmeter/billing/charges/flatfee/service/linemapper.go
  • openmeter/billing/charges/flatfee/service/realizations/correct.go
  • openmeter/billing/charges/flatfee/service/realizations/credits.go
  • openmeter/billing/charges/flatfee/service/realizations/creditsonly.go
  • openmeter/billing/charges/flatfee/service/realizations/credittheninvoice.go
  • openmeter/billing/charges/flatfee/service/realizations/preview.go
  • openmeter/billing/charges/flatfee/service/realizations/reconcile.go
  • openmeter/billing/charges/flatfee/service/realizations/service.go
  • openmeter/billing/charges/service/handlers_test.go
  • openmeter/billing/charges/service/invoicable_test.go
  • openmeter/billing/charges/testutils/handlers.go
  • openmeter/ledger/chargeadapter/flatfee.go
💤 Files with no reviewable changes (2)
  • openmeter/billing/charges/flatfee/service/realizations/correct.go
  • openmeter/billing/charges/flatfee/service/realizations/creditsonly.go

📝 Walkthrough

Walkthrough

The flat-fee billing flow now supports settlement-fiat overage allocation at invoice finalization. It centralizes rated-run reconciliation, adds allocation and correction handlers, updates invoice lines, simplifies realization-run APIs, and expands lifecycle tests for retries, rerating, deletion, and persistence.

Changes

Flat-fee settlement-fiat flow

Layer / File(s) Summary
Contracts and realization-run API
openmeter/billing/charges/flatfee/handler.go, openmeter/billing/charges/flatfee/service/realizations/*.go
Adds validation and handler methods for settlement-fiat allocation and correction. Realization methods return flatfee.RealizationRun directly.
Rated-run and credit reconciliation
openmeter/billing/charges/flatfee/service/realizations/reconcile.go, openmeter/billing/charges/flatfee/service/realizations/credits.go, openmeter/billing/charges/flatfee/service/realizations/service.go, openmeter/billing/charges/flatfee/service/{creditsonly,creditheninvoice,lineengine}.go
Centralizes rated-run reconciliation, persists credit lineage, allocates settlement-fiat overage, and corrects settlement-fiat before charge-currency realizations.
Invoice-finalization line updates
openmeter/billing/charges/flatfee/service/lineengine.go, openmeter/billing/charges/flatfee/service/linemapper.go
Invoice finalization allocates eligible fiat overage credits and repopulates custom-currency invoice lines with updated applied credits and totals.
Lifecycle validation and handler support
openmeter/billing/charges/service/invoicable_test.go, openmeter/billing/charges/service/handlers_test.go, openmeter/billing/charges/testutils/handlers.go, openmeter/ledger/chargeadapter/flatfee.go, openmeter/billing/charges/README.md
Adds lifecycle coverage for allocation timing, retries, rerating, deletion, and persistence. Documents the settlement contract and adds test and unsupported adapter callbacks.

Estimated code review effort: 5 (Critical) | ~120 minutes

Merge Risk: ⚪ Minimal · up to b32e4

The PR adds fiat-credit settlement for custom-currency flat-fee overages while preserving allocation lineage and retry behavior; no actionable merge-blocking risk remains after normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant InvoiceFinalization
  participant Realizations
  participant FiatCreditHandler
  participant LineMapper
  InvoiceFinalization->>Realizations: AllocateFiatOverageCredits
  Realizations->>FiatCreditHandler: OnAllocateFiatOverageCredits
  FiatCreditHandler-->>Realizations: credit allocations
  Realizations-->>InvoiceFinalization: updated charge and run
  InvoiceFinalization->>LineMapper: populate invoice_finalizing line
  LineMapper-->>InvoiceFinalization: updated invoice line
Loading
🚥 Pre-merge checks | ✅ 4
✅ 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 and concisely describes the main change: settling custom-currency flat-fee overages with fiat credits.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/flatfee-fiat-overage-credit-settlement

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.

Base automatically changed from feat/fiat-overage-credit-settlement to main August 14, 2026 12:50
@turip
turip force-pushed the feat/flatfee-fiat-overage-credit-settlement branch from b32e4d1 to 633ccbd Compare August 14, 2026 12:58
@turip
turip requested a review from GAlexIHU August 14, 2026 13:08
@turip
turip enabled auto-merge (squash) August 14, 2026 13:31
@turip
turip merged commit 20a0eca into main Aug 14, 2026
27 checks passed
@turip
turip deleted the feat/flatfee-fiat-overage-credit-settlement branch August 14, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants