hafiz-asad-2554/E-Commerce-Fraud-Detection-Prevention
GitHub: hafiz-asad-2554/E-Commerce-Fraud-Detection-Prevention
Stars: 0 | Forks: 0
# E-Commerce Fraud Detection & Prevention System
**Storefront:** Secure Shopping
**Submitted by:** Hafiz Muhammad Asad Mustafa (S23NDOCS1M01042) & Muhammad Islam (S22NDOCS1M01093)
**University:** The Islamia University of Bahawalpur, Bahawalnagar Campus
**Currency:** PKR
Full-stack e-commerce with **rule-based fraud checks (Laravel)** and an **ML fraud engine (FastAPI)**.
## Project structure
E-Commerce Fraud Detection & Prevention/
├── ecommerce Site/
│ ├── frontend/ # HTML/CSS/JS storefront (port 5500)
│ ├── backend/ # Laravel 12 API + admin (port 8000)
│ ├── start-backend.bat
│ └── start-frontend.bat
└── fraud detection engine/
├── backend/ # FastAPI + ML (port 8001)
├── datasets/ # Training CSV samples
├── model/ # Trained model artifacts
├── database/ # Sample transactions DB scripts
├── setup-ml-and-db.bat
└── start-engine.bat
## Prerequisites
- PHP 8.2+ and [Composer](https://getcomposer.org/)
- Python 3.11+
- Git
## One-time setup
### 1. Laravel backend
cd "ecommerce Site\backend"
composer install
copy .env.example .env
php artisan key:generate
php artisan migrate --seed
php artisan storage:link
Optional — hero slides and Kaggle-style product images:
php artisan db:seed --class=HeroSlideSeeder
php artisan db:seed --class=KaggleProductSeeder
php artisan products:sync-local-images --max=4
php artisan db:seed --class=ContactSeeder
Product images are read from `ecommerce Site\frontend\assets\images\products\` (CSV + `e-commerce/images/`).
### 2. Fraud detection engine
cd "fraud detection engine\backend"
python -m venv venv
.\venv\Scripts\activate
pip install -r requirements.txt
cd ..
.\setup-ml-and-db.bat
This trains the model (`model/fraud_model.joblib`) and creates the sample SQLite DB (`database/fraud_transactions.db`).
## Run (3 terminals)
| # | Command | URL |
|---|---------|-----|
| 1 | `ecommerce Site\start-backend.bat` | http://127.0.0.1:8000 |
| 2 | `fraud detection engine\start-engine.bat` | http://127.0.0.1:8001/docs |
| 3 | `ecommerce Site\start-frontend.bat` | http://127.0.0.1:5500 |
| Page | URL |
|------|-----|
| Store | http://127.0.0.1:5500/index.html |
| Checkout | http://127.0.0.1:5500/checkout.html |
| Admin | http://127.0.0.1:8000/admin/login |
| ML dashboard | http://127.0.0.1:8001/dashboard |
**Admin login:** `admin@iub.edu.pk` / `password`
Set `API_BASE_URL` in `ecommerce Site/frontend/js/config.js` to `http://127.0.0.1:8000/api` if needed.
## Testing
### FastAPI health & prediction
curl.exe http://127.0.0.1:8001/health
curl.exe -X POST http://127.0.0.1:8001/predict -H "Content-Type: application/json" -d "{\"amount_pkr\":200000,\"city\":\"Lahore\",\"ip_region\":\"sindh\",\"payment_method\":\"card\",\"previous_orders_24h\":5,\"account_age_days\":0.3,\"is_new_account\":true,\"items_count\":1}"
Expect high `combined_score` / `"risk_level": "High"` for the sample above.
### Sample transactions database
cd "fraud detection engine\database"
python ..\database\seed_database.py
.\view-sample-db.bat
Or open `fraud_transactions.db` in [DB Browser for SQLite](https://sqlitebrowser.org/).
### Storefront fraud demos (Laravel rules)
1. **High amount** — laptop Rs. 145,000+
2. **Location mismatch** — city Lahore vs simulated region Karachi
3. **Velocity** — 3+ orders in 30 minutes as the same user
Review flagged orders in **Admin → Fraud Detection**.
## Features
- Customer storefront: products, cart, multi-step checkout, geo hints, payment validation
- Admin: products (multi-image upload), categories, orders, users, hero slides, fraud module
- FastAPI: hybrid ML + rules, `/predict`, `/dashboard` with prediction logging
## API (Laravel)
Base URL: `http://127.0.0.1:8000/api`
| Method | Endpoint | Description |
|--------|----------|-------------|
| POST | `/register`, `/login` | Auth (Sanctum) |
| GET | `/products`, `/categories`, `/slides` | Catalog & homepage |
| GET | `/checkout/geo` | IP geolocation hint |
| POST | `/orders` | Place order (fraud checks) |
Admin panel routes are under `/admin/*` (session auth).
## Retrain ML model
cd "fraud detection engine\backend"
.\venv\Scripts\activate
python train.py
python ..\database\seed_database.py
## Troubleshooting
| Problem | Fix |
|---------|-----|
| Login `Failed to fetch` | Start `start-backend.bat`; free port 8000 |
| FastAPI `model not trained` | Run `setup-ml-and-db.bat` |
| Port 8001 in use | Stop other Python/uvicorn processes |
| Product images missing | `php artisan storage:link` and run image seeders |
| Old cached checkout | Hard refresh (`Ctrl+F5`) on checkout page |
## Contact
**Hafiz Muhammad Asad Mustafa** — hafizasad2554@gmail.com — S23NDOCS1M01042
**Muhammad Islam** — S22NDOCS1M01093 — Haroonabad, Pakistan — 0301 8445656
*Final Year Project — Department of Computer Science — IUB Bahawalnagar*
标签:ffuf