Jump to content

Preventing Multiple Purchases

Go to solution Solved by zwirek2201,
13 minutes ago, AlexHouse said:

Imagine if you will you've just coded your own store to sell one off merch, you need to prevent multiple sales, so in the database you make a column with a limit quantity of 1, after checkout the availablity changes to 0, but 2 people were completing the purchase at the same time so in fact the availablity becomes -1, so you have the manually cancel a transaction. Is there a way to prevent multiple sales going through at the same time? thank you

I think you're looking for a database transaction system. 

https://en.wikipedia.org/wiki/Database_transaction

Essentially your purchasing process should be a transaction and if at the end of the process it fails, it reverts all of the changes that it made to the database.

Alternatively, you could temporarily change the quantity and if the purchasing process doesn't go through to the end (ex. user cancels purchase), it reverts back to the previous state.

Edit: I did not understand your question very well. From the database point of view the only thing that you need to do is to not allow the availability to go below 0. If during the transaction it does happen (meaning another user just got the last product) you should cancel the whole database transaction (using the transaction system mentioned above) and display an error to the user. You have to remember though that it could be quite annoying for the user and there may be problems if user paid for the product that has just been bought.

 

It is not that much a database problem, but a design problem. You should never let your availability go below 0, but at the same time you should never allow the system to even try to get it below 0.

 

What some stores are doing is "reserving" the product for the customer when they go to the checkout. If they leave the website without buying the product, availability is updated and another user can get it. 

To avoid any mishaps with payments etc. you should never let the customer go to the checkout if at least one product is not "available" at the moment.

Imagine if you will you've just coded your own store to sell one off merch, you need to prevent multiple sales, so in the database you make a column with a limit quantity of 1, after checkout the availablity changes to 0, but 2 people were completing the purchase at the same time so in fact the availablity becomes -1, so you have the manually cancel a transaction. Is there a way to prevent multiple sales going through at the same time? thank you

Link to comment
https://linustechtips.com/topic/783349-preventing-multiple-purchases/
Share on other sites

Link to post
Share on other sites

13 minutes ago, AlexHouse said:

Imagine if you will you've just coded your own store to sell one off merch, you need to prevent multiple sales, so in the database you make a column with a limit quantity of 1, after checkout the availablity changes to 0, but 2 people were completing the purchase at the same time so in fact the availablity becomes -1, so you have the manually cancel a transaction. Is there a way to prevent multiple sales going through at the same time? thank you

I think you're looking for a database transaction system. 

https://en.wikipedia.org/wiki/Database_transaction

Essentially your purchasing process should be a transaction and if at the end of the process it fails, it reverts all of the changes that it made to the database.

Alternatively, you could temporarily change the quantity and if the purchasing process doesn't go through to the end (ex. user cancels purchase), it reverts back to the previous state.

Edit: I did not understand your question very well. From the database point of view the only thing that you need to do is to not allow the availability to go below 0. If during the transaction it does happen (meaning another user just got the last product) you should cancel the whole database transaction (using the transaction system mentioned above) and display an error to the user. You have to remember though that it could be quite annoying for the user and there may be problems if user paid for the product that has just been bought.

 

It is not that much a database problem, but a design problem. You should never let your availability go below 0, but at the same time you should never allow the system to even try to get it below 0.

 

What some stores are doing is "reserving" the product for the customer when they go to the checkout. If they leave the website without buying the product, availability is updated and another user can get it. 

To avoid any mishaps with payments etc. you should never let the customer go to the checkout if at least one product is not "available" at the moment.

Try, fail, learn, repeat...

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×