I have a Product database table with columns such as product_id, price, and inventory_count.
A user clicks to buy a certain product at a certain price. My program generates a confirmation page listing the product and the price. Everything is fine and the user clicks “Confirm”. My program updates the inventory_count of that product and charges the user’s credit card the amount listed in the product’s price.
But during the time after the confirmation page is generated but before the user clicks “Confirm”, the price of that product has been changed. So the user might have seen a price of $10 in the confirmation page but after he clicks “Confirm” the price in the Product table has already been changed to $11 and that’s what he will be charged.
What’s the best way to handle a situation like this? I’m using MySQL and Python if that’s relevant.
Create a new table for pending purchases with the price you intend to actually charge, and add items to it when you generate the confirmation page.
Deduct from the
Producttable’sinventory_countwhen you add an item to thePendingtable, then you can re-increment theinventory_countif the purchase doesn’t go through.