Repository navigation
fix: interim payment errors for progressively billed usage-based items - #4665
Conversation
📝 WalkthroughWalkthroughThis PR removes the shared ChangesPayment settlement state advancement
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant LineEngine
participant Charge
participant StateMachine
LineEngine->>Charge: RefetchCharge / update realization run
LineEngine->>StateMachine: AdvanceUntilStateStable
StateMachine->>StateMachine: Permit TriggerNext -> StatusFinal
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
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/usagebased/service/payments.go`:
- Around line 103-109: The payment-settled advancement in payments.go is too
broad because AdvanceUntilStateStable on advancementStateMachine can fire
non-settlement TriggerNext transitions as well. Add a status guard around the
generic advancement path in the payment-settlement callback so it only runs when
the charge is in the settlement state, keeping the state machine advancement
focused on settlement finalization and avoiding unintended lifecycle moves for
older or parallel lines.
🪄 Autofix (Beta)
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
Run ID: 144790c1-5213-4eb6-9791-ff3bffc43c9a
📒 Files selected for processing (6)
openmeter/billing/charges/flatfee/service/creditheninvoice.goopenmeter/billing/charges/flatfee/service/lineengine.goopenmeter/billing/charges/meta/triggers.goopenmeter/billing/charges/service/invoicable_test.goopenmeter/billing/charges/usagebased/service/creditheninvoice.goopenmeter/billing/charges/usagebased/service/payments.go
💤 Files with no reviewable changes (2)
- openmeter/billing/charges/meta/triggers.go
- openmeter/billing/charges/flatfee/service/creditheninvoice.go
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
test/credits/credit_then_invoice_test.go (2)
4219-4221: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winStale "given" comment contradicts the updated assertion.
The
givencomment says "the charge is awaiting payment settlement," but line 4204 now assertsusagebased.StatusFinal. This comment should reflect the finalized state.📝 Suggested comment update
s.Run("when the charge shrink patch targets the immutable invoice period", func() { // given: - // - the invoice line is immutable and the charge is awaiting payment settlement + // - the invoice line is immutable and the charge is finalized without fiat payment🤖 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 `@test/credits/credit_then_invoice_test.go` around lines 4219 - 4221, The scenario setup comment in the `s.Run("when the charge shrink patch targets the immutable invoice period", ...)` test is stale and no longer matches the assertion state. Update the `given` comment near the immutable invoice period setup so it reflects that the charge is finalized (`usagebased.StatusFinal`) instead of saying it is awaiting payment settlement, keeping the test narrative aligned with the actual setup.
4608-4615: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winStale subtest name and "then" comment contradict the updated assertion.
The subtest is named "given a final standard invoice line awaiting payment settlement" and the
thencomment says "the charge is waiting for payment settlement," but line 4683 now assertsusagebased.StatusFinal. These should be updated to match the new no-fiat finalization behavior.📝 Suggested comment updates
- s.Run("given a final standard invoice line awaiting payment settlement", func() { + s.Run("given a final standard invoice line after no-fiat finalization", func() { // given: // - a ledger-backed customer has enough credits to cover usage // - usage is visible inside the original service period // when: // - the standard invoice is collected and approved // then: - // - the final run has invoice usage booked and the charge is waiting for payment settlement + // - the final run has invoice usage booked and the charge is finalized without fiat payment🤖 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 `@test/credits/credit_then_invoice_test.go` around lines 4608 - 4615, The subtest name and the trailing “then” comment in the credits finalization test no longer match the actual assertion in this scenario. Update the test description around the subtest in credit_then_invoice_test.go to reflect the new no-fiat finalization behavior and align it with the `usagebased.StatusFinal` assertion in the final run, so the name and comments describe that the usage-based charge is finalized rather than waiting for payment settlement.
🤖 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 `@test/credits/credit_then_invoice_test.go`:
- Around line 4219-4221: The scenario setup comment in the `s.Run("when the
charge shrink patch targets the immutable invoice period", ...)` test is stale
and no longer matches the assertion state. Update the `given` comment near the
immutable invoice period setup so it reflects that the charge is finalized
(`usagebased.StatusFinal`) instead of saying it is awaiting payment settlement,
keeping the test narrative aligned with the actual setup.
- Around line 4608-4615: The subtest name and the trailing “then” comment in the
credits finalization test no longer match the actual assertion in this scenario.
Update the test description around the subtest in credit_then_invoice_test.go to
reflect the new no-fiat finalization behavior and align it with the
`usagebased.StatusFinal` assertion in the final run, so the name and comments
describe that the usage-based charge is finalized rather than waiting for
payment settlement.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 7d3a4627-5d8a-41c4-ae71-7562540878ea
📒 Files selected for processing (1)
test/credits/credit_then_invoice_test.go
Summary
TriggerNextadvancement pathall_payments_settledtrigger now that flat-fee and usage-based settlement both use normal charge advancementWhy
Progressively billed usage-based items can produce interim invoice lines that are fully covered by credits and do not require a fiat payment transaction. Those lines were able to leave the charge waiting for payment settlement even though there was no payment to settle, which could surface as payment-processing errors for interim invoices.
The charge state machine already has the correct guarded advancement rules. This change records the line payment state first, then lets the standard advancement loop settle the charge into the correct final state when all invoiced usage is settled or no fiat transaction is required.
Validation
go test -tags=dynamic -count=1 ./openmeter/billing/charges/flatfee/servicego test -tags=dynamic -count=1 ./openmeter/billing/charges/usagebased/serviceenv POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 ./openmeter/billing/charges/service -run 'TestInvoicable|TestUsageBased'env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 ./test/creditsmake test-nocacheSummary by CodeRabbit
Bug Fixes
Tests
Greptile Summary
This PR updates payment settlement for credit-then-invoice charges. The main changes are:
all_payments_settledtrigger with guardedTriggerNextadvancement.Confidence Score: 5/5
This looks safe to merge.
Important Files Changed
TriggerNexttransition.all_payments_settledtrigger constant.Reviews (2): Last reviewed commit: "test(billing): expect no-fiat usage char..." | Re-trigger Greptile
Context used (3)