Create Order (Checkout)
Request Body
ID of the user placing the order. Can be null for guest checkouts.
Array of products being ordered
Total order amount
Delivery address
Response
Indicates whether the order was successfully created
Human-readable result message
ID of the newly created order (only on success)
This endpoint uses database transactions to ensure data consistency. If any step fails (order creation, detail insertion, or stock update), the entire transaction is rolled back.
Order Processing Flow
- Validation: Checks that the cart is not empty
- Transaction Start: Begins a database transaction
- Order Creation: Inserts a new order with status “pendiente” (pending)
- Order Details: Inserts each cart item into the order details table
- Stock Update: Decrements product stock for each ordered item
- Transaction Commit: Commits the transaction if all steps succeed
- Rollback: Rolls back on any error
Create Order (PHP Checkout)
Request Body
User ID placing the order (optional)
Array of cart items
Total order amount
Response
Order success status
Result message
Redirect URL on success (typically “gracias.html”)
Order Data Structure
The order is stored in two tables: pedidos table:id_pedido- Auto-generated order IDid_usuario- User ID (nullable)total- Order total amountfecha_pedido- Timestamp (auto-generated)estado- Status (default: “pendiente”)metodo_pago- Payment method (Node.js version sets to “Tarjeta”)
id_pedido- Foreign key to pedidosid_producto- Product IDcantidad- Quantity orderedprecio_unitario- Unit price (Node.js version)precio- Unit price (PHP version)subtotal- Line item total (Node.js version)
There are slight schema differences between the Node.js and PHP implementations. The Node.js version includes
subtotal and precio_unitario fields in the order details, while the PHP version uses precio.Order Status Values
Orders in the system use the following status values:| Status | Description |
|---|---|
pendiente | Order has been placed but not yet processed |
procesando | Order is being prepared |
enviado | Order has been shipped |
entregado | Order has been delivered |
cancelado | Order has been cancelled |
The default status for new orders is “pendiente” (pending).
Error Handling
Both checkout endpoints implement robust error handling:- Validation errors return 400 status with descriptive messages
- Database errors return 500 status with error details
- Transaction failures trigger automatic rollback (Node.js version)
- Debug logging is enabled for troubleshooting (PHP version writes to debug_input.txt)