Skip to content

feat(ent): Add credit purchase changes#3973

Merged
mark-vass-konghq merged 1 commit intofeat/credit-purchase-invoicefrom
feat/credit-purchase-invoice-db
Mar 19, 2026
Merged

feat(ent): Add credit purchase changes#3973
mark-vass-konghq merged 1 commit intofeat/credit-purchase-invoicefrom
feat/credit-purchase-invoice-db

Conversation

@mark-vass-konghq
Copy link
Contributor

@mark-vass-konghq mark-vass-konghq commented Mar 19, 2026

Add schema related changes to credit purchase invoicing

Summary by CodeRabbit

Release Notes

  • New Features

    • Credit purchases can now be settled through invoices with automatic tracking, including draft invoice creation, approval workflows, and payment settlement state management.
    • Added invoice ID support to payment settlement records for enhanced billing audit trails.
  • Tests

    • Added comprehensive test coverage for invoice-based credit purchase settlement workflows.

@mark-vass-konghq mark-vass-konghq self-assigned this Mar 19, 2026
@mark-vass-konghq mark-vass-konghq added the release-note/feature Release note: Exciting New Features label Mar 19, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 19, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 03b06f4f-50f8-4dd8-94be-b02139df8ea5

📥 Commits

Reviewing files that changed from the base of the PR and between 68a9222 and 7b4ef79.

