Skip to content

fix(billing): preserve payment authorizations on invoice retry - #5229

Merged
turip merged 1 commit into
mainfrom
feat/billing-authorization-retry
Oct 2, 2026
Merged

turip merged 1 commit into
mainfrom
feat/billing-authorization-retry

Conversation

@turip

@turip turip commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Overview

Invoice authorization can commit the first line's payment before a later line callback fails. Retrying the invoice replays all lines, and the existing payment previously blocked advancement.

Flat-fee, usage-based, and credit-purchase charges now acknowledge an existing valid, non-deleted payment when its namespace, invoice, line, and amount match. The original authorization and ledger references are preserved, including when the payment has already settled. Credit purchases apply this check before replaying the authorization state transition. Conflicting or invalid payment records remain errors.

Notes for reviewer

Service period is deliberately excluded from the retry identity check. Separate integration tests cover all three charge types with distinct $5 and $10 lines. They reproduce partial progress through the invoice app callback boundary, retry through the billing service, and verify that the first booking remains unchanged while exactly one authorization is added for the second line.

Validation

  • Authorization guard tests for all three charge types pass, covering matching authorized/settled payments, differing service periods, conflicts, and malformed records.
  • The full credit-then-invoice test suite passes with local PostgreSQL, including all three partial-progress retry regressions.
  • Scoped make lint-go-fast passes for the affected charge services and credit tests.

Summary by CodeRabbit

  • Bug Fixes
    • Invoice payment authorization retries now safely reuse an existing active payment when it matches the invoice line and amount, avoiding duplicate bookings.
    • Conflicting or invalid payment details continue to produce errors.
    • Fixed retry handling across flat-fee, usage-based, and credit-purchase charges so completed line bookings are preserved while missing authorizations are added.

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no actionable defect was established.

Summary

The PR makes invoice authorization retries recognize matching existing payments for flat-fee, usage-based, and credit-purchase charges.

  • Preserves the original authorization and ledger references instead of booking them again.
  • Adds guard tests and partial-progress invoice retry regressions for all three charge types.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Invoice authorization callback] --> B{Charge type}
  B --> C[Flat-fee or usage-based run]
  B --> D[Credit purchase]
  C --> E{Existing valid matching payment?}
  D --> E
  E -- Yes --> F[Keep payment and authorization references]
  E -- No payment --> G[Book authorization]
  E -- Conflicting or invalid --> H[Return error]
Loading

Reviews (1) · Last reviewed commit: "fix(billing): preserve payment authoriza..."

@turip
turip requested a review from a team as a code owner October 2, 2026 07:45
@coderabbitai

coderabbitai Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

🧰 Additional context used
📚 Code guidelines (1)
AGENTS.md — auto-discovered

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: 19bf3b3c-3b25-4ef9-8ad9-16026163a1b7

📥 Commits

Reviewing files that changed from the base of the PR and between 72c0e47 and b50e02c.

📒 Files selected for processing (10)
  • openmeter/billing/charges/README.md
  • openmeter/billing/charges/creditpurchase/service/invoice.go
  • openmeter/billing/charges/creditpurchase/service/invoice_test.go
  • openmeter/billing/charges/creditpurchase/service/realizations/service.go
  • openmeter/billing/charges/flatfee/service/payment.go
  • openmeter/billing/charges/flatfee/service/payment_test.go
  • openmeter/billing/charges/usagebased/service/run/payment.go
  • openmeter/billing/charges/usagebased/service/run/payment_test.go
  • test/credits/base.go
  • test/credits/credit_then_invoice_payment_retry_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Invoice authorization retries now accept an existing active payment when its namespace, invoice, line, and amount match. The change covers flat-fee, usage-based, and credit-purchase charges, with unit and integration tests for matching replays, conflicts, and retry results.

Changes

Payment Authorization Retries

Layer / File(s) Summary
Replay validation across charge types
openmeter/billing/charges/README.md, openmeter/billing/charges/flatfee/service/*, openmeter/billing/charges/usagebased/service/run/*, openmeter/billing/charges/creditpurchase/service/*
The three authorization paths accept matching existing bookings and reject conflicting or invalid bookings. Unit tests cover matching authorized or settled payments, mismatches, and invalid data. The README describes the retry behavior.
Partial invoice retry integration coverage
test/credits/base.go, test/credits/credit_then_invoice_payment_retry_test.go
Integration tests inject an authorization failure during invoice processing, then retry flat-fee, usage-based, and credit-purchase invoices. They check preserved bookings and realizations, invoice state, and ledger transactions.

Priority: ➖ Normal

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

Change: Bug fix

Suggested reviewers: mark-vass-konghq

Merge Risk: ⚪ Minimal · up to b50e0

This change makes invoice authorization retries safely reuse matching existing payments, with conflicts and invalid records still rejected. No merge-blocking concerns remain.

Security Architecture Review

Security architecture risk: 🔵 Low · up to b50e0

Retries reuse a valid payment only when its namespace, invoice, line and amount match, preserving the original booking rather than authorizing it again. No introduced attack path was demonstrated. Remaining uncertainty concerns transaction boundaries and lifecycle recovery during interruptions or concurrent retries.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed acceptance decision is bounded to an existing invoice-linked payment in the supplied charge or realization run, with namespace, invoice, line and amount equality required. These checks constrain replay identity but do not independently establish permission to invoke invoice authorization.

Trust Boundaries and Controls

  • observed — The inspected billing caller locks by customer ID and namespace before loading and manipulating the invoice state machine. The lock is acquired inside the transaction wrapper, providing serialization counterevidence against concurrent retries through this caller.

Resilience and Maintainability Implications

  • observed — The historical ledger wraps group creation and postings in the same transaction framework that retrieves an existing driver from context. Repository helpers also reuse an ambient driver. This supports transaction propagation, but the inspected evidence does not complete verification of nested commits, charge-state persistence or callback-success/payment-insert-failure recovery.
🚥 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: preserving payment authorizations during invoice retries.
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
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


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 Oct 2, 2026
@turip
turip merged commit 482d55e into main Oct 2, 2026
@turip
turip deleted the feat/billing-authorization-retry branch October 2, 2026 08:14
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