Skip to content

fix(billing): handle custom currency gathering previews - #5167

Merged
turip merged 1 commit into
mainfrom
feat/custom-currency-gathering-preview
Sep 21, 2026
Merged

turip merged 1 commit into
mainfrom
feat/custom-currency-gathering-preview

Conversation

@turip

@turip turip commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Overview

  • resolve custom-currency cost basis read-only when projecting credit-purchase gathering lines
  • omit usage-based custom-currency scheduling placeholders from gathering previews until credit allocation establishes fiat overage
  • keep flat-fee previews on the same omission semantics
  • document the preview contract and cover the three charge types with focused tests

Notes for reviewer

Credit-purchase invoice amounts are directly determined by purchased quantity and cost basis, so they can be projected safely. Flat-fee and usage-based invoice amounts depend on post-allocation overage and are intentionally omitted.

Validation:

  • focused unit tests for cost-basis, credit-purchase, usage-based, and flat-fee packages
  • PostgreSQL-backed preview tests for credit-purchase, flat-fee, and usage-based charges
  • configured Go linter for all touched packages

Summary by CodeRabbit

  • New Features

    • Credit-purchase previews now calculate dynamic custom-currency pricing to show projected fiat amounts and detailed invoice lines.
    • Preview calculations do not persist unresolved pricing or advance charges.
  • Bug Fixes

    • Gathering-invoice previews now omit custom-currency flat-fee and usage-based lines when invoiceable fiat amounts cannot yet be determined.
  • Tests

    • Added coverage for dynamic pricing previews, omitted gathering lines, and preservation of unresolved pricing.

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the changed preview paths preserve read-only behavior and correctly separate projectable credit purchases from unresolved overage placeholders.

Summary

This PR changes custom-currency gathering previews to project credit purchases from their persisted or dynamically resolved cost basis while omitting flat-fee and usage-based placeholders whose fiat overage is not known before credit allocation.

  • Adds a read-only dynamic cost-basis resolver for preview calculations.
  • Uses that resolver for custom-currency credit-purchase previews without persisting charge state.
  • Omits custom-currency usage-based preview placeholders using the existing deleted-line filtering contract.
  • Documents and tests the distinct preview behavior across charge types.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  G[Custom-currency gathering line] --> T{Charge type}
  T -->|Credit purchase| C{Persisted cost basis?}
  C -->|Yes| P[Use persisted snapshot]
  C -->|No, dynamic| R[Resolve rate at service-period start]
  P --> L[Build projected fiat line]
  R --> L
  T -->|Flat fee or usage based| O[Mark placeholder deleted in preview copy]
  O --> F[Preview filtering omits line]
  L --> V[Return read-only gathering preview]
  F --> V
Loading

Reviews (1) · Last reviewed commit: "fix(billing): handle custom currency gat..."

@turip
turip requested a review from a team as a code owner September 21, 2026 09:16
@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: openmeterio/openmeter/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 91e8a64f-8af2-4e48-9cb0-9911eb698c25

📥 Commits

Reviewing files that changed from the base of the PR and between 6d76d8a and 9a10039.

📒 Files selected for processing (7)
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/creditpurchase/service/lineengine.go
  • openmeter/billing/charges/models/costbasis/resolver.go
  • openmeter/billing/charges/models/costbasis/resolver_test.go
  • openmeter/billing/charges/service/creditpurchase_costbasis_test.go
  • openmeter/billing/charges/usagebased/service/lineengine.go
  • test/subscription/custom_currency_billing_test.go

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

Preview generation now resolves dynamic cost bases for credit-purchase projections without persisting them. Custom-currency gathering placeholders are omitted from previews. Tests cover resolver behavior, projected totals, and unresolved charge state.

Changes

Preview cost-basis handling

Layer / File(s) Summary
Preview resolver contract
openmeter/billing/charges/models/costbasis/resolver.go, openmeter/billing/charges/models/costbasis/resolver_test.go
Adds ResolveForPreviewInput and ResolveForPreview. Persisted states return directly. Dynamic intents use the resolver. Unresolved non-dynamic intents return an error.
Credit-purchase preview integration
openmeter/billing/charges/creditpurchase/service/lineengine.go, openmeter/billing/charges/service/creditpurchase_costbasis_test.go
Credit-purchase previews resolve dynamic custom-currency cost bases locally and project the resulting invoice lines. Regular invoice construction does not enable this resolution.
Gathering preview filtering
openmeter/billing/charges/usagebased/service/lineengine.go, test/subscription/custom_currency_billing_test.go, openmeter/billing/charges/README.md
Custom-currency gathering lines are marked deleted and excluded. Integration coverage verifies a zero-total USD preview with no lines and no persisted cost basis.

Priority: ➖ Normal

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

Change: Bug fix

Suggested reviewers: galexihu, chrisgacsal

Sequence Diagram(s)

sequenceDiagram
  participant GatheringPreview
  participant CreditPurchaseLineEngine
  participant CostBasisResolver
  participant Charge
  GatheringPreview->>CreditPurchaseLineEngine: Build preview lines
  CreditPurchaseLineEngine->>CostBasisResolver: Resolve dynamic cost basis
  CostBasisResolver-->>CreditPurchaseLineEngine: Return projected state
  CreditPurchaseLineEngine->>Charge: Apply state for line construction
  CreditPurchaseLineEngine-->>GatheringPreview: Return projected invoice lines
Loading
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
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: fixing custom-currency gathering previews. It matches the implementation and stated objectives.
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.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

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 area/billing labels Sep 21, 2026
@turip
turip merged commit 40c9335 into main Sep 21, 2026
47 of 52 checks passed
@turip
turip deleted the feat/custom-currency-gathering-preview branch September 21, 2026 09:40
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.

3 participants