How to block data in MongoDB Realm until payment is done

I have this function to add an Order inside my Realm Repository:

suspend fun addOrder(products: List<Product>) {
           realm.write {
            val order = Order().apply {
                products.forEach {
                    this.products.add(it)
                }
            }
            copyToRealm(order)
        }
}

I know that a payment is approved when a new object is inserted with the message “Approved” inside isPaid collection: {“message”:“Approved”}

What is the best way to block the data before the payment starts and unblock it after the payment is done? If the payment fails, the process should be reverted. Its like Transaction in normal DB I guess.

Note: the AddOrder(list) function is called from UI (Kotlin button).

Hello @Ciprian_Gabor ,

Thanks for raising your question. This seems a more logical implementation question than blocking data in the database.
Could you share some more details on your workflow and code snippets? Did you try putting a boolean flag to the payment collection and allowing data to show when the value of the flag changes? You may need a separate collection for your payment and the data you would want to show.

I hope provided information helps in working out your flow.

Cheers, :performing_arts:
Henna

Hello @henna.s . Thanks for you time :slight_smile:
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 10 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 because there are 0 croissants left.

Do I have to block the croissant when one user is trying to pay?

I know that a payment is approved when a new object is inserted with the message “Approved” inside isPaid collection: {“message”:“Approved”}

What is the best flow?

Have a nice day!

Hello @Ciprian_Gabor,

Thank you for sharing more details. This seems like conflict resolution, those who ordered first will get the item first.
This is inbuilt logic in all Realm SDKs and you will not need to implement anything on top of this.

You may need to be online for this to function correctly. In addition, this requires thread management using the tech stack you are following that unless your write transaction is completed (read Async), it cannot in-take any other transaction. Hence, the person who made the order first will be processed first before another transaction can take place for the second person.

I hope the provided information is helpful. I would suggest creating a test application before you push the code to production.

Cheers, :performing_arts:
Henna

1 Like

Hello @henna.s, thanks for your time.

I found a better solution since our project is different.
We want to block the data before the payment is done because the payment is done on the native side since the user has multiple payment methods.
Step 1: remove the specific data from the DB when the user wants to order (before payment)
Step 2:
case1: if the payment is successfully done, the new order will be added
case2: if the payment is declined or cancelled, the removed data will be readded to the database.

Hope that makes sense
Have a nice day!

1 Like

Hello @Ciprian_Gabor,

Glad to know you were able to find a solution. I have been on a break last week and catching up now.

As you are doing verification client-side, I believe your way of implementation is correct. Otherwise, you could do verification using MongoDB Atlas Functions on the server side.

Would love to see your working app when it is ready :smiley:

Cheers, :performing_arts:
Henna