Design and Implement Payment Authorization Holds
Model holds that set money aside, then capture, release or expire them without ever letting open holds exceed the balance, and implement the core with tests.
Design and implement the part of a payment gateway that places holds on money. A hold sets aside part of an account's balance for a pending payment. Later the merchant either captures the hold, which actually takes the money, or releases it, which gives the money back to the customer to spend.
For this exercise, assume: - Each account has a balance in whole cents. Its available balance is the balance minus the amounts of all of its open holds. A new hold succeeds only if its amount is at most the available balance. - Placing a hold takes a request ID, an account, an amount and a time to live in seconds.
Sending the same request ID again returns the original result instead of placing a second hold. Reusing a request ID with different values is an error. - A hold is open until it is captured, released, or expires.
An open hold may be captured once, for any amount greater than 0 and at most its held amount; the captured amount leaves the balance and any remainder is released. Release gives the whole hold back. - A hold expires when its time to live has passed.
An expired hold can no longer be captured, and its amount is available again. - The current time is passed in through a clock, so tests can control it.
Unknown accounts or holds, amounts that are not positive, insufficient available balance, and actions on a hold that is no longer open are rejected with clear errors and change nothing. - Everything runs in one process in memory.
Refunds, currency conversion, persistence, card networks, and fraud checks are out of scope.
After the design, implement the hold operations in code and show the tests you would run.