Skip to content

Checkout Flow

A complete e-commerce checkout flow demonstrating multi-step form management with state machines. This example shows how to handle cart management, shipping information, payment processing, and order completion.

This checkout flow demonstrates several key state machine patterns:

  • Multi-step workflow - Cart → Shipping → Payment → Processing → Success/Failure
  • Form state management - Input validation and data persistence across steps
  • Async processing - Simulated payment processing with success/failure scenarios
  • Error handling - Graceful error states with retry capabilities
  • Navigation controls - Back/forward navigation between checkout steps

The demo includes sample cart items and simulates payment processing with a 70% success rate.

export type CartData = {
items: Array<{ id: string; name: string; price: number; quantity: number }>;
total: number;
};
export type ShippingForm = {
address: string;
city: string;
zipCode: string;
error?: string | null;
};
export type PaymentForm = {
cardNumber: string;
expiryDate: string;
cvv: string;
error?: string | null;
};
export type ShippingData = {
cart: CartData;
shipping: ShippingForm;
};
export type PaymentData = {
cart: CartData;
shipping: ShippingForm;
payment: PaymentForm;
};
export type ProcessingData = {
cart: CartData;
shipping: ShippingForm;
payment: PaymentForm;
};
export type SuccessData = {
cart: CartData;
shipping: ShippingForm;
payment: PaymentForm;
orderId: string;
};
export type FailedData = {
cart: CartData;
shipping: ShippingForm;
payment: PaymentForm;
error: string;
};

The checkout flow uses a single state machine to manage the entire process:

  1. Cart State - Display items and total, allow proceeding to shipping
  2. Shipping State - Collect shipping address with validation
  3. Payment State - Collect payment information and display order summary
  4. Processing State - Handle async payment processing with loading state
  5. Success/Failed States - Show completion status with appropriate actions

Each state maintains the necessary data and provides clear navigation options, ensuring users can only perform valid actions at each step.

Using a state machine for checkout flows provides several advantages:

  1. Clear Progress - Users always know where they are in the process
  2. Data Persistence - Form data is preserved when navigating between steps
  3. Error Recovery - Failed payments can be retried without losing information
  4. Validation - Each step can validate data before allowing progression
  5. Predictable Flow - Impossible states are prevented by design

This checkout example demonstrates practical state machine usage for e-commerce. You might want to explore: