An order as a process: status, reservation and external payment in a teaching model

A teaching model for orders, reservations and simulated payments on MySQL 8.4/InnoDB: independent transactions, unknown outcomes, retries and cancellation.

A teaching metaphor for a hold and a pending decision.

A buyer presses a button and sees an order status. Behind it may be several independent decisions: whether the order was accepted, whether goods are held, and what is known about payment. A single status field can hide the point at which one participant has finished while another still does not know the result. A particularly awkward case arises when a payment response is lost and the buyer asks to cancel.

Consider a fictional crate of pears. This is a conceptual teaching model, not a description of an existing shop or a finished integration. The local example has been run; the results and limits are below. There are no real orders, money, bank or external payment-service calls. MySQL connections use a network and database credentials, but the payment side itself remains a fake. The aim is to ask who owns each fact before choosing tools.

Three owners instead of one flag

Suppose there is one crate at the start. An order is created for one unit at 24,000 fictional minor units in UAH. The order, reservation and payment operation have three distinct stable keys. The form of a key matters less than using the same values for a repeat of the same intent. If the same key arrives with a different quantity, amount or currency, the model must reject it before another effect.

The order owner stores intent and process state. The reservation owner decides whether it can hold the crate and counts releases. The fake payment side stores its own final outcome and number of simulated captures. In the current implementation, each owner uses a separate native PDO connection to MySQL 8.4 and commits independently. In tests these connections share a schema, but the three ledgers do not form a common transaction. The tables use InnoDB, CHECK constraints and the utf8mb4_0900_bin collation; indexed identities use VARCHAR(191), while currency uses LONGTEXT. Before an operation, the model checks keys for valid UTF-8 and a limit of 191 characters. Model::initialize() explicitly prepares the schema through separate migrator connections before business transactions; repeated initialization preserves the existing stock balance. The constructor performs no DDL, and runtime connections have DML-only privileges. This is neither an atomic schema upgrade nor an automatic transfer of data from old SQLite files.

A positive stock snapshot is not a reservation. Two readers may see one available unit; reading alone does not decide who holds it. In this sequential model a separate reservation write comes before the simulated payment. A zero-stock control should reject the reservation and make no payment call. This model does not prove protection against concurrent requests: that needs separate concurrency tests against the actual database and implementation.

An unknown response is knowledge of its own

Before calling the payment side, the order owner records its dispatch intent. The fake can then commit success in its InnoDB ledger through a separate connection while the response to the order is lost. The payment side knows of one simulated capture, while the order has payment_unknown. The reservation remains held. This is not a payment failure: no response does not tell us whether the effect occurred.

A dangerous reaction is to create a new payment key and send again. The interface may have missed the response after the first success. A repeated command in the model therefore makes no second call. Recovery asks the fake's authoritative record about the same operation key. Confirmed success can move the order to paid; confirmed failure can move it to a payment failure state. The authority of that record is an assumption of this exercise. A real provider's guarantees and verification procedure must be studied separately.

Even a read may resolve nothing. A teaching 404 may mean the record is not yet visible; 503 may mean temporary unavailability. Neither proves the earlier call had no effect. The model reserves at most three reads for the same operation. When the budget is exhausted it retains uncertainty, the hold and a reason for follow-up. Three is an exercise limit, not advice for every provider. A real next step needs an explicit owner and policy, not a silent new payment.

Cancellation has timing and consequences

If cancellation arrives before dispatch, the model releases the reservation once and makes no payment call. Repeated cancellation must not increase stock twice. If the response is already unknown, it first records cancel_requested and keeps the hold: releasing the goods while assuming there was no payment would be a decision without the necessary fact.

Reconciliation has two different outcomes. Authoritative failure together with cancellation permits cancelled and one release. Authoritative success with the same request produces refund_required: the simulated capture already occurred, the hold remains, and there is no automatic refund or shipment. Compensation is a separate, unimplemented decision. A button press alone does not mean that money was returned or cancellation completed.

This caution has a cost: goods may remain held until a person or a separate process resolves the unknown state. Hold duration, expiry, late success and responsibility for a refund are product policies to design together. Automatically releasing on a timer without such a policy only moves the problem later.

Repeats, old events and local failures

A command must preserve the identity of its intent, not merely check the current status text. A repeat with the same key and content must not reserve, capture or release twice. The same key with different content is a conflict, not a new operation. Event identity similarly identifies a repeated delivery. An old pending event must not move a confirmed success back to waiting. This rule concerns knowledge of the result; later real refunds or disputes would need their own transitions and are not modelled here.

