Backend API for a farm e-commerce app with JWT auth, product/order management, and Mpesa payment integration using MongoDB.
# FarmConnect API - Node.js Backend
A backend REST API built with Node.js and Express to support the **FarmConnect** platformβa system designed to eliminate middlemen in the agricultural supply chain. This platform empowers farmers by directly connecting them to verified buyers and managing logistics, payments, and withdrawals seamlessly.
---
## π Purpose
Many farmers are exploited by brokers who buy low and sell high. **FarmConnect** eliminates this by:
- Allowing farmers to list their produce
- Enabling buyers to directly place and pay for orders
- Managing logistics centrally
- Paying farmers directly via **M-PESA**
---
## π§© Features
- β
Farmer and Buyer registration/login (JWT Auth)
- β
Role-based access (Farmer / Buyer)
- β
Product listing (Farmers)
- β
Order placement (Buyers)
- β
M-PESA payment integration (via STK push)
- β
Order tracking for both parties
- β
Farmer wallets and balance tracking
- β
Withdrawal requests by farmers
---
## π Folder Structure
```bash
βββ controllers # Route logic
βββ models # Mongoose schemas
βββ routes # Express routes
βββ middleware # Authentication & error handling
βββ services # Mpesa & utility services
βββ config # DB & environment config
βββ app.js # Main app entry
βββ server.js # Starts the server
```
---
## π Authentication
- Uses **JWT tokens** for secure route access
- Protected routes based on user role (`farmer`, `buyer`)
---
## π³ M-PESA Integration
- Buyers pay via **STK Push**
- Payment validation handled via callback URL
- Farmersβ wallet balance updated after successful payment
- Farmers can **request withdrawals**, triggering M-PESA B2C payments
---
## π οΈ Tech Stack
- **Node.js + Express**
- **MongoDB + Mongoose**
- **JWT** for Auth
- **M-PESA Daraja API** for payments
- **Dotenv** for environment management
---
## π§ͺ API Endpoints
| Method | Endpoint | Role | Description |
| -- β¦