I have a website that allows purchasing by paypal. I sell items that are finite in quantity for each item, and that means the code has to be carefully structured to prevent 2 people from simultaneously buying the same item (for example, someone could get as far as logging into paypal, and then do nothing for a half hour while someone else could complete the entire process), since there could be only 1 left.
My solution has been to add a column to my inventory tables called “hold”. As soon as a user adds an item to the cart, it increments the hold cell for that item. When checking to see if a person can add the item, it requires that (inventory-hold)>0. Then, when I get around to processing things, if I complete an order I can take 1 off the hold value and also take 1 off the inventory. And if they remove items from the cart, of course it also takes the item off hold.
The problem is that I’m not really sure what to do if someone adds items to the cart, but then decides to just close the browser and never come back. How would I know that they’ve done that? This is in fact likely to happen quite often, and would result things sitting on hold indefinitely with no way for me to know that they’ve been abandoned. And that is a problem because it would eventually lead to items being unavailable for purchase even though they are available in inventory.
The site is PHP+mySQL, with some Javascript as well. The cart is handled with PHP Sessions (there’s no user log-in).
How can I solve this problem? Is there some way I can put a “time limit” when something is added to the cart, and if it’s not purchased within that time, it’s taken back out of cart (and off hold)? It would have to be a server-side clock of some kind, so that it continues to run and perform changes to the database even when nobody is on the site. Can that be done? I am still pretty new to this, maybe a dumb question…