How the Cart Works
Cart state lives inStoreContext as a plain object called cartItems. Each key is a food item’s _id and each value is the quantity currently in the basket:
loadCartData syncs the server-side cart to this local map:
addToCart and removeFromCart
Both functions optimistically update local state first, then persist the change to the backend when a token is present.Cart changes made by a guest (no token) are kept in memory only for the current browser session and are not persisted to the database.
Cart Page
The/cart route renders the Cart component, which lists every item where cartItems[item._id] > 0. For each row the component shows the item image, name, unit price, quantity, and line total. The × button calls removeFromCart.
The Cart Totals panel at the bottom summarises:
/order. Unauthenticated users are redirected back to /cart, as are users who reach /order with an empty cart.
PlaceOrder Page
The/order route renders the PlaceOrder component. It presents a delivery address form alongside a final cost summary.
Delivery Address Fields
Delivery Address Fields
All fields are
required.Stripe Payment Flow
1
POST /api/order/place
The frontend posts the full order payload —
items, address, and amount — along with the auth token.The backend:- Saves a new
Orderdocument to MongoDB withpayment: false - Clears the user’s
cartDatain theUserdocument - Builds a Stripe
line_itemsarray from the ordered items plus a Delivery Charges line item - Creates a Stripe Checkout session and returns
session_url
2
Redirect to Stripe
The frontend replaces the current page with the Stripe-hosted checkout URL:Using
replace removes the order page from browser history so the customer cannot navigate back to it after paying.3
Stripe redirects back
After the customer completes or cancels payment, Stripe redirects to:
4
POST /api/order/verify
The
Verify page immediately calls POST /api/order/verify with { success, orderId }.- success = “true” → sets
payment: trueon the order, then redirects to/myorders - success = “false” → deletes the order document, then redirects to
/
All Stripe line items use the INR currency, as set in
orderController.js. Prices are passed to Stripe in paise (price * 100).Delivery Fee Line Item
The flat Rs.2 delivery charge is appended to the Stripeline_items array on the backend so it appears as a named line on the Stripe-hosted receipt: