Skip to content

feat(credit): credit purchase charges - #3925

Merged
GAlexIHU merged 7 commits into
mainfrom
feat/credit-purchase-charges-rebased
Mar 5, 2026
Merged

GAlexIHU merged 7 commits into
mainfrom
feat/credit-purchase-charges-rebased

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

Overview

rewrote history on #3916
keeping original branch as is just to be safe

Fixes #(issue)

Notes for reviewer

Summary by CodeRabbit

  • New Features

    • Added support for external payment settlements in credit purchase transactions.
    • Introduced payment state management for credit purchases with authorization and settlement tracking.
  • Improvements

    • Enhanced credit purchase charge handling with improved payment workflow management.
    • Restructured payment settlement data models for better consistency across invoice and credit purchase types.

@GAlexIHU
GAlexIHU requested a review from a team as a code owner March 5, 2026 14:30
@GAlexIHU GAlexIHU added the release-note/feature Release note: Exciting New Features label Mar 5, 2026
@coderabbitai

coderabbitai Bot commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR extends the billing charges system with comprehensive credit purchase and external payment settlement support, introducing new adapter interfaces, orchestrators, service methods, domain models, and corresponding database schema migrations to handle credit purchase lifecycle management and payment state transitions.

Changes

Cohort / File(s) Summary
Adapter Interface & Base
openmeter/billing/charges/adapter.go
Added new CreditPurchaseAdapter and ExternalPaymentSettlementAdapter interfaces to the public Adapter. Refactored CreateStandardInvoicePaymentSettlement to accept a single input struct instead of multiple parameters.
Adapter Implementations
openmeter/billing/charges/adapter/charge.go, adapter/creditpurchase.go, adapter/external.go, adapter/stdinvoice.go
Implemented new adapter methods: UpdateCreditPurchaseCharge, CreateExternalPaymentSettlement, and UpdateExternalPaymentSettlement. Updated CreateStandardInvoicePaymentSettlement signature and validation. Added helper updateChargeIntent for consolidated charge updates.
Adapter Helpers & Mapping
openmeter/billing/charges/adapter/create.go, adapter/mapper.go, adapter/faltfee.go, adapter/get.go
Refactored adapter methods to use consolidated helper functions. Enhanced mapping logic for external payment settlements and credit grant realizations. Updated charge query expansion to include external payment settlements.
Charge Domain Models
openmeter/billing/charges/chargecreditpurchase.go, charges/charge.go, charges/chargeflatfee.go, charges/chargeusagebased.go
Added ErrorAttributes() method to charge types. Renamed PaymentSettlementStatus to CreditPurchaseInitialPaymentSettlementStatus and updated constants. Replaced ExternalAuthorizedCreditPurchaseSettlement with ExternalCreditPurchaseSettlement. Refactored CreditPurchaseState to use CreditGrantRealization and ExternalPaymentSettlement instead of status. Introduced CreditPurchaseOrchestrator and CreditPurchaseHandler interfaces.
Payment Settlement Models
openmeter/billing/charges/payment.go, charges/stdinvoice.go, charges/errors.go
Introduced new PaymentSettlementBase struct with validation, ExternalPaymentSettlement with nested settlement info, and ExternalPaymentSettlementCreateInput. Refactored StandardInvoicePaymentSettlement to embed PaymentSettlementBase. Added payment state error codes for authorization and settlement conflicts.
Service Interface & Configuration
openmeter/billing/charges/service.go, service/service.go
Embedded new CreditPurchaseService interface. Updated handlers to include CreditPurchaseHandler. Wired creditPurchaseOrchestrator and renamed flatFeeService to flatFeeOrchestrator throughout.
Service Implementations
openmeter/billing/charges/service/creditpurchase.go, service/creditpurchase/creditpurchase.go, service/creditpurchase/promotional.go, service/creditpurchase/external.go
Implemented credit purchase service with UpdateExternalCreditPurchasePaymentState method. Added CreditPurchaseOrchestrator with PostCreate flow routing to promotional or external handlers. Implemented state-driven external payment settlement handling with authorization and settlement workflows.
Service Updates
openmeter/billing/charges/service/flatfee/payment.go, service/invoice.go, service/invoicependinglines.go, service/create.go, service/flatfee/flatfee.go
Refactored flat-fee payment authorization/settlement flows to use new settlement adapter methods with explicit state management. Updated invoice handling to use renamed orchestrator. Restructured post-create flow with result accumulator pattern for charge consolidation.
Test Infrastructure
openmeter/billing/charges/service/handlers_test.go, service/service_test.go
Added creditPurchaseTestHandler implementing CreditPurchaseHandler. Introduced countedLedgerTransactionCallback helper for tracking invocations. Added comprehensive tests for promotional and external credit purchase flows with multiple payment state transitions.
Database Schema
openmeter/ent/schema/charges.go
Introduced ChargeExternalPaymentSettlement entity with settlement fields (status, amount, service period, authorized/settled transaction groups). Added fields to ChargeCreditPurchase (credit grant transaction group, granted timestamp, external settlement link). Restructured ChargeStandardInvoicePaymentSettlement (removed charge_id, changed status type). Added foreign key between flat fees and standard invoice payment settlements.
Database Migrations
tools/migrate/migrations/20260305140658_billing-charges-credit-purchase.up.sql, .../...down.sql
Created charge_external_payment_settlements table with nested transaction group tracking. Extended charge_credit_purchases with grant tracking and external settlement link. Removed charge_id from charge_standard_invoice_payment_settlements. Added linkage between flat fees and payment settlements.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Service
    participant Orchestrator
    participant Adapter
    participant Handler
    participant Database

    Client->>Service: UpdateExternalCreditPurchasePaymentState (Authorized)
    Service->>Service: Validate input
    Service->>Adapter: Get charge by ID with realizations
    Adapter->>Database: Query charge + credit purchase
    Database-->>Adapter: Charge entity
    Adapter-->>Service: CreditPurchaseCharge
    
    Service->>Orchestrator: HandleExternalCreditPurchasePaymentAuthorized
    Orchestrator->>Handler: OnCreditPurchasePaymentAuthorized
    Handler-->>Orchestrator: LedgerTransactionGroupReference
    
    Orchestrator->>Adapter: CreateExternalPaymentSettlement
    Adapter->>Database: Insert settlement with authorized status
    Database-->>Adapter: ExternalPaymentSettlement
    Adapter-->>Orchestrator: Settlement created
    
    Orchestrator->>Adapter: UpdateCreditPurchaseCharge
    Adapter->>Database: Update charge + link settlement
    Database-->>Adapter: Updated charge
    Adapter-->>Orchestrator: CreditPurchaseCharge
    
    Orchestrator-->>Service: CreditPurchaseCharge
    Service-->>Client: Success
Loading
sequenceDiagram
    participant Client
    participant Service
    participant Orchestrator
    participant Adapter
    participant Handler
    participant Database

    Client->>Service: Create CreditPurchaseCharge (Promotional)
    Service->>Orchestrator: PostCreate
    Orchestrator->>Handler: OnPromotionalCreditPurchase
    Handler-->>Orchestrator: LedgerTransactionGroupReference
    
    Orchestrator->>Orchestrator: Set CreditGrantRealization with reference + timestamp
    Orchestrator->>Orchestrator: Set charge status to Final
    
    Orchestrator->>Adapter: UpdateCreditPurchaseCharge
    Adapter->>Database: Update charge with grant realization
    Database-->>Adapter: Updated charge
    Adapter-->>Orchestrator: CreditPurchaseCharge
    
    Orchestrator-->>Service: CreditPurchaseCharge
    Service-->>Client: Charge created with grant realization
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • tothandras
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat(credit): credit purchase charges' directly matches the PR's main objective of implementing credit purchase charge functionality across the billing system.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/credit-purchase-charges-rebased

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 and usage tips.

@GAlexIHU
GAlexIHU enabled auto-merge (squash) March 5, 2026 14:34
@GAlexIHU
GAlexIHU merged commit 6faf0bc into main Mar 5, 2026
26 of 27 checks passed
@GAlexIHU
GAlexIHU deleted the feat/credit-purchase-charges-rebased branch March 5, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants