Repository navigation
Debit cards, ATMs and payments - #21
Conversation
- Banking: debit cards ordered at the teller, with an optional name and a 4-digit PIN hashed with BCrypt; rename, change PIN, block, unblock or replace; 3 wrong PINs block the card; removing someone's access blocks their cards - ATMs: every placed kmb_atm1 map object (model 2942) is an ATM; /atm shows the balance, withdraws cash (at most 1,000 per card in 24 hours) and deposits cash that reaches the account 2 hours later; an account can't close while a deposit is on its way - Payments: ICheckoutService lets features take a cash or card payment into a bank account; /testpayment for testers and admins - Inventories: IInventoryItemNameProvider lets a feature name its items, so a card shows its bank, account and digits; Use on a card opens the ATM or the card's details - Docs: cards and ATMs, payments, banking, database, commands Database: new tables BankCards and BankPendingDeposits, new BankTransactions columns BankCardId and Description, and a seeded Debit card item (68). Hosts need a migration.
| } | ||
|
|
||
| /// <summary> | ||
| /// The limit check and the debit in one transaction, so two withdrawals at once can't both pass the limit. |
There was a problem hiding this comment.
Should fix (or a question): this comment promises more than the code does when several servers share one database.
The SUM in GetWithdrawnInPeriod is a normal read. In MySQL/MariaDB (InnoDB), a normal read inside a transaction does not lock anything. So two servers that withdraw with the same card at the same moment can both read "900 used", both pass the check, and both debit 100. The card then ends at 1,100. The UPDATE on BankAccounts only waits for the balance; it doesn't run the limit check again.
On one server this can't happen, because everything runs one after the other on the main thread. That's why I'm not calling it blocking.
A small fix is to lock the card row first, so the second withdrawal waits until the first one commits:
// Takes a row lock on the card, so a second withdrawal with this card waits here.
db.BankCards.Where(row => row.Id == card.Card.Id)
.ExecuteUpdate(setters => setters.SetProperty(row => row.WrongPinAttempts, row => row.WrongPinAttempts));
if (GetWithdrawnInPeriod(db, card.Card.Id) + amount > BankingRules.AtmWithdrawalLimit)If you'd rather leave it, please change the comment to say it's only safe on one server, and add that to the known limitations in docs/features/banking/cards-and-atms.md.
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
There was a problem hiding this comment.
Should fix: this file changed from CRLF to LF line endings, so GitHub shows all 352 lines as rewritten. The real change is about 35 lines (the name providers). This blank line in the using block is also new.
It makes the diff hard to review and git blame loses the history of the whole file. AGENTS.md says not to reformat code the task doesn't need.
Fix: put the file's original line endings back (for example open it in an editor that keeps CRLF, or run unix2dos on it) and remove this blank line, so only the provider changes are left in the diff.
ReviewVerdict: Needs changes Should fix
Docs: up to date. There are new pages for cards and ATMs and for Payments, a line in |
- ATMs: lock the card row before the withdrawal limit check, so two withdrawals with one card, even on two servers, can't both pass the limit - Inventories: InventoryDialogService.cs keeps its CRLF line endings and loses the extra blank line, so only the item name changes are left - AtmSession.GetCardId() gets a summary - Docs: cards and ATMs
ReviewVerdict: Ready to merge Earlier points, now fixed:
Docs: up to date |
Merges everything on
devintomain(2 commits). Merging publishes a newOpenRP.FrameworkNuGet version.Changes
kmb_atm1map objects (model 2942) work as ATMs through/atm: balance, withdrawals up to 1,000 per card in 24 hours, and deposits that are credited 2 hours later.ICheckoutServicegives features one way to take a cash or card payment into a bank account./testpaymenttries it out; no feature uses it yet.IInventoryItemNameProviderlets features give items their own names and extra information.Database
Hosts need a migration after updating:
BankCardsandBankPendingDepositsBankTransactions.BankCardIdandBankTransactions.DescriptionNo NuGet packages were added or changed.