A local commit can fail too. If the release write is interrupted before commit, the reservation cannot be declared released. After recovery, reread each ledger through fresh connections and retry the safe local action using the same key. One commit does not automatically make the other two atomic. Reservation and release lock the stock row with SELECT … FOR UPDATE inside their own InnoDB transaction; this does not make the whole process concurrency-safe. On an exception, the transaction wrapper rolls back its own transaction if still active and propagates the error, including native lock-wait error 1205 and deadlock error 1213. MySQL's InnoDB error-handling documentation explains rollback boundaries. Separately, DDL can cause an implicit commit, so schema preparation stays outside runtime transactions. This exercise is not a power-loss test or proof of distributed atomicity.

Checking the model requires more than the final words “paid” or “cancelled”. After each step, inspect the order state, hold and release count, outcome and capture count, keys, send and read counts, and reason for uncertainty. Then close the three connections and open fresh ones to the same schema. Controls include normal success and duplicate; no stock; cancellation before dispatch; a lost response after success; cancellation during uncertainty followed by both success and failure; three inconclusive reads; old events, payload conflict and local rollback. Both native scripts passed after the MySQL 8.4 port: tests/OrderLab/model_test.php and tests/OrderLab/trace_test.php. Run them through the guarded disposable MySQL fixture: bash fixtures/mysql_labs/run.sh php tests/OrderLab/model_test.php and bash fixtures/mysql_labs/run.sh php tests/OrderLab/trace_test.php. The shared App\Infrastructure\MysqlLab helper supplies the test schema and separate migrator and runtime connections; DDL runs only during explicit fixture preparation. The trace retains 8 main flows and 12 labelled subcases, 74 post-operation snapshots after reconnecting, and 153 explicit checks. Exact rows in all five tables, orders, events, stock, holds and payments, are checked against independent static expected vectors, including O/R/P keys, quantity 1, amount 24000 UAH, cancellation, dispatch, known outcome, reason, reads, hold, release, sends and captures. Quantity, amount and currency conflicts and R/P collisions leave the ledgers unchanged. The implementation in src/OrderLab/Model.php was adapted to native MySQL, not left unchanged. These checks cover controlled pre-commit exceptions and normal reconnection, not an OS crash, concurrency, a shared atomic transaction, an external payment or an actual refund.

From map to implementation

Symfony Workflow can help describe permitted transitions, and Messenger can help deliver and retry messages. Neither mechanism alone decides which payment outcome is authoritative, whether goods can be released, or who performs compensation. Retrying delivery does not automatically make an external effect idempotent either. Owners, keys, rules and observable consequences come first; suitable mechanisms follow.

An earlier catalog map separated a stock observation from a reservation. This boundary continues through the order and payment: three owners can temporarily have different but honestly named knowledge. The model excludes taxes, discounts, rounding, authentication, shipping, real refunds and a production process. Its value is to make uncertainty visible and agree in advance which fact permits the next decision.

Process state and screen message need not be identical. “Checking payment” may be understandable to a person, while the internal record keeps the operation key, read count and reason for waiting. The interface must not promise that money was not taken when the system does not know. Similarly, refund_required means a compensation decision is needed in the exercise, not a bank instruction or confirmed refund.

Recording intent before dispatch creates another awkward window: the process might stop before even calling the fake. The order then does not know whether the other side saw the operation. The same reconciliation protocol must explain this case; the absence of a local response does not permit an invented outcome. In this exercise, exhausting the budget leaves the work open. Real provider contracts may differ in lookup, key retention and the meaning of responses.

Finally, the counters are neither business metrics nor proof that a payment system works. They make teaching expectations testable: one normal case expects one simulated effect; cancellation before dispatch expects zero calls and one release. If observations differ, investigate the sequence of writes rather than just changing the displayed status. Those expectations were confirmed in the checked scenarios.

The shared MySQL fixture and its files are introduced in the transaction chapter. Run commands from the single article_demo root; do not create a separate Symfony application for each article.

  1. Databases Handler idempotency: repeat the task without duplicating its result
  2. Platform Engineering Timeout after an external request: when the outcome is unknown
  3. Platform Engineering Old events arrive later: revisions and asynchronous change ordering
  4. Architecture Product catalog and journal: where reuse must end