Skip to content

ydv-manoj/ecom-flow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›’ eCommerce Checkout Flow Simulation

A complete 3-page eCommerce application built with Next.js App Router, Express.js, and MongoDB, featuring dynamic payment simulation and email notifications.

๐ŸŒŸ Features

  • Landing Page: Product display with variant selection and quantity controls
  • Checkout Page: Complete form validation and payment simulation
  • Thank You Page: Order confirmation with full transaction details
  • Payment Simulation: Different CVV codes trigger different payment outcomes
  • Email Notifications: Automated confirmation emails via Mailtrap
  • Database Storage: All order and customer data stored in MongoDB
  • Responsive Design: Mobile-first design with Tailwind CSS

๐Ÿ—๏ธ Project Structure

ecommerce-project/
โ”œโ”€โ”€ backend/                    # Express backend
โ”‚   โ”œโ”€โ”€ server.js              # Main server file
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”‚   โ””โ”€โ”€ db.js              # MongoDB connection
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”œโ”€โ”€ Product.js         # Product schema
โ”‚   โ”‚   โ””โ”€โ”€ Order.js           # Order schema
โ”‚   โ”œโ”€โ”€ controllers/
โ”‚   โ”‚   โ”œโ”€โ”€ productController.js
โ”‚   โ”‚   โ”œโ”€โ”€ orderController.js
โ”‚   โ”‚   โ””โ”€โ”€ emailController.js
โ”‚   โ”œโ”€โ”€ routes/
โ”‚   โ”‚   โ”œโ”€โ”€ products.js
โ”‚   โ”‚   โ”œโ”€โ”€ orders.js
โ”‚   โ”‚   โ””โ”€โ”€ email.js
โ”‚   โ”œโ”€โ”€ middleware/
โ”‚   โ”‚   โ””โ”€โ”€ errorHandler.js
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ .env.example
โ”‚
โ”œโ”€โ”€ frontend/                  # Next.js frontend (App Router)
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ layout.js         # Root layout
โ”‚   โ”‚   โ”œโ”€โ”€ page.js           # Landing page
โ”‚   โ”‚   โ”œโ”€โ”€ globals.css       # Global styles
โ”‚   โ”‚   โ”œโ”€โ”€ checkout/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ page.js       # Checkout page
โ”‚   โ”‚   โ””โ”€โ”€ thank-you/
โ”‚   โ”‚       โ””โ”€โ”€ page.js       # Thank you page
โ”‚   โ”œโ”€โ”€ utils/
โ”‚   โ”‚   โ””โ”€โ”€ api.js            # API utilities
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”œโ”€โ”€ tailwind.config.js
โ”‚   โ””โ”€โ”€ next.config.js
โ”‚
โ””โ”€โ”€ README.md                 # This file

๐Ÿš€ Getting Started

Prerequisites

  • Node.js (v16 or higher)
  • MongoDB (local or cloud instance)
  • Git

1. Clone the Repository

git clone 
cd ecommerce-project

2. Backend Setup

cd backend
npm install

Create a .env file in the backend directory:

cp .env.example .env

Update the .env file with your configuration:

# Server Configuration
PORT=5000
NODE_ENV=development

# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017/ecommerce

# Mailtrap Email Configuration
MAILTRAP_HOST=sandbox.smtp.mailtrap.io
MAILTRAP_PORT=2525
MAILTRAP_USER=your_mailtrap_username
MAILTRAP_PASS=your_mailtrap_password

# Email Settings
FROM_EMAIL=[email protected]

Start the backend server:

npm run dev

Backend will be running on http://localhost:5001

3. Frontend Setup

cd frontend
npm install

Create a .env.local file in the frontend directory:

NEXT_PUBLIC_API_URL=http://localhost:5001/api

Start the frontend development server:

npm run dev

Frontend will be running on http://localhost:3000

4. Database Seeding

The application will automatically seed a sample product when you first visit the landing page. You can also manually seed products by making a POST request to:

POST http://localhost:5000/api/products/seed

๐ŸŽฎ Payment Simulation

Use these CVV codes during checkout to simulate different payment outcomes:

  • 111 โ†’ โœ… Approved Transaction
  • 222 โ†’ โŒ Declined Transaction
  • 333 โ†’ โš ๏ธ Gateway Error/Failure
  • Any other 3 digits โ†’ โœ… Approved

