Lifecycle Overview
BeanQuick uses two separate status fields:
estado for order fulfillment and estado_pago for payment status.Order States (estado)
The estado field tracks the order’s fulfillment progress:
Pedido.php:15-16
Pendiente
Pendiente
Initial state when order is createdCharacteristics:
- Order exists in database but not confirmed
- Payment status:
pendiente - Stock is NOT deducted yet
- Customer can still cancel
- Business cannot see this order yet
PedidoController.php:97-105
Preparando
Preparando
Active preparation by businessCharacteristics:
- Payment approved (
estado_pago = 'aprobado') - Stock has been deducted
- Order appears in business dashboard
- Business is preparing the products
- Customer cannot cancel (must contact business)
Pendiente to Preparando when payment is approved via Mercado Pago webhook.Business sees:PedidoController.php:153-157
Listo
Listo
Ready for pickupCharacteristics:
- Order is prepared and waiting for customer
- Customer should arrive at scheduled pickup time
- Business notifies customer (via frontend UI)
This is the customer’s signal that their order is ready to collect.
Entregado
Entregado
Order completedCharacteristics:
- Customer received their order
- Transaction complete
- Customer can now review products
- Final state (terminal)
Once delivered, the order cannot be modified further.
Cancelado
Cancelado
Order cancelledCharacteristics:
- Order will not be fulfilled
- If payment was approved, stock is returned
- Payment status changes to
rechazado - Terminal state
- Customer (only if
estado = 'Pendiente') - Business (at any time via status update)
PedidoController.php:179-192
Payment States (estado_pago)
Payment status is managed separately via Mercado Pago integration:
- pendiente
- aprobado
- rechazado
Awaiting paymentThis creates a Mercado Pago preference and returns a payment link.
- Order created but not yet paid
- Customer must complete payment
- Stock is validated but NOT deducted
- Order not visible to business yet
Order Creation Flow
Create Order
Customer proceeds to checkout:System validates:
- Cart is not empty
- All products belong to specified business
- Stock is sufficient for each product
- No duplicate pending orders exist
Prevent Duplicates
PedidoController.php:39-49
Customers can only have one pending unpaid order at a time.
Create Order Record
Order created with
estado = 'Pendiente' and estado_pago = 'pendiente'Products are linked via pedido_productos pivot table with snapshot of price:PedidoController.php:110-116
Prices are stored at order time to preserve historical accuracy even if product prices change later.
Payment Processing Flow
Initiate Payment
Customer clicks “Pay” on their pending order:This generates a Mercado Pago payment link and returns it to the frontend.
Customer Pays
Customer is redirected to Mercado Pago to complete payment using:
- Credit/debit card
- Cash payment locations
- Bank transfer
- Other payment methods
Webhook Notification
Mercado Pago sends payment confirmation:The
PagoController processes this and:- Verifies payment status
- Updates
estado_pago = 'aprobado' - Changes
estadotoPreparando - Deducts stock from products
Business Fulfillment Flow
View Orders
Business checks incoming orders:Only shows orders where
estado_pago = 'aprobado':PedidoController.php:153-157
Status Update Validation
PedidoController.php:213-217
Customer Cancellation Flow
Customers can cancel orders under specific conditions:Validate Cancellation
PedidoController.php:175-177
Only orders in
Pendiente state can be cancelled by customers.Return Stock (if applicable)
PedidoController.php:182-186
State Transition Rules
- Valid Transitions
- Invalid Transitions
- Allowed Values
| From | To | Trigger | Actor |
|---|---|---|---|
| Pendiente | Preparando | Payment approved | System (webhook) |
| Pendiente | Cancelado | Cancellation request | Customer |
| Preparando | Listo | Marked ready | Business |
| Preparando | Cancelado | Manual cancel | Business |
| Listo | Entregado | Marked delivered | Business |
| Listo | Cancelado | Manual cancel | Business |
Stock Management
When Stock is Deducted
Payment Approval
Stock is deducted when payment is confirmed (webhook):
When Stock is Returned
Cancellation
Stock is returned when paid orders are cancelled:
PedidoController.php:182-186
If order was never paid, stock was never deducted, so nothing needs to be returned.
Order Model Structure
Pedido.php:12-19
Relationships
Pedido.php:35-58
Database Schema
Complete State Diagram
Common Scenarios
Scenario 1: Successful Order
Scenario 1: Successful Order
- Customer places order →
Pendiente/pendiente - Customer pays via Mercado Pago
- Webhook confirms payment →
Preparando/aprobado+ stock deducted - Business prepares order
- Business marks ready →
Listo/aprobado - Customer picks up order
- Business confirms →
Entregado/aprobado✅
Scenario 2: Customer Cancels Before Payment
Scenario 2: Customer Cancels Before Payment
- Customer places order →
Pendiente/pendiente - Customer changes mind
- Customer cancels →
Cancelado/rechazado - No stock was deducted (no refund needed) ✅
Scenario 3: Business Cancels After Payment
Scenario 3: Business Cancels After Payment
- Customer places order →
Pendiente/pendiente - Payment approved →
Preparando/aprobado+ stock deducted - Business realizes they can’t fulfill (e.g., item damaged)
- Business cancels →
Cancelado/rechazado+ stock returned - Customer receives refund (handled by Mercado Pago) ✅
Scenario 4: Customer Abandons Order
Scenario 4: Customer Abandons Order
- Customer places order →
Pendiente/pendiente - Customer never pays
- Order remains
Pendienteindefinitely - No stock deducted, no impact on business
- Customer can cancel manually or create new order ⚠️
Consider implementing automatic expiration of unpaid orders after a timeout period.
API Endpoints Summary
Customer Endpoints
Business Endpoints
System Endpoints
Error Handling
Insufficient Stock
Cannot Cancel Non-Pending Order
Cannot Update Unpaid Order
Duplicate Pending Order
Best Practices
For Businesses
- Check orders frequently during business hours
- Update status promptly as orders progress
- Communicate with customers if delays occur
- Verify order details before marking as delivered
For Customers
- Pay promptly after placing order
- Arrive at scheduled pickup time
- Cancel early if plans change
- Leave reviews after receiving order
For Developers
- Always validate payment status before allowing state changes
- Use database transactions for stock operations
- Log all state transitions for audit trail
- Handle webhook failures gracefully with retries
For Admins
- Monitor orders stuck in Pendiente state
- Investigate payment failures
- Handle customer disputes about cancelled orders
- Review stock discrepancies
Next Steps
User Roles
Learn about permissions for each role
Business Workflow
Understand the business registration process