Skip to content

Validate customer billing for invoice-funded credit grants - #4608

Merged
turip merged 1 commit into
mainfrom
feat/customerbilling-credit-grant-validation
Jun 30, 2026
Merged

turip merged 1 commit into
mainfrom
feat/customerbilling-credit-grant-validation

Conversation

@turip

@turip turip commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

What

  • Add shared customer billing app validation for invoicing-capable customer apps.
  • Reuse the shared validator from subscription billing validation.
  • Validate invoice-funded credit grants before creating charge/invoice artifacts, so Stripe billing setups without customer Stripe app data fail immediately.

Why

Invoice-funded credit grants could create pending invoice artifacts for customers whose billing profile uses the Stripe app but whose customer app data is missing. The later invoice issuing path then failed with a Stripe customer precondition error, leaving the grant invoice in a pending state.

Validation

  • go test -tags=dynamic ./openmeter/billing/validators/... ./openmeter/billing/creditgrant/service ./app/common
  • go vet -tags=dynamic ./openmeter/billing/validators/... ./openmeter/billing/creditgrant/service ./app/common ./test/app/stripe ./test/credits
  • env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./test/app/stripe -run TestStripeInvoicing/TestInvoiceFundedCreditGrantRequiresStripeCustomerData
  • env POSTGRES_HOST=127.0.0.1 go test -tags=dynamic ./test/credits -run TestCreditGrantTestSuite/TestCreateInvoiceFundedCreatesInvoiceArtifacts

Summary by CodeRabbit

  • New Features
    • Invoice-funded credit grants now perform stricter billing setup validation before creation, including required invoicing and payment capabilities.
  • Bug Fixes
    • Prevents credit-grant creation when customer invoicing app or required billing data is missing, reducing unintended invoice generation.
    • Subscription billing validation now uses the shared invoicing-capability checks for consistency.
  • Tests
    • Added coverage to ensure invoice-funded credit grants fail when required customer billing data (e.g., for invoicing/Stripe) is incomplete.

@turip
turip requested a review from a team as a code owner June 30, 2026 10:01
@turip turip added the release-note/bug-fix Release note: Bug Fixes label Jun 30, 2026
@greptile-apps

greptile-apps Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR validates billing setup before creating invoice-funded credit grants. The main changes are:

  • Adds a shared customer invoicing app validator.
  • Reuses the validator from subscription billing validation.
  • Wires billing service into credit grant creation.
  • Adds Stripe tests for missing customer billing data.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
openmeter/billing/creditgrant/service/service.go Adds billing service as a required dependency and validates invoice-funded grants before creating billing artifacts.
openmeter/billing/validators/customerbilling/validator.go Adds the shared customer invoicing app validator used by credit grants and subscriptions.
openmeter/billing/validators/subscription/validator.go Moves subscription billing setup validation onto the shared validator.
app/common/creditgrant.go Passes billing service into the credit grant service wiring.
test/app/stripe/invoice_credits_test.go Adds coverage for invoice-funded credit grants with missing Stripe customer data.
test/app/stripe/invoice_test.go Adds credit grant service setup to the Stripe invoice test suite.
test/credits/creditgrant_test.go Updates the credit grant test service setup for the new billing service dependency.

Reviews (2): Last reviewed commit: "Validate customer billing for invoice cr..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 52120f21-4e6c-4d32-bbc3-91a29cb6f499

📥 Commits

Reviewing files that changed from the base of the PR and between a992cdc and ed8d285.

📒 Files selected for processing (7)
  • app/common/creditgrant.go
  • openmeter/billing/creditgrant/service/service.go
  • openmeter/billing/validators/customerbilling/validator.go
  • openmeter/billing/validators/subscription/validator.go
  • test/app/stripe/invoice_credits_test.go
  • test/app/stripe/invoice_test.go
  • test/credits/creditgrant_test.go
🚧 Files skipped from review as they are similar to previous changes (7)
  • app/common/creditgrant.go
  • openmeter/billing/validators/customerbilling/validator.go
  • openmeter/billing/creditgrant/service/service.go
  • test/app/stripe/invoice_credits_test.go
  • test/app/stripe/invoice_test.go
  • openmeter/billing/validators/subscription/validator.go
  • test/credits/creditgrant_test.go

📝 Walkthrough

Walkthrough

Adds a BillingService dependency to the credit grant service and a shared ValidateCustomerInvoicingApp helper. Invoice-funded credit grants now validate invoicing setup through that helper, the subscription validator uses the same path, and tests cover the new wiring and failure case.

Changes

Invoice-funded credit grant billing validation

Layer / File(s) Summary
ValidateCustomerInvoicingApp shared helper
openmeter/billing/validators/customerbilling/validator.go
New helper fetches the customer billing override profile, extracts the invoicing app, and delegates to ValidateCustomer with the requested capabilities.
BillingService wired into credit grant service + Invoice validation
openmeter/billing/creditgrant/service/service.go, app/common/creditgrant.go
BillingService is added to Config, validated, stored, and used in Create to validate invoice-funded grants through the shared helper. Production wiring passes billingRegistry.Billing into the service constructor.
Subscription validator refactored to use shared helper
openmeter/billing/validators/subscription/validator.go
The subscription billing validator replaces its inline invoicing-app validation flow with a single customerbilling.ValidateCustomerInvoicingApp call.
Integration tests for invoice-funded credit grant validation
test/app/stripe/invoice_test.go, test/app/stripe/invoice_credits_test.go, test/credits/creditgrant_test.go
Stripe invoice tests wire in a CreditGrant service, add a failure case for invoice-funded credit grants without Stripe customer data, and update the credit grant test suite to pass BillingService.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

area/billing

Suggested reviewers

  • tothandras

Possibly related PRs

  • openmeterio/openmeter#4166: Both PRs modify openmeter/billing/creditgrant/service/service.go and the credit-grant creation path around billing/charge handling.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 matches the main change: validating customer billing before creating invoice-funded credit grants.
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
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/customerbilling-credit-grant-validation

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/validators/customerbilling/validator.go`:
- Around line 29-33: In validator.go, the customer billing validation path in
the customer profile flow should guard the invoicing app before calling
customerapp.AsCustomerApp, since MergedProfile.Apps.Invoicing may be nil and
AsCustomerApp relies on GetID()/GetType() and can panic. Add a nil check around
appBase in the validator logic, return a clear validation error when invoicing
is missing, and only call customerapp.AsCustomerApp when the app is present.
🪄 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: aeae4854-e8b2-45f5-a4cd-e6aa73013f3c

📥 Commits

Reviewing files that changed from the base of the PR and between 60126ca and a992cdc.

📒 Files selected for processing (7)
  • app/common/creditgrant.go
  • openmeter/billing/creditgrant/service/service.go
  • openmeter/billing/validators/customerbilling/validator.go
  • openmeter/billing/validators/subscription/validator.go
  • test/app/stripe/invoice_credits_test.go
  • test/app/stripe/invoice_test.go
  • test/credits/creditgrant_test.go

@turip
turip force-pushed the feat/customerbilling-credit-grant-validation branch from a992cdc to ed8d285 Compare June 30, 2026 10:12
@turip
turip merged commit bd4d21f into main Jun 30, 2026
26 checks passed
@turip
turip deleted the feat/customerbilling-credit-grant-validation branch June 30, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants