Skip to main content

Overview

The Cart to Order API handles the critical conversion from shopping cart to confirmed orders. This system manages payment processing, inventory validation, cart cleanup, and order creation with comprehensive error handling and real-time updates. Location: convex/customers/cartToOrder.ts

Checkout Process

1

Validate Cart

Check item availability, stock levels, and price changes
2

Process Payment

Handle payment via Stripe, cash, or digital wallet
3

Create Order

Convert cart items to confirmed order with delivery details
4

Clear Cart

Automatically remove items from cart after successful order
5

Update Inventory

Reduce product stock levels and update analytics

Create Order from Cart

Creates an order from the items in a customer’s cart for a specific store.
customerId
Id<'customers'>
required
Customer ID placing the order
storeId
Id<'stores'>
required
Store ID receiving the order
deliveryAddress
DeliveryAddress
Delivery address information (required for delivery orders, optional for pickup)
deliveryType
'delivery' | 'pickup'
required
Delivery method selection
deliveryFee
number
required
Delivery fee amount (0 for pickup)
paymentMethod
'cash' | 'card' | 'wallet' | 'google_pay' | 'apple_pay'
required
Payment method used
paymentIntentId
string
Stripe PaymentIntent ID (required for card payments)
paymentStatus
'pending' | 'paid' | 'failed'
Payment status (default: “pending”)
notes
string
Optional order notes or special instructions
estimatedDeliveryTime
number
Estimated delivery time in minutes

Validate Cart for Checkout

Validates cart items before checkout, checking availability, stock, and price changes.
customerId
Id<'customers'>
required
Customer ID
storeId
Id<'stores'>
required
Store ID

Complete Checkout Flow

Here’s a comprehensive checkout implementation:

Delivery Address Structure

Complete delivery address format:

Payment Integration Notes

Stripe Payments

Method: "card" Required: paymentIntentId Status: "paid" for successful payments

Cash Payments

Method: "cash" Required: None Status: "pending" until delivery

Wallet Payments

Method: "wallet" Required: Sufficient wallet balance Status: "paid" immediately

Google Pay

Method: "google_pay" Required: paymentIntentId Status: "paid" for successful payments

Apple Pay

Method: "apple_pay" Required: paymentIntentId Status: "paid" for successful payments

Automatic Processing

When an order is created, the system automatically:
Automatic: Cart is cleared after successful order creation
Automatic: Product inventory is updated
Automatic: Store wallet is credited with earnings
Automatic: Relevant parties are notified

Error Handling

Status Code: 400
Status Code: 400
Status Code: 400
Status Code: 402
Status Code: 400

Best Practices

Always Validate

Always validate the cart before creating an order to handle stock changes and price updates

Handle Failures

Implement comprehensive error handling for payment failures and validation issues

User Feedback

Provide clear feedback during each step of the checkout process

Security

Never store sensitive payment information - let Stripe handle all card data

Order Creation Flow

The cart-to-order conversion is atomic - either the entire process succeeds or fails completely, ensuring data consistency.
Use the validation function before showing the payment screen to catch issues early and provide a better user experience.