Skip to content
All posts

Backend · Payments · Express

Designing an escrow flow for AssignMate

Kamruzzaman Akash · · 2 min read

AssignMate is an assignment marketplace. Students post work, verified experts bid on it, and the student's payment sits in escrow until the work is delivered. Payments go through bKash. The backend is Express 5, PostgreSQL with Prisma, and Redis, all running in Docker.

The interesting part isn't the UI. It's the question: who is allowed to move money, and when?

Model the lifecycle first

Before writing a single endpoint I wrote the states an assignment can be in:

open → paid → in_progress → submitted

Each arrow is a transition with a rule:

  • open → paid: only after the payment provider confirms the charge.
  • paid → in_progress: only when the chosen expert accepts.
  • in_progress → submitted: only the assigned expert can submit.

Writing it down as a state machine made the rest of the design obvious. Every endpoint becomes "attempt this transition", and the server either allows it or rejects it.

The server owns every transition

It's tempting to let the client say "the payment worked, mark it paid". Don't. The client is the one place you can't trust. The status only changes when the backend has verified the event itself, for example by checking the payment with the provider.

In practice that means a single function per transition that:

  1. loads the assignment,
  2. checks the current state and the user's role,
  3. performs the side effect,
  4. writes the new state.

The client only ever asks; it never decides.

Assume callbacks arrive twice

Payment providers retry. Networks time out after the work is done. If your "payment confirmed" handler runs twice, you must not charge twice or move the assignment twice.

The pattern I follow is idempotency: every payment event has an ID, and handling the same ID a second time is a no-op. Combined with the state machine (you can't go from paid to paid again), a duplicate callback simply does nothing.

Roles are part of the data, not the UI

Students, experts and admins see different buttons, but hiding a button is not security. Every transition re-checks the role on the server. The frontend's job is only to make the allowed path the obvious one.

What I'd tell myself at the start

Draw the states before you design the screens. Once the lifecycle is right, the API, the database schema and even the UI copy ("Waiting for the expert to accept") fall out of it.