Learnixo
All Projects
Backendadvanced View on GitHub

CartFlow

E-commerce checkout API — Redis cart, Stripe payments, inventory reservation

3–4 hours to set up9 technologies5 guided steps

About This Project

Build a production e-commerce checkout system in .NET: Redis-backed cart service, inventory reservation with optimistic concurrency, idempotent Stripe payment integration, and a checkout saga with full compensation logic. Covers the real failure modes of checkout — double charges, oversold inventory, and abandoned carts.

What You'll Learn

Implement inventory reservation with optimistic concurrency and retry
Integrate Stripe with idempotency keys to prevent double charges
Build a checkout saga with compensation for each failure mode
Use Redis for cart storage, flash-sale token queues, and caching
Reconcile payment state via Stripe webhooks

Key Features

Redis-backed cart with sliding expiry and cross-device sync
Inventory reservation with 15-minute hold and optimistic concurrency
Idempotent Stripe payment — safe to retry, never double-charges
Checkout saga: reserve → create order → charge → confirm → fulfil
Compensation logic: automatic rollback on any saga step failure
Stripe webhook reconciliation for payment state consistency
Flash sale mode: Redis token queue caps concurrent checkouts to stock count
Background worker releasing expired reservations every 60 seconds
ProblemDetails error responses with machine-readable error codes

Project Structure

directory tree
CartFlow/
├── src/
│   ├── CartFlow.Api/             # Minimal API endpoints
│   ├── CartFlow.Application/     # Checkout saga, commands, handlers
│   ├── CartFlow.Domain/          # Product, Order, InventoryReservation
│   └── CartFlow.Infrastructure/  # EF Core, Redis cart, Stripe client
├── tests/
│   ├── CartFlow.Unit/            # Saga and handler unit tests
│   └── CartFlow.Integration/     # Testcontainers: Postgres + Redis
└── docker-compose.yml

Setup Guide

1

Clone and start infrastructure

Start PostgreSQL, Redis, and the Stripe webhook forwarder.

bash
git clone https://github.com/asmanasir/CartFlow.git
cd CartFlow
docker-compose up -d
2

Configure Stripe keys

Add your Stripe test keys to user secrets.

bash
dotnet user-secrets set "Stripe:SecretKey" "sk_test_..."
dotnet user-secrets set "Stripe:WebhookSecret" "whsec_..."
3

Run migrations

Create the orders and inventory schema.

bash
dotnet ef database update --project src/CartFlow.Infrastructure

Running the Project

1

Run the API

Start CartFlow — Scalar docs at /scalar/v1.

bash
dotnet run --project src/CartFlow.Api
2

Run the checkout flow

Add to cart, reserve inventory, and complete payment.

bash
# Add item to cart
curl -X POST http://localhost:5000/api/cart/items \
  -H "Content-Type: application/json" \
  -d '{"productId": 1, "quantity": 2}'

# Start checkout (reserves inventory)
curl -X POST http://localhost:5000/api/checkout/start

# Complete payment
curl -X POST http://localhost:5000/api/checkout/confirm \
  -d '{"paymentMethodId": "pm_card_visa"}'

Project Info

CategoryBackend
Difficultyadvanced
Setup time3–4 hours to set up
Technologies9 tools

Tech Stack

ASP.NET Core (.NET 9)EF Core 9PostgreSQLRedisStripe.netMassTransitSerilogOpenTelemetryDocker

Prerequisites

  • .NET 9 SDK installed
  • Docker Desktop installed
  • Stripe test account (free) — test card numbers provided
View Source on GitHub
L

Learnixo

Project Author

Checkout is the most failure-sensitive flow in e-commerce. This project shows the real patterns — optimistic concurrency for inventory, idempotency keys for Stripe, and a saga with working compensation — not the tutorial version that collapses under concurrent load.