⛔ Files ignored due to path filters (43)
  • openmeter/ent/db/billinginvoiceline.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline/billinginvoiceline.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/billinginvoiceline_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase/chargecreditpurchase.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchase_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchaseinvoicedpayment.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchaseinvoicedpayment/chargecreditpurchaseinvoicedpayment.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchaseinvoicedpayment/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchaseinvoicedpayment_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchaseinvoicedpayment_delete.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchaseinvoicedpayment_query.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargecreditpurchaseinvoicedpayment_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeflatfeepayment.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeflatfeepayment/chargeflatfeepayment.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeflatfeepayment/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeflatfeepayment_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeflatfeepayment_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeusagebasedrunpayment.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeusagebasedrunpayment/chargeusagebasedrunpayment.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeusagebasedrunpayment/where.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeusagebasedrunpayment_create.go is excluded by !**/ent/db/**
  • openmeter/ent/db/chargeusagebasedrunpayment_update.go is excluded by !**/ent/db/**
  • openmeter/ent/db/client.go is excluded by !**/ent/db/**
  • openmeter/ent/db/cursor.go is excluded by !**/ent/db/**
  • openmeter/ent/db/ent.go is excluded by !**/ent/db/**
  • openmeter/ent/db/entmixinaccessor.go is excluded by !**/ent/db/**
  • openmeter/ent/db/expose.go is excluded by !**/ent/db/**
  • openmeter/ent/db/hook/hook.go is excluded by !**/ent/db/**
  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • openmeter/ent/db/mutation.go is excluded by !**/ent/db/**
  • openmeter/ent/db/paginate.go is excluded by !**/ent/db/**
  • openmeter/ent/db/predicate/predicate.go is excluded by !**/ent/db/**
  • openmeter/ent/db/runtime.go is excluded by !**/ent/db/**
  • openmeter/ent/db/setorclear.go is excluded by !**/ent/db/**
  • openmeter/ent/db/tx.go is excluded by !**/ent/db/**
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (7)
  • openmeter/billing/charges/creditpurchase/chargecreditpurchase.go
  • openmeter/billing/charges/models/payment/invoiced.go
  • openmeter/billing/charges/service/creditpurchase_test.go
  • openmeter/ent/schema/billing.go
  • openmeter/ent/schema/chargescreditpurchase.go
  • tools/migrate/migrations/20260319100453_add-credit-charges-changes.down.sql
  • tools/migrate/migrations/20260319100453_add-credit-charges-changes.up.sql

📝 Walkthrough

Walkthrough

This PR extends the credit purchase billing flow to support invoice settlement, adding an InvoiceSettlement field to the credit purchase state, introducing required InvoiceID tracking in the invoiced payment model, creating a new database entity for tracking invoiced credit purchase payments, and validating the integrated flow with a comprehensive test.

Changes

Cohort / File(s) Summary
Credit Purchase State
openmeter/billing/charges/creditpurchase/chargecreditpurchase.go
Added optional InvoiceSettlement *payment.Invoiced field to State struct for tracking invoice-based settlement, though validation logic remains absent for this field.
Payment Model & Validation
openmeter/billing/charges/models/payment/invoiced.go
Introduced required InvoiceID field to both InvoicedCreate and Invoiced structs with corresponding validation checks, extended InvoicedCreator and InvoicedGetter interfaces with ID setter/getter methods, and updated factory function to propagate the invoice ID.
Database Schema & Relationships
openmeter/ent/schema/billing.go, openmeter/ent/schema/chargescreditpurchase.go
Created new ChargeCreditPurchaseInvoicedPayment schema entity with unique cascade edges to invoice lines and credit purchases; updated ChargeCreditPurchaseExternalPayment with explicit mixins and edge declarations; added invoiced_payment edge to ChargeCreditPurchase.
Database Migrations
tools/migrate/migrations/20260319100453_add-credit-charges-changes.*.sql
Up migration creates charge_credit_purchase_invoiced_payments table with required schema and foreign key constraints, adds invoice_id columns to flat-fee and usage-based payment tables; down migration reverses all changes.
Integration Tests
openmeter/billing/charges/service/creditpurchase_test.go
Added comprehensive TestStandardInvoiceCreditPurchase test validating the end-to-end invoice settlement flow from credit purchase creation through approval and payment settlement.

Sequence Diagram

sequenceDiagram
    participant Test as Test Suite
    participant Service as Credit Purchase Service
    participant BillingService as Billing Service
    participant CustomInvoicing as Custom Invoicing Service
    participant Database as Database

    Test->>Service: Create credit purchase<br/>(InvoiceSettlement intent)
    Service->>Database: Insert ChargeCreditPurchase<br/>with invoiced_payment
    Service-->>Test: onCreditPurchaseInitiated callback<br/>(SettlementTypeExternal)
    
    Test->>BillingService: ApproveInvoice
    BillingService->>Database: Update invoice status<br/>to approved
    BillingService-->>Test: Invoice approved
    Service-->>Test: onCreditPurchasePaymentAuthorized<br/>(CreditGrantRealization set)
    
    Test->>CustomInvoicing: HandlePaymentTrigger<br/>(TriggerPaid)
    CustomInvoicing->>Database: Record payment settlement
    CustomInvoicing-->>Test: Payment processed
    Service-->>Test: onCreditPurchasePaymentSettled<br/>(ExternalPaymentSettlement set,<br/>ChargeStatusFinal)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #3960: Modifies the same creditpurchase test files and test suite setup infrastructure.
  • #3770: Updates the same BillingInvoiceLine.Edges() function to add related invoice entity edges.
  • #3943: Introduces the foundational creditpurchase package and invoiced payment models/schema that this PR builds upon.

Suggested labels

area/billing

Suggested reviewers

  • tothandras
  • turip
  • GAlexIHU
✨ 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/credit-purchase-invoice-db
📝 Coding Plan
  • Generate coding plan for human review comments

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.

@mark-vass-konghq mark-vass-konghq force-pushed the feat/credit-purchase-invoice-db branch from 482b94c to 13e8abf Compare March 19, 2026 10:07
@mark-vass-konghq mark-vass-konghq changed the base branch from main to feat/credit-purchase-invoice March 19, 2026 10:08
@mark-vass-konghq mark-vass-konghq force-pushed the feat/credit-purchase-invoice-db branch from 13e8abf to 52a6e97 Compare March 19, 2026 10:10
@mark-vass-konghq mark-vass-konghq changed the base branch from feat/credit-purchase-invoice to main March 19, 2026 10:11
@mark-vass-konghq mark-vass-konghq force-pushed the feat/credit-purchase-invoice-db branch from 52a6e97 to 7b4ef79 Compare March 19, 2026 10:25
@mark-vass-konghq mark-vass-konghq changed the base branch from main to feat/credit-purchase-invoice March 19, 2026 10:25
@mark-vass-konghq mark-vass-konghq marked this pull request as ready for review March 19, 2026 11:41
@mark-vass-konghq mark-vass-konghq requested a review from a team as a code owner March 19, 2026 11:41
@mark-vass-konghq mark-vass-konghq merged commit 5cafbf2 into feat/credit-purchase-invoice Mar 19, 2026
7 of 8 checks passed
@mark-vass-konghq mark-vass-konghq deleted the feat/credit-purchase-invoice-db branch March 19, 2026 11:42
turip added a commit that referenced this pull request Mar 19, 2026
Co-authored-by: Peter Turi <peter.turi@openmeter.io>
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.

2 participants