Habib Afolabi
Case study04

Audiophile

A headphone and speaker store with a real backend. Stock runs out, the server verifies every card payment through Paystack, and a payment can never turn into two orders, even when two shoppers race for the last unit.

Audiophile storefront home page featuring the XX99 Mark II headphones.
Audiophile product page for the XX99 Mark II headphones at $2,999, with a quantity picker and Add to Cart.
Product page
Audiophile checkout: billing and shipping fields beside a summary with a grand total of $3,049, to be charged in naira by card.
Checkout
  • Next.js
  • TypeScript
  • Postgres
  • Prisma
  • Paystack

The problem

Audiophile started as Frontend Mentor’s e-commerce challenge, and the storefront pages follow its design. The brief stops at the browser, though. Its checkout form leads to a thank-you screen with nothing behind it.

I built the parts a shop needs a server for. Stock is shared, so the last unit belongs to whoever pays first, and only a database can decide that. The server can’t take prices from the browser, so it prices every line again before charging. A popup saying “success” proves nothing, so the server confirms every payment with the payment provider itself. That’s why this project has a backend.

Paystack, not Stripe

Stripe doesn’t open accounts in Nigeria, so the store uses Paystack in test mode. The server checks the cart again, prices it and starts the transaction, so the server sets the amount. After the popup closes, the server checks the status, currency and amount with Paystack before it places an order.

The account had two surprises. Paystack rejected USD with “Currency not supported by merchant”, so it charges cards in naira at a fixed demo rate, which the checkout shows before you pay. Paystack also has an undocumented amount limit. A ₦200M test charge went through and a ₦500M one didn’t. The checkout now catches that rejection and says the order is too large to pay online.

An order exists before the payment

When Paystack accepts a transaction, the server records a PENDING_PAYMENT order that holds no stock. Three callers can then try to place it, often at the same moment. They are the browser after the popup, a “Check payment again” button, and Paystack’s webhook.

The claim is a conditional update. The change from PENDING_PAYMENT to PLACED succeeds for exactly one caller, and the others find nothing to change. The database requires each payment reference to be unique, so one payment can never become two orders. The cost is a table of pending orders from abandoned checkouts, which the store keeps.

Stock taken in one statement

The server takes stock with UPDATE … SET stock = stock - n WHERE id = … AND stock >= n, in the same transaction as the order claim. If any line updates no row, the whole order rolls back. Nothing can change the stock between the check and the write, and a stock >= 0 constraint in the database backs the rule up.

Refund rather than reserve

The store doesn’t hold stock while someone is in the payment popup, so the last unit can sell in the meantime. That order becomes REFUND_PENDING and the server refunds it in full. The server claims each refund before sending it and releases the claim if Paystack refuses, so it retries without ever refunding twice. The webhook answers with a 500 until Paystack has accepted the refund, so Paystack keeps redelivering the event.

Where it bit back

  • The design’s own colours. Some of the brief’s colours fail WCAG AA contrast, so the orange is deeper (#B05C2B instead of #D87D4A) and the grey body text is darker.

Testing

  • The order logic runs against a real database, not a mock. Concurrency bugs come from how the database locks and orders writes, and a mock would agree with whatever the code assumes.
  • When a test key is present, the tests call Paystack’s test API for real. They create and verify a transaction end to end, and deliver a signed webhook to the real route.
  • Test files run one at a time, because they share one test database and a file that changes stock would otherwise break another’s assertions.

Why full-stack here, client-only elsewhere

Listenprint and Pennywise each handle one person's data, so keeping it on their device is the point. A shop needs the opposite. Stock is shared, and only a database can decide who gets the last unit.