All projects

E-Commerce Backend

A REST API for an online store: catalogue, cart, checkout and Stripe payments.

Repository Live API docs

A store’s backend has to do more than CRUD. Prices and product details change after an order is placed. Stock has to stay right when orders are cancelled. Payment confirmation arrives from the payment provider, separately from the customer’s browser.

This project is a store API built around those cases: catalogue, carts, orders and payments, with order records that stay accurate after the catalogue moves on.

  • JWT authentication on a custom user model that signs in with email. Access tokens last 60 minutes and refresh tokens one day.
  • Categories with parent categories, products, variants with their own size, colour, price and stock, and product images. Anyone can read the catalogue; only staff can change it.
  • Filtering, search, ordering and pagination on catalogue queries, including price ranges on variants.
  • Carts that merge a repeated variant into one line and keep the price at the time it was added.
  • Checkout that turns a cart into an order and starts a Stripe Checkout Session. Users only ever see their own carts, orders, payments and addresses.
  • Product reviews, flagged as a verified purchase when the reviewer has a paid, shipped or delivered order for the product.
  • An order confirmation task run by a Celery worker through RabbitMQ.
  • Swagger and ReDoc documentation generated by drf-yasg.
E-Commerce Backend architectureClient sends to Caddy (HTTPS + JWT). Caddy sends to Web (proxy). Web exchanges data with Stripe (Checkout, webhook). Web sends to RabbitMQ (task). RabbitMQ sends to Celery worker. Celery worker sends to Redis (result). Web sends to PostgreSQL (catalogue, orders). Celery worker sends to PostgreSQL (order lookup). Docker Compose on a GCP VPS contains Caddy, Web, RabbitMQ, Celery worker, Redis.Docker Compose on a GCP VPSClientStorefrontCaddyTLS, Let's EncryptStripeHosted checkoutWebGunicorn, Django, DRFRabbitMQCelery brokerCelery workerOrder confirmationRedisTask resultsPostgreSQLManaged on SupabaseHTTPS + JWTproxyCheckoutwebhooktaskresultcatalogue, ordersorder lookup
The payment path is drawn in green. Stripe’s webhook marks the order as paid, and the web process hands the confirmation task to a Celery worker through RabbitMQ. Dashed boxes are managed services outside the server.

Path of an order

  1. The customer fills a cart with product variants. A database constraint allows one open cart per user.
  2. The client posts the cart_id and a saved shipping_address_id to /api/store/orders/checkout/.
  3. In one transaction the API checks the cart and stock, then creates the order, its items, a copy of the shipping address and a pending payment.
  4. It creates a Stripe Checkout Session that carries the order and payment ids in its metadata, and returns the checkout_url.
  5. The customer pays on Stripe’s hosted page.
  6. Stripe posts checkout.session.completed to the webhook. The API verifies the Stripe-Signature header, marks the payment as succeeded and the order as paid, saves the payment intent id, and queues the confirmation task.
  7. A Celery worker takes the task from RabbitMQ and runs it.

Stripe’s hosted checkout, not a custom card form

Card details are entered on Stripe’s page, so the API never receives or stores them.

Trade-off. There is less control over the payment screen, and the customer leaves the storefront to pay.

Fulfil on the webhook, not the redirect

An order is marked as paid only when Stripe’s signed checkout.session.completed event arrives; anything that fails the signature check is rejected. The browser redirect is a convenience, because a customer can close the tab before it happens. Internal order and payment ids travel in the session metadata so the webhook can find its records.

Trade-off. Order status is eventually consistent. For a moment after paying, the order still reads as pending.

Snapshot what was bought

A cart line keeps the price at the time the variant was added. Order items store the product name, variant details and unit price at the time of purchase, and the shipping address is copied onto the order. Editing or removing a product later does not rewrite order history.

Trade-off. The same facts are stored twice, on purpose.

Stock and totals kept in sync by signals

Django signals recalculate the order total when its items change, take stock down when an item is ordered and put it back when the item is removed or the order is cancelled, and mark the cart as checked out once its order exists. Restocking compares the previous order status, so cancelling twice never returns stock twice.

Trade-off. Signals keep this logic in one place, but it runs away from the code that triggers it, and bulk operations such as update() skip signals entirely. That is why the suite tests these paths end to end, including partial quantity changes and repeated cancellations. Stock is taken when the order is created, so an abandoned checkout holds its units until the order is cancelled.

Invariants enforced by the database

Conditional unique constraints guarantee one open cart per user, one default shipping address per user and one featured image per product, even when two requests race. Indexes cover the fields the API filters on.

Trade-off. A violation surfaces as a database error, which the API has to turn into a useful response.

RabbitMQ as the broker, Redis for results

Celery tasks are queued on RabbitMQ, and Redis stores task results.

Trade-off. That is one more service to run than using Redis for both, on a server with 1 GB of memory. Redis is capped at 64 MB and also serves as the AgriTech backend’s broker, on a separate database number.

  • Migration files are not committed yet, so a fresh clone runs makemigrations before migrate.
  • The tests do not run in CI yet, and the checkout and webhook have no API tests; the Stripe flow was tested by hand with test cards and the Stripe CLI.
  • No email backend is configured, so the confirmation task does not send real emails yet. It should also be queued only after the transaction commits.
  • Expired sessions and refunds are not handled as Stripe events yet.
  • Some diagnostics still use print instead of structured logging.
API
Python 3.11, Django 4.2, Django REST Framework, Simple JWT, django-filter, drf-yasg
Async
Celery 5, RabbitMQ, Redis
Data
PostgreSQL (Supabase)
Payments
Stripe Checkout and webhooks
Infrastructure
Docker Compose, Gunicorn, WhiteNoise, Caddy, GCP Compute Engine, Cloudflare DNS
Testing
Django test runner (signal integration tests)