Order Creation
Overview
Orders are created through the checkout process. The system supports both authenticated checkout (for registered customers) and guest checkout (for customers without accounts).
Order Creation Flow
Authenticated Checkout
For authenticated customers:
- Customer adds items to cart
- Customer selects shipping/billing addresses
- System creates payment intent
- Customer completes payment
- Webhook creates order from payment confirmation
Guest Checkout
For guest customers:
- Guest adds items to cart (using session ID)
- Guest provides email, name, phone, and address during checkout
- System creates customer record with
isGuest=true - System creates addresses for guest customer
- System creates payment intent
- Guest completes payment
- Webhook creates order from payment confirmation
Payment Intent Creation
Authenticated Request
POST /orders
Authorization: Bearer {token}
Content-Type: application/json
{
"shippingAddressId": "uuid",
"billingAddressId": "uuid",
"shippingCost": 50.0
}
Guest Request
POST /orders
X-Session-Id: {session-id}
Content-Type: application/json
{
"email": "guest@example.com",
"name": "Guest User",
"phone": "+919876543210",
"address": {
"type": "shipping",
"street": "123 Main St",
"city": "Mumbai",
"state": "Maharashtra",
"pincode": "400001",
"district": "Mumbai",
"country": "India"
},
"password": "SecurePassword123!",
"shippingCost": 50.0
}
Note: The password field is optional. If provided, creates an account instead of guest checkout.
Order Creation from Webhook
Orders are created automatically when payment is confirmed via webhook:
- Payment webhook received
- System retrieves checkout metadata (includes
customerId) - System creates order linked to
customerId - System commits inventory reservations
- System clears cart
Checkout Metadata
The checkout metadata stored during payment intent creation includes:
{
customerId: string; // Required - customer ID (guest or authenticated)
userId: string | null; // Optional - null for guests
shippingAddressId: string;
billingAddressId: string;
shippingCost: number;
discountSnapshot: DiscountSnapshot | null;
pricingSnapshot: PricingSnapshot | null;
createdAt: string;
}
Key Point: Orders are linked to customerId, not userId. This allows both guest and authenticated customers to have orders.
Customer Linking
Authenticated Customers
- Orders linked via
customerId(derived fromuserId) - Customer can view orders via
/ordersendpoint - Full order history available
Guest Customers
- Orders linked via
customerId(created during checkout) - Guest cannot access
/ordersendpoint (requires authentication) - Orders visible in admin panel
- Guest can claim account later to access orders
Account Claiming
After guest checkout, customers can claim their account:
POST /customers/claim
Content-Type: application/json
{
"email": "guest@example.com",
"token": "verification-token",
"newPassword": "SecurePassword123!"
}
This converts the guest customer to a regular account:
- Sets
isGuest=false - Sets
emailVerified=true - Updates
passwordHashin user record - Customer can now login and access orders
Order Data
All orders contain:
- Customer Information: Linked via
customerId - Shipping Address: From checkout metadata
- Billing Address: From checkout metadata
- Pricing Snapshot: Immutable pricing at time of checkout
- Discount Snapshot: Immutable discounts at time of checkout
- Order Items: Cart items at time of checkout
- Payment Information: Payment intent and transaction details
Idempotency
Order creation is idempotent:
- Payment-scoped idempotency prevents duplicate orders
- Same payment intent cannot create multiple orders
- Webhook retries are safe
Error Handling
Common Errors
Empty Cart
{
"statusCode": 400,
"message": "Cart is empty"
}
Missing Required Fields (Guest)
{
"statusCode": 400,
"message": "Email, name, phone, and address are required for guest checkout"
}
Missing Session ID (Guest)
{
"statusCode": 400,
"message": "Session ID is required for guest checkout"
}
Email Already Registered
{
"statusCode": 400,
"message": "Email already registered. Please login to continue."
}
Best Practices
- Guest Checkout: Always collect email early in the process
- Session Management: Maintain session ID throughout guest checkout
- Account Upsell: Offer account creation during checkout
- Post-Purchase: Send claim account email after guest checkout
- Order Tracking: Provide order lookup by email + order ID for guests