Complete backend system for managing SaaS subscriptions with Stripe payments and Dahila integration.
β Stripe Integration
- Payment Intent creation and management
- Customer management
- Subscription handling
- Webhook support for real-time events
- Invoice management
β Dahila Integration
- Master Key authentication
- Product management
- Order management
- Invoice generation
- Dashboard statistics
β User Management
- User registration and login
- JWT authentication
- Profile management
- Password encryption with bcryptjs
β Subscription Management
- Create subscriptions
- Change subscription plans
- Cancel subscriptions
- Track subscription status
```bash git clone cd saas ```
```bash npm install ```
```bash cp .env.example .env ```
Edit .env and add your credentials:
```env
STRIPE_SECRET_KEY=sk_test_your_key_here STRIPE_PUBLISHABLE_KEY=pk_test_your_key_here STRIPE_WEBHOOK_SECRET=whsec_your_secret_here
DAHILA_API_KEY=your_dahila_master_key_here DAHILA_API_URL=https://api.dahila.com
JWT_SECRET=your_jwt_secret_key_here JWT_EXPIRE=7d
MONGODB_URI=mongodb://localhost:27017/saas-db
PORT=5000 NODE_ENV=development ```
```bash npm run dev # Development with auto-reload npm start # Production ```
| Method | Endpoint | Description |
|---|---|---|
| POST | /create-payment-intent |
Create a payment intent |
| GET | /payment-intent/:id |
Get payment intent status |
| POST | /customer |
Create a customer |
| GET | /customer/:id |
Get customer details |
| POST | /subscription |
Create a subscription |
| POST | /subscription/:id/cancel |
Cancel subscription |
| GET | /subscription/:id |
Get subscription details |
| POST | /webhook |
Handle Stripe webhooks |
| Method | Endpoint | Description |
|---|---|---|
| GET | /master-key |
Validate Master Key |
| GET | /products |
Get all products |
| POST | /products |
Create a product |
| GET | /orders |
Get all orders |
| POST | /orders |
Create an order |
| GET | /orders/:id |
Get order details |
| PUT | /orders/:id |
Update an order |
| GET | /invoices |
Get all invoices |
| POST | /invoices |
Generate an invoice |
| GET | /dashboard |
Get dashboard statistics |
| Method | Endpoint | Description |
|---|---|---|
| POST | /register |
Register a new user |
| POST | /login |
Login a user |
| GET | /profile/:id |
Get user profile |
| PUT | /profile/:id |
Update user profile |
| Method | Endpoint | Description |
|---|---|---|
| POST | /create |
Create a subscription |
| GET | /:id |
Get subscription details |
| PUT | /:id/change-plan |
Change subscription plan |
| POST | /:id/cancel |
Cancel subscription |
| GET | /user/:userId |
Get user subscriptions |
```bash curl -X POST http://localhost:5000/api/users/register \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "securePassword123", "fullName": "John Doe" }' ```
```bash curl -X POST http://localhost:5000/api/stripe/create-payment-intent \ -H "Content-Type: application/json" \ -d '{ "amount": 29.99, "currency": "USD", "description": "Premium Plan Subscription" }' ```
```bash curl -X POST http://localhost:5000/api/stripe/customer \ -H "Content-Type: application/json" \ -d '{ "email": "customer@example.com", "name": "John Doe" }' ```
```bash curl -X POST http://localhost:5000/api/subscriptions/create \ -H "Content-Type: application/json" \ -d '{ "customerId": "cus_xxxxx", "planId": "price_xxxxx", "userId": "user_id_here" }' ```
```bash curl -X GET http://localhost:5000/api/dahila/master-key \ -H "Authorization: Bearer YOUR_TOKEN" ```
β
Environment Variables: Never commit .env file - use .env.example as template
β
JWT Tokens: Tokens expire after configured time (default: 7 days)
β
Password Hashing: Uses bcryptjs for secure password storage
β
HTTPS: Use HTTPS in production
β
Rate Limiting: Implement rate limiting in production
β
Input Validation: Always validate API inputs
Use Stripe's test cards for development:
- Valid:
4242 4242 4242 4242 - Declined:
4000 0000 0000 0002 - 3D Secure Required:
4000 0025 0000 3155
Dahila test credentials should be obtained from Dahila documentation.
``` saas/ βββ config/ β βββ stripe.js # Stripe configuration β βββ dahila.js # Dahila API client βββ routes/ β βββ stripe.js # Stripe endpoints β βββ dahila.js # Dahila endpoints β βββ user.js # User authentication β βββ subscription.js # Subscription management βββ server.js # Main server file βββ package.json # Dependencies βββ .env.example # Environment template βββ README.md # This file ```
```bash heroku login heroku create your-app-name git push heroku main ```
```bash heroku config:set STRIPE_SECRET_KEY=sk_live_xxxxx heroku config:set DAHILA_API_KEY=your_master_key heroku config:set JWT_SECRET=your_secret ```
For issues or questions:
- π§ Email: support@yoursite.com
- π Stripe Docs: https://stripe.com/docs
- π Dahila Docs: https://dahila.com/docs
MIT License - Feel free to use this project commercially
Made with β€οΈ by abdelhamidadel418-droid