How to block data to complete payment

Hello everyone

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?

What is the best flow?

Have a nice day!

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?

[image]

How I can block some fields of table in MongoDB Realm?

Hi @Ciprian_Gabor

I think the use case is classic transaction:

  • User 1 opens a transaction to buy croissant
  • 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.

Best regards
Kevin

1 Like

Thank you for you answer Kevin!

Does anyone have an example of this for Kotlin or Swift. I am working with a KMM app and MongoDB Realm.

Hello everyone. Should I use write function from Realm to make the transaction possible?

To prevent multiple users from purchasing the same product simultaneously in your app:

  1. Reserve the product temporarily when a user starts the payment process.
  2. Verify the payment within about 30 seconds.
  3. If successful, mark the product as “sold” and complete the transaction.
  4. If the payment fails or times out, release the reservation.

Integrate a payment gateway like Stripe for secure transactions. This way, you maintain fairness and user experience in your app.