Overview
The Billing service provides:- Stripe Webhooks: Process payment and subscription events from Stripe
- Event Handlers: Specialized handlers for different Stripe event types
- Provider Support: Managed Service Provider (MSP) billing operations
- Invoice Processing: Handle invoices, payments, and refunds
- Subscription Management: Track subscription lifecycle events
- Background Jobs: Scheduled billing operations and maintenance
Architecture
Configuration
Fromsrc/Billing/Startup.cs:32:
Service Configuration
Stripe Webhook Endpoint
Fromsrc/Billing/Controllers/StripeController.cs:38:
Webhook Handler
POST /stripe/webhook?key={webhook_key}
Authentication: Webhook key verification
Content-Type: application/json
Event Handlers
The Billing service implements specialized handlers for different Stripe event types:Subscription Events
customer.subscription.deleted
Handle subscription cancellation and cleanup
customer.subscription.updated
Process subscription plan changes and upgrades
invoice.upcoming
Preview upcoming invoice and notify user
invoice.finalized
Finalize invoice before payment attempt
Payment Events
charge.succeeded
Process successful payment
charge.refunded
Handle payment refunds
payment_intent.succeeded
Confirm payment intent completion
payment_intent.payment_failed
Handle failed payment attempts
Customer Events
customer.updated
Sync customer data changes
payment_method.attached
Track new payment method additions
setup_intent.succeeded
Confirm payment method setup
coupon.deleted
Handle promotional code deletion
Event Processing Flow
Event Processor
Subscription Lifecycle
Subscription Created
Handled through API service during organization upgrade.Subscription Updated
1
Receive Event
Stripe sends
customer.subscription.updated webhook2
Extract Changes
Parse subscription changes (plan, quantity, status)
3
Update Database
Sync changes to organization subscription record
4
Notify User
Send email notification if plan changed
Subscription Canceled
From subscription deleted handler:Invoice Processing
Payment Succeeded
When an invoice is paid successfully:Payment Succeeded Handler
Payment Failed
When payment fails:1
Receive Webhook
invoice.payment_failed event received2
Update Organization
Mark organization as at-risk or disabled based on retry count
3
Notify User
Send payment failed email with retry information
4
Log Event
Record payment failure in event log
Provider Billing
The service supports Managed Service Provider (MSP) billing:Provider Event Service
- Consolidated billing for multiple client organizations
- Per-seat pricing
- Automatic client organization synchronization
- Volume-based discounts
Background Jobs
Fromsrc/Billing/Startup.cs:107:
Job Configuration
- Invoice Finalization: Prepare invoices before payment
- Usage Tracking: Sync seat usage with Stripe
- Failed Payment Retry: Monitor and retry failed payments
- Subscription Cleanup: Remove expired subscriptions
PayPal Integration
The service includes PayPal IPN (Instant Payment Notification) support:PayPal Client
PayPal integration is deprecated in favor of Stripe for new subscriptions.
Middleware Pipeline
Fromsrc/Billing/Startup.cs:123:
Request Pipeline
Stripe Facade
The service uses a facade pattern for Stripe API interactions:Stripe Facade
- Centralized error handling
- Retry logic for transient failures
- Logging and instrumentation
- Testability through mocking
Security
Webhook Signature Verification
Signature Verification
API Version Validation
Fromsrc/Billing/Controllers/StripeController.cs:57:
Version Check
Cloud Region Validation
For multi-region deployments:Region Validation
Deployment
Environment Variables
Docker
Stripe Webhook Configuration
1
Create Webhook
In Stripe Dashboard, create webhook endpoint
2
Set URL
https://billing.bitwarden.com/stripe/webhook?key={webhook_key}3
Select Events
Subscribe to relevant event types
4
Copy Secret
Save webhook signing secret to configuration
5
Test Webhook
Send test event to verify configuration
Monitoring
Health Checks
Webhook Monitoring
Monitor webhook processing:- Event processing success rate
- Average processing time
- Failed event count
- Retry queue depth
Stripe Dashboard
Use Stripe Dashboard to:- View webhook delivery status
- Retry failed webhooks
- Monitor API usage
- Track subscription metrics
Troubleshooting
Common Issues
Debug Logging
Best Practices
- Idempotency: Handle duplicate webhook events gracefully
- Async Processing: Process webhooks asynchronously when possible
- Error Handling: Return 200 OK even for handled errors
- Timeout: Process webhooks within 30 seconds to avoid retries
- Logging: Log all webhook events for audit trail
- Testing: Use Stripe CLI for local webhook testing
Related Services
- API Service - Subscription management endpoints
- Admin Service - Manual billing operations