From Cart to Order
Introduction to Checkout in Our API
Checkout is where an e-commerce backend stops being “a shopping list” and starts being “a real transaction.” In this lesson we’ll implement (and understand) the exact workflow our codebase uses to convert an open cart into a pending order, while keeping the data consistent even if two requests happen at the same time.
You’ll see how the checkout logic is organized across a repository layer and a service layer, and how a Next.js route turns service results into a consistent HTTP response. By the end, you’ll know exactly where “snapshotting” happens, why we use database locks during checkout, and what rules the system enforces before it will create an order.
Snapshotting: Why Orders Copy Cart Items
A cart is editable, temporary, and “live.” An order is a historical record that should not change just because product prices or inventories change later.
In our project, snapshotting happens by copying each cart_items row into a new order_items row at the moment of checkout. The copied data includes the product_id, quantity, and the unit_price_cents that the cart item had at that moment. That gives us a reliable receipt-like record for the order.
Where the Checkout Workflow Lives
Checkout is implemented across three layers:
- Repository layer:
src/lib/repositories/ordersRepo.tsTalks to PostgreSQL, performs transactional work, and snapshots cart items into order items. - Service layer:
src/lib/services/ordersService.tsConverts repository errors into API-friendlyServiceResults and enforces order-state rules for pay/cancel. - Route handler:
src/app/api/carts/[id]/checkout/route.tsValidates the URL param, calls the service, and returns standardized success/error envelopes.
Let’s walk through each part, starting with the repository, because that’s where the most important checkout guarantees are enforced.
Reading Orders and Mapping Database Rows
This first part of src/lib/repositories/ordersRepo.ts defines row shapes (what Postgres returns) and maps them into our domain types (Order and OrderItem). These helpers keep the rest of the repo code clean and ensure our API deals with consistent, typed objects.
OrderRowandOrderItemRowdescribe the exact columns we read from PostgreSQL. This repo works directly with SQL, so having explicit row shapes prevents accidental field mismatches and makes mapping predictable.mapOrderRow()is where the DB-facingstatus: stringbecomes a safe domain value. If the status in the database isn’t one of our supported values (pending,paid,shipped,cancelled), we default to'pending'so downstream code doesn’t crash on unknown strings.mapItemRow()does the same for items, turning raw DB rows into domainOrderItemobjects with consistent naming and structure.- You’ll notice
computeTaxCentsis imported here, but checkout currently setstaxto0later. That’s intentional in the current code: tax logic is a placeholder to be refined, so the import hints at planned evolution without changing current behavior.
