Skip to content Skip to sidebar Skip to footer

Concurrent Payment Control

When I click on 'Place Order' I have begun the transaction and set the column is_payment_processing to True before taking the user to the merchant website and then there could be t

Solution 1:

Ideally you should have a stock field in your Product model to keep the number of quantities available for that product.

When someone places an order, a separate order instance should be created with the number of quantities of the Prodduct specified. The stock should only be reduced once the callback is received for that order or a webhook is received confirming the payment.

This would not prevent other customers from placing orders for the same product until the item has actually been sold.

Another approach could be to reduce the stock when the customer goes to the callback page and release the stock if the payment is not received within certain duration of time. Background task would be required for this.

Note: use F object from django.models while reducing the stock in order to reduce the stock from the DB value and not the instance attribute.

Solution 2:

Cron seems to be the best approach if you are able to(If you are on a shared host, many providers dont allow you any crons)

I might make the cron intelligent by checking if the transaction is still in progress. (Here I am assuming I have a mechanism to do so)

EDIT: Please note, locking the inventory once the payment transaction is in progress, is pretty standard

Post a Comment for "Concurrent Payment Control"