Skip to content

feat(billing): support usage-based invoice line creates - #4669

Merged
turip merged 2 commits into
mainfrom
feat/usagebased-line-create-support
Jul 10, 2026
Merged

turip merged 2 commits into
mainfrom
feat/usagebased-line-create-support

Conversation

@turip

@turip turip commented Jul 8, 2026 •

Copy link
Copy Markdown
Member

Summary

  • support usage-based charge-backed invoice line creates from API invoice edits

two rules:

  • If a gathering invoice is edited then we create the charge and allow the gathering line to govern it's lifecycle
  • If the standard invoice contains the line create we create a charge and start a final realization run pointed to the current line

Summary by CodeRabbit

  • New Features

    • Added API support for manually creating usage-based invoice lines (gathering and standard) and mapping resulting charges into the invoice lifecycle, returning the created lines.
    • Usage-based invoice-line creates are now supported for managed lines; updates remain unsupported.
    • Manual standard-line attachment now advances charge state and links it to the related realization run.
  • Bug Fixes

    • Improved UnitConfig propagation/usage so it’s preserved from intent through charge creation and metered quantity conversion.
  • Documentation

    • Updated usage-based billing guidance to explicitly describe create behavior and constraints.
  • Tests

    • Added integration coverage for manual gathering and manual standard creation flows.

Greptile Summary

This PR adds support for usage-based invoice line creates in billing. The main changes are:

  • Manual usage-based gathering lines now create charge-backed gathering lines.
  • Manual usage-based standard lines now create a charge and attach a realization run.
  • UnitConfig is carried as line pricing context during usage mapping.
  • Shared charge state-machine construction was simplified across usage-based and flat-fee paths.
  • Integration tests cover manual usage-based gathering and standard invoice creates.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.
  • The usage-based create path keeps UnitConfig on the line before standard-line mapping reads it.
  • The manual charge creation flow matches the invoice mutation lifecycle described by the surrounding code.

Important Files Changed

Filename Overview
openmeter/billing/charges/usagebased/service/lineengine.go Adds manual usage-based create handling for gathering and standard invoice lines.
openmeter/billing/charges/usagebased/service/linemapper.go Maps API-created invoice lines into usage-based intents and reads UnitConfig from the standard line during run mapping.
openmeter/billing/charges/usagebased/service/create.go Copies UnitConfig from usage-based charge intent data onto created gathering lines.
openmeter/billing/gatheringinvoice.go Exposes UnitConfig through gathering invoice line readers.
openmeter/billing/invoiceline.go Extends generic invoice line readers with UnitConfig access.

Reviews (4): Last reviewed commit: "test(billing): cover manual usage line u..." | Re-trigger Greptile

Context used (3)

  • Context used - CLAUDE.md (source)
  • Context used - AGENTS.md (source)
  • Context used - api/spec/AGENTS.md (source)

@turip
turip requested a review from a team as a code owner July 8, 2026 12:30
@turip turip added release-note/feature Release note: Exciting New Features area/billing labels Jul 8, 2026
@turip

turip commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds manual creation of usage-based invoice lines through invoice APIs, persists UnitConfig on invoice lines, and routes flat-fee and usage-based charge operations through charge-scoped state-machine helpers.

Changes

Manual usage-based line creation and state-machine refactor

Layer / File(s) Summary
Contracts and unit configuration
openmeter/billing/charges/statemachine/machine.go, .agents/skills/charges/SKILL.md, openmeter/billing/invoiceline.go, openmeter/billing/gatheringinvoice.go, openmeter/billing/charges/usagebased/service/create.go, openmeter/billing/charges/usagebased/service/linemapper.go
Extends state-machine and invoice-line contracts, documents manual creates, stores unit configuration on gathering lines, and reads standard-line unit configuration during mapping.
Charge-scoped state-machine construction
openmeter/billing/charges/flatfee/service/*, openmeter/billing/charges/usagebased/service/{triggers,payments,lineengine}.go
Centralizes charge-based state-machine creation and updates trigger, payment, edit, delete, preview, and attachment flows to use generic state machines.
Manual usage-based line creation flow
openmeter/billing/charges/usagebased/service/{linemapper,lineengine}.go
Validates created lines, builds manual usage-based intents, creates charges, and attaches results to gathering or standard invoice lines.
Manual creation integration coverage
openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
Tests manual gathering and standard usage-based line creation, charge state, computed values, linkage, and persistence.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant InvoiceAPI
  participant UsageBasedLineEngine
  participant UsageBasedLineMapper
  participant UsageBasedChargeService
  participant ChargeStateMachine

  InvoiceAPI->>UsageBasedLineEngine: Submit created invoice lines
  UsageBasedLineEngine->>UsageBasedLineMapper: Build manual intent
  UsageBasedLineMapper-->>UsageBasedLineEngine: Normalized usage-based intent
  UsageBasedLineEngine->>UsageBasedChargeService: Create charge
  UsageBasedChargeService-->>UsageBasedLineEngine: Created charge
  UsageBasedLineEngine->>ChargeStateMachine: Attach line and advance charge
  ChargeStateMachine-->>UsageBasedLineEngine: Realization state and patches
  UsageBasedLineEngine-->>InvoiceAPI: Return created lines
Loading

Possibly related PRs

Suggested labels: kind/feature

Suggested reviewers: tothandras, GAlexIHU

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 matches the main change: adding support for usage-based invoice line creates.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/usagebased-line-create-support

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/gathering-line-delete to main July 8, 2026 13:05
@turip
turip force-pushed the feat/usagebased-line-create-support branch from f19ead9 to 063fc2c Compare July 8, 2026 13:06
@turip
turip force-pushed the feat/usagebased-line-create-support branch from 063fc2c to 9d51d4e Compare July 10, 2026 05:26
@turip
turip merged commit 7d6a4f7 into main Jul 10, 2026
26 checks passed
@turip
turip deleted the feat/usagebased-line-create-support branch July 10, 2026 07:22
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