A robust billing service implementation using Express.js and Stripe for handling subscriptions, payments, and customer management.
- Subscription management with multiple tiers (Starter, Startup, Advanced, Enterprise)
- Secure payment processing via Stripe Checkout
- Customer portal integration for subscription management
- Webhook handling for Stripe events
- Session management and tracking
- Subscription cancellation
- Detailed subscription status retrieval
- Node.js (v14 or higher)
- npm or yarn
- Stripe account with API keys
- Clone the repository:
git clone https://github.com/yourusername/billing-service.git
cd billing-service
- Install dependencies:
npm install
- Create a
.env
file in the root directory with the following variables:
PORT=3000
STRIPE_SECRET_KEY=your_stripe_secret_key
STRIPE_WEBHOOK_SECRET_KEY=your_webhook_secret
BASE_URL=http://localhost:3000
FALLBACK_URL=http://localhost:3000/billing
The service uses a configuration module (config.ts
) to manage environment variables and Stripe-specific settings. You'll need to configure:
- Stripe API keys
- Webhook secrets
- Base URL for redirects
- Subscription tier pricing IDs
The service supports multiple subscription tiers with corresponding Stripe price IDs:
- Starter
- Startup
- Advanced
- Enterprise
Configure your price IDs in the config.ts
file.
GET /subscribe?tier=<tier_name>
- Creates a new subscription checkout session
- Required query parameter: tier (starter|startup|advanced|enterprise)
- Returns: session ID and checkout URL
GET /success?session_id=<session_id>
- Handles successful subscription checkout
- Redirects to customer portal
GET /customers/:customerId
- Retrieves customer portal session
- Redirects to Stripe customer portal
DELETE /subscriptions/:subscriptionId
- Cancels an active subscription
- Returns: canceled subscription details
GET /subscriptions/session/:sessionId
- Retrieves subscription details for a session
- Returns: subscription status and details
POST /webhook
- Handles Stripe webhook events
- Requires raw body parsing
const response = await fetch('/subscribe?tier=starter');
const { url } = await response.json();
window.location.href = url;
const response = await fetch(`/subscriptions/${subscriptionId}`, {
method: 'DELETE'
});
const canceledSubscription = await response.json();
const response = await fetch(`/subscriptions/session/${sessionId}`);
const subscriptionDetails = await response.json();
The service implements several security measures:
- Webhook signature verification
- Environment variable protection
- Stripe session handling
- Raw body parsing for webhooks
The service includes comprehensive error handling for:
- Invalid subscription tiers
- Failed payment processing
- Invalid session IDs
- Webhook verification failures
- Customer portal access issues
To run the service locally:
npm run dev
For production:
npm run build
npm start