I have been developing an online retail shop.
It is written in php and uses paypal and IPN to handle payments.
I have written my own shopping cart. When the user wants to checkout they click on the checkout button which has the standard cart upload function wrapped around the button and the user is taken to paypal to complete payment. Paypal then sends me an IPN to notify me of the payment.
My question is, at what point should I store the order and when should the stock levels be reduced?
The standard process flow is that I have right now is as follows:
-
User adds item to cart.
- If item is now sold out or the quantity added is more than available, the cart is updated to reflect this.
- However, stock added to the cart does not reduce stock levels.
-
User clicks checkout.
- The cart is loaded into an order record in the db AND the stock levels are reduced.
- User is taken to paypal to complete payment.
-
(a) User completes payment.
(b) User does not complete payment by either returning to website or going somewhere else / closing browser.
-
(Optional) User clicks return to website.
- User sees a 'thank you, order complete' complete page.
- Nothing is processed relating to the order table since paypal will send IPN anyway.
- Paypal sends IPN
- Update order with the transaction status
- Update order with the transaction status
As you may see, there are some issues with this process. If the customer leaves the paypal page without completing payment I will have a 'dangling' order and since stock levels are also reduced this stock will not be available to other customers! A solution to this is to manually 'clean' the database every so often.
Alternatives?
option I) Do not store the order in the database until a 'completed transaction' IPN is received, then use the cart info stored in the session to create an order and reduce stock levels. However, sessions can expire and paypal payments might take days depending on payment.
option II) Store the order as is now but do not reduce stock levels until completed transaction IPN is received. This still has the issue of dangling orders but at least no stock will have to be re added again when cleaning up, I'll just have to remove the orders. Another problem with this though is that if multiple people order at similar times and thus collectively their orders contain quantities that exceed stock. This could be quite chaotic when the system receives completed IPNS and then reduces stock levels to negative quantities!
I have looked everywhere on the internet for some sort of help, but it is not mentioned anywhere! Everyone just skips to how IPN should be handled. I just don't understand how other people could not have had this problem?!
Please help!