A robust and secure payment system backend that facilitates money transfers between agents and clients, with administrative oversight.
- π Secure authentication with JWT
- π₯ Role-based access control (Agent, Client, Admin)
- π° Fund transfer capabilities
- π Transaction ledger
- π Agent deposit management
- π KYC status tracking
- π³ Balance management
- π Transaction safety with database transactions
- π Comprehensive logging
- π‘οΈ Rate limiting and security headers
- Node.js
- Express.js
- PostgreSQL
- JWT Authentication
- bcrypt for password hashing
- dotenv for environment management
- cors for cross-origin resource sharing
- pg for PostgreSQL client
- Node.js (v14 or higher)
- PostgreSQL (v12 or higher)
- npm or yarn
- Basic understanding of REST APIs and database operations
-
Clone the repository:
git clone https://github.com/your-username/backend-repo.git cd backend-repo -
Install dependencies:
npm install
-
Database Setup:
# Create a PostgreSQL database createdb payment_system # Run the setup script psql -d payment_system -f sql/setup.sql
-
Environment Configuration: Create a
.envfile in the root directory with the following variables:PORT=3000 DB_USER=your_db_user DB_HOST=localhost DB_NAME=payment_system DB_PASSWORD=your_db_password DB_PORT=5432 JWT_SECRET=your_jwt_secret NODE_ENV=development
npm run devnpm start- POST
/api/auth/register - Body:
{ "phone_number": "string", "role": "agent|client|admin", "password": "string" } - Response (201 Created):
{ "id": 1, "phone_number": "+1234567890", "role": "agent" } - Error Responses:
- 400 Bad Request: Invalid input data
- 409 Conflict: Phone number already registered
- 500 Internal Server Error: Server-side error
- POST
/api/auth/login - Body:
{ "phone_number": "string", "password": "string" } - Response (200 OK):
{ "token": "jwt_token_here", "user": { "id": 1, "phone_number": "+1234567890", "role": "agent", "balance": 1000.00 } } - Error Responses:
- 401 Unauthorized: Invalid credentials
- 404 Not Found: User not found
- 500 Internal Server Error: Server-side error
- POST
/api/agent/transfer - Headers:
Authorization: Bearer <token> - Body:
{ "recipient_phone": "string", "amount": number } - Response (200 OK):
{ "success": true, "transaction_id": "uuid_here", "new_balance": 900.00 } - Error Responses:
- 400 Bad Request: Invalid amount or insufficient funds
- 401 Unauthorized: Invalid or missing token
- 403 Forbidden: Not an agent
- 404 Not Found: Recipient not found
- 500 Internal Server Error: Server-side error
- POST
/api/admin/approve-deposit - Headers:
Authorization: Bearer <token> - Body:
{ "agent_id": number, "amount": number, "method": "cash|bank_transfer" } - Response (200 OK):
{ "success": true, "deposit": { "id": 1, "agent_id": 1, "amount": 1000.00, "method": "bank_transfer", "verified": true } } - Error Responses:
- 400 Bad Request: Invalid input data
- 401 Unauthorized: Invalid or missing token
- 403 Forbidden: Not an admin
- 404 Not Found: Agent not found
- 500 Internal Server Error: Server-side error
id: SERIAL PRIMARY KEYphone_number: VARCHAR(15) UNIQUErole: VARCHAR(10)balance: DECIMAL(15,2)password_hash: VARCHAR(255)kyc_status: VARCHAR(20)created_at: TIMESTAMPTZ DEFAULT NOW()updated_at: TIMESTAMPTZ DEFAULT NOW()
id: UUID PRIMARY KEYcreated_at: TIMESTAMPTZtype: VARCHAR(20)sender_id: INTEGERreceiver_id: INTEGERamount: DECIMAL(15,2)status: VARCHAR(20)reference: VARCHAR(100)metadata: JSONB
id: SERIAL PRIMARY KEYagent_id: INTEGERamount: DECIMAL(15,2)method: VARCHAR(20)verified: BOOLEANverified_at: TIMESTAMPTZverified_by: INTEGERcreated_at: TIMESTAMPTZ DEFAULT NOW()
- JWT-based authentication
- Password hashing with bcrypt
- Role-based access control
- Database transaction safety
- Input validation
- Environment variable protection
- Rate limiting
- Security headers
- SQL injection prevention
- XSS protection
- CORS configuration
- Follow ESLint configuration
- Use meaningful variable and function names
- Add comments for complex logic
- Keep functions small and focused
- Use async/await for asynchronous operations
- Create feature branch from development
- Make atomic commits
- Write meaningful commit messages
- Update documentation as needed
- Create pull request with description
- Address review comments
- Merge after approval
- Write unit tests for new features
- Test edge cases and error scenarios
- Maintain test coverage above 80%
- Run tests before committing
-
Database Connection
- Check PostgreSQL service is running
- Verify database credentials in .env
- Ensure database exists
-
Authentication Issues
- Verify JWT_SECRET is set
- Check token expiration
- Validate token format
-
Transaction Failures
- Check database transaction logs
- Verify sufficient balance
- Check recipient existence
- Application logs in
logs/directory - Database logs in PostgreSQL log file
- Error tracking with stack traces
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Include description of changes
- Add tests for new features
- Update documentation
- Follow code style guidelines
- Address all review comments
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
For support, please:
- Check the documentation
- Search existing issues
- Create a new issue with:
- Detailed description
- Steps to reproduce
- Expected behavior
- Environment details
- Express.js team for the amazing framework
- PostgreSQL team for the robust database
- All contributors who have helped shape this project
- Open source community for inspiration and tools