Skip to content

improvement(billing): add billing enforcement for webhook executions, consolidate helpers#975

Merged
waleedlatif1 merged 5 commits into
stagingfrom
fix/billing
Aug 15, 2025
Merged

improvement(billing): add billing enforcement for webhook executions, consolidate helpers#975
waleedlatif1 merged 5 commits into
stagingfrom
fix/billing

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

  • fix(billing): clinet-side envvar for billing
  • remove unrelated files
  • fix(billing): add billing enforcement for webhook executions, consolidate implementation

Summary

add billing enforcement for webhook executions, consolidate helpers

Type of Change

  • New feature

Testing

Tested manually.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel Bot commented Aug 15, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
sim Ready Ready Preview Comment Aug 15, 2025 7:34pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs Skipped Skipped Aug 15, 2025 7:34pm

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR introduces comprehensive billing enforcement for webhook executions and consolidates billing-related utilities across the codebase. The primary change adds a new Phase 4 usage limit check to the webhook trigger route that validates user subscription limits before executing webhooks, preventing users from exceeding their plan limits through external webhook calls.

The consolidation effort involves several key architectural improvements:

  1. Environment Variable Migration: Moves billing configuration from server-side BILLING_ENABLED to client-side NEXT_PUBLIC_BILLING_ENABLED, enabling billing checks in browser contexts. The getEnv() function is enhanced to provide universal environment variable access with Docker runtime support.

  2. Centralized Billing Logic: Replaces inline environment variable checks throughout the UI components (sidebar, settings modal) with a unified isBillingEnabled helper from @/lib/environment. This ensures consistent billing status determination across the application.

  3. Code Deduplication: Extracts the checkInternalApiKey authentication function into a shared utility (@/lib/copilot/utils.ts), eliminating duplicate authentication code in multiple API routes.

  4. Usage Monitoring Refactor: Moves usage monitoring logic from lib/usage-monitor.ts to lib/billing/calculations/usage-monitor.ts and introduces centralized getUserUsageLimit() function, simplifying subscription plan handling.

The webhook billing enforcement follows the existing architectural patterns by adding the usage check as a separate phase after rate limiting but before task queuing. It returns HTTP 200 responses (to prevent webhook provider retries) while blocking execution when limits are exceeded, with provider-specific response formatting for Microsoft Teams.

Confidence score: 4/5

  • This PR is safe to merge with moderate confidence, introducing proper billing enforcement without breaking existing functionality
  • Score reflects well-structured consolidation with comprehensive test coverage, but some complexity in environment variable handling and potential edge cases in webhook provider responses
  • Pay close attention to apps/sim/lib/environment.ts and apps/sim/app/api/webhooks/trigger/[path]/route.ts for the core billing logic changes

13 files reviewed, 2 comments

Edit Code Review Bot Settings | Greptile

Comment thread apps/sim/lib/environment.ts Outdated
Comment thread apps/sim/lib/copilot/utils.ts
@vercel vercel Bot temporarily deployed to Preview – docs August 15, 2025 19:25 Inactive
@waleedlatif1 waleedlatif1 merged commit f1fe2f5 into staging Aug 15, 2025
3 of 4 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/billing branch August 15, 2025 19:28
arenadeveloper02 pushed a commit to arenadeveloper02/p2-sim that referenced this pull request Sep 19, 2025
… consolidate helpers (simstudioai#975)

* fix(billing): clinet-side envvar for billing

* remove unrelated files

* fix(billing): add billing enforcement for webhook executions, consolidate implementation

* cleanup

* add back server envvar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant