I want to be able to make a payment with card on my App. How I can block some fields until my payment is completed so other users dont complete the payment and there are no products to sell.
For example: I have 1 croissant to sell. Two users are looking at the same time. They both are paying with the card. It takes about 30 seconds to verify the payment. The fastest verified user will take the croissant and the other user will pay but will not have the croissant.
Do I have to block the croissant when one user is trying to pay?
Hi I believe this is the best way (pessimistic), the block is temporary until payment confirmation. Can be placed inside a transaction in a moderate way. What do you think?
User 2 opens a transaction to buy the same croissant
User 1’s payment got processed first, updates the e.g. “stocks” collection, marking croissant having 0 stock
User 1’s transaction committed
Since croissant is now 0, user 2’s transaction aborted
That’s the very very general gist of it. Note that this kind of workflow is frequently cited as examples for transaction use in many other database products as well, so you’ll be able to find a related tutorial realtively easily.
In MongoDB, transaction is not very different. See Transactions for more details.