Example Test Data

Card Number: 1234 5678 9012 3456
Expiry Date: 12/26 (any future date)
CVV: Use codes above for different outcomes

๐Ÿ“ง Email Configuration

Mailtrap Setup

  1. Sign up for a free account at mailtrap.io
  2. Create a new inbox in sandbox mode
  3. Copy your SMTP credentials
  4. Update the .env file with your credentials

Email Templates

The application sends different email templates based on payment outcomes:

  • Approved: Confirmation email with order details
  • Declined: Payment declined notification with retry instructions
  • Failed: Technical error notification

๐Ÿ› ๏ธ API Endpoints

Products

  • GET /api/products - Get all products
  • GET /api/products/:id - Get single product
  • POST /api/products/seed - Seed sample products

Orders

  • POST /api/orders - Create new order
  • GET /api/orders/:orderNumber - Get order by number
  • GET /api/orders - Get all orders (admin)

Email

  • POST /api/email/send-order-email - Send confirmation email
  • GET /api/email/test-connection - Test email connection

๐ŸŽจ UI/UX Features

  • Responsive Design: Mobile-first approach with Tailwind CSS
  • Form Validation: Real-time validation with react-hook-form
  • Loading States: Smooth loading indicators and transitions
  • Toast Notifications: User-friendly success/error messages
  • Card Formatting: Automatic credit card number formatting
  • Inventory Management: Real-time stock checking
  • Variant Selection: Dynamic product variant options

๐Ÿ”’ Security Features

  • Input Validation: Server-side validation for all inputs
  • Card Masking: Credit card numbers are masked in storage
  • CVV Protection: CVV codes are never stored
  • Error Handling: Comprehensive error handling and logging
  • CORS Protection: Configured CORS policies

๐Ÿ“ฑ Page Flow

  1. Landing Page (/)

    • Browse product with variants (color, size)
    • Select quantity and options
    • Add to cart and proceed to checkout
  2. Checkout Page (/checkout)

    • Enter customer information
    • Provide shipping address
    • Enter payment details with simulation
    • Submit order with real-time validation
  3. Thank You Page (/thank-you)

    • View order confirmation
    • See payment status (approved/declined/failed)
    • Download/print order details
    • Access customer support

๐Ÿงช Testing the Flow

  1. Start both servers (backend on :5000, frontend on :3000)
  2. Visit landing page: Select product variants and quantity
  3. Proceed to checkout: Fill out the form with test data
  4. Use simulation CVV: Try different CVV codes for different outcomes
  5. View confirmation: Check the thank you page and email notifications

๐Ÿš€ Deployment

Backend Deployment (Railway/Render)

  1. Push code to GitHub
  2. Connect to Railway or Render
  3. Set environment variables
  4. Deploy with auto-scaling

Frontend Deployment (Vercel/Netlify)

  1. Connect GitHub repository
  2. Set NEXT_PUBLIC_API_URL environment variable
  3. Deploy with automatic CI/CD

Environment Variables for Production

Backend:

NODE_ENV=production
MONGODB_URI=your_production_mongodb_uri
MAILTRAP_USER=your_mailtrap_user
MAILTRAP_PASS=your_mailtrap_pass

Frontend:

NEXT_PUBLIC_API_URL=https://your-backend-url.com/api

๐Ÿ› Troubleshooting

Common Issues

  1. MongoDB Connection Error

    • Ensure MongoDB is running
    • Check connection string format
    • Verify network connectivity
  2. API Connection Issues

    • Verify backend is running on correct port
    • Check CORS configuration
    • Ensure frontend API URL is correct
  3. Email Not Sending

    • Verify Mailtrap credentials
    • Check spam folder
    • Review server logs for errors
  4. Build Errors

    • Clear node_modules and reinstall
    • Check Node.js version compatibility
    • Review environment variable configuration

๐Ÿ“ Development Notes

  • App Router: Uses Next.js 13+ App Router for modern routing
  • Server Components: Optimized for performance where possible
  • Error Boundaries: Comprehensive error handling throughout
  • TypeScript Ready: Easy migration to TypeScript if needed
  • Scalable Architecture: Modular design for easy expansion

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published