Complete e-commerce backend system built with Spring Boot 3, MongoDB, and Mock Payment Service.
- β Product Management (Create, List, Search)
- β Shopping Cart (Add, View, Clear)
- β Order Management (Create from cart, View details)
- β Payment Processing (Mock service with webhook)
- β Stock Management (Automatic updates)
- β Complete Order Flow (Products β Cart β Order β Payment β Status Update)
E-Commerce API (Port 8080) ββ Mock Payment Service (Port 8081)
β
MongoDB (Port 27017)
Two Applications:
- Main E-Commerce API - Handles products, cart, orders, and payments
- Mock Payment Service - Simulates payment processing and webhook callbacks
- Java 17 or higher
- Maven 3.6+
- MongoDB (running on localhost:27017)
macOS (using Homebrew):
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb-communityVerify MongoDB is running:
mongoshcd /Users/princeshakya/Desktop/E-CommerceBuild Main Application:
mvn clean installBuild Payment Service:
cd payment-service
mvn clean install
cd ..Terminal 1 - Start Mock Payment Service:
cd payment-service
mvn spring-boot:runTerminal 2 - Start Main E-Commerce API:
mvn spring-boot:runVerify both services are running:
- Main API: http://localhost:8080
- Payment Service: http://localhost:8081
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/products |
Create a new product |
| GET | /api/products |
Get all products |
| GET | /api/products/{id} |
Get product by ID |
| GET | /api/products/search?q=laptop |
Search products (Bonus) |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/cart/add |
Add item to cart |
| GET | /api/cart/{userId} |
Get user's cart |
| DELETE | /api/cart/{userId}/clear |
Clear cart |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/orders |
Create order from cart |
| GET | /api/orders/{orderId} |
Get order details |
| GET | /api/orders/user/{userId} |
Get user order history (Bonus) |
| POST | /api/orders/{orderId}/cancel |
Cancel order if not paid (Bonus) |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/payments/create |
Create payment |
| POST | /api/webhooks/payment |
Payment webhook (called by payment service) |
- Import
E-Commerce-API.postman_collection.json - Collection includes all endpoints with sample requests
- Variables:
userId,productId,orderIdare auto-populated
Step 1: Create Products
POST http://localhost:8080/api/products
{
"name": "Laptop",
"description": "Gaming Laptop",
"price": 50000.0,
"stock": 10
}Step 2: Add to Cart
POST http://localhost:8080/api/cart/add
{
"userId": "user123",
"productId": "<productId from step 1>",
"quantity": 2
}Step 3: View Cart
GET http://localhost:8080/api/cart/user123
Step 4: Create Order
POST http://localhost:8080/api/orders
{
"userId": "user123"
}Note: Cart will be cleared, stock will be reduced
Step 5: Create Payment
POST http://localhost:8080/api/payments/create
{
"orderId": "<orderId from step 4>",
"amount": 100000.0
}Step 6: Wait 3 seconds for webhook
Payment service will automatically:
- Wait 3 seconds (simulate processing)
- Call webhook:
POST http://localhost:8080/api/webhooks/payment - Update payment status (SUCCESS/FAILED)
- Update order status (PAID/FAILED)
Step 7: Check Order Status
GET http://localhost:8080/api/orders/<orderId>
[
{
"name": "Laptop",
"description": "Gaming Laptop",
"price": 50000.0,
"stock": 10
},
{
"name": "Mouse",
"description": "Wireless Mouse",
"price": 1000.0,
"stock": 50
},
{
"name": "Keyboard",
"description": "Mechanical Keyboard",
"price": 3000.0,
"stock": 30
}
]- User adds items to cart β Validates product exists & stock available
- User creates order β
- Validates cart not empty
- Checks all items in stock
- Creates order with status
CREATED - Creates order items (snapshot prices)
- Reduces product stock
- Clears cart
- User initiates payment β
- Validates order status is
CREATED - Creates payment with status
PENDING - Calls mock payment service
- Validates order status is
- Payment service processes β
- Waits 3 seconds
- Sends webhook with status (SUCCESS/FAILED)
- Webhook updates β
- Updates payment status
- Updates order status (PAID/FAILED)
E-Commerce API Mock Payment Service
| |
|---(1) Create Payment-------->|
|<--(2) Payment Initiated------|
| |
| (Wait 3s)
| |
|<--(3) Webhook Callback------|
| (Payment Status) |
| |
(Update Order Status)
- Validation Errors β 400 Bad Request with field errors
- Resource Not Found β 404 Not Found
- Business Logic Errors β 400 Bad Request with message
- Empty cart
- Insufficient stock
- Invalid order status
- Server Errors β 500 Internal Server Error
E-Commerce/
βββ src/main/java/com/example/ecommerce/
β βββ model/ # Entities (User, Product, Order, etc.)
β βββ repository/ # MongoDB Repositories
β βββ service/ # Business Logic
β βββ controller/ # REST Controllers
β βββ dto/ # Request/Response DTOs
β βββ exception/ # Exception Handling
β βββ config/ # Configuration
β βββ webhook/ # Webhook Controllers
βββ payment-service/
β βββ src/main/java/com/example/paymentservice/
β βββ controller/ # Mock Payment Controller
β βββ dto/ # DTOs
β βββ config/ # Configuration
βββ pom.xml
- Product APIs (15 points)
- Cart APIs (20 points)
- Order APIs (25 points)
- Payment Integration (30 points)
- Order Status Update (10 points)
- Code Quality (10 points) - Clean code, proper structure
- Postman Collection (10 points)
- Order History -
GET /api/orders/user/{userId}- View all orders for a user (+5 points) - Product Search -
GET /api/products/search?q=<query>- Search products by name (+5 points) - Order Cancellation -
POST /api/orders/{orderId}/cancel- Cancel order if not paid, restore stock (+5 points)
Error: MongoSocketOpenException
Solution: Ensure MongoDB is running
brew services start mongodb-communityError: Port 8080 is already in use
Solution: Kill process or change port in application.yaml
Error calling payment service
Solution: Ensure payment service is running on port 8081
- Mock Payment Service: 90% success rate (randomly fails 10% of time for testing)
- Stock Management: Automatic stock reduction on order creation
- Cart Behavior: Cart cleared automatically after order creation
- Order Status:
CREATEDβPAID(on success) orFAILED(on failure) - Payment Status:
PENDINGβSUCCESSorFAILED
Built for in-class assignment following Spring Boot best practices with clean architecture and proper error handling.
Built with π by Prince Shakya