Hi Folks,
This is a topic that has discussed before, but I'm not conviced that the ideal solution has been found. Hopefully some of the more experienced users will have come across this before. Does anyone have any comments, pointers or code that would help explain this problem?
The aim of the game is to allow people to book/order a product or service whether by simple selection or a complex shopping cart system. Assuming that we have a satisfactory shopping cart system and a third-party payment partner to accept credit cards, the problem is in ensuring that the order process is consistent. I'll explain the problem in detail below.
Using the 1,2,3, analogy of online shopping, imagine customer #1 starts an order, booking 'n' amount of product 'x'...
Page 1
Customer #1 confirms his order and personal details
Page 2 (off-site 3rd party payment partner)
Customer #1 pays for his order
Page 3
Customer #1 receives confirmation of his order
Now imagine customer #2 starts looking at the availability of product 'x' while customer #1 is in the middle of paying for his order. We want to make sure that customer #2 sees only the actual number of product 'x' LESS the 'n' that customer #1 is in the process of buying. Otherwise, we risk letting customer #2 buy the same products as customer #1, or worse, if for example there are only 3 of 'x' and customers #1 and #2 both order 2, we will have an over-order.
Now, there may be people out there saying, "use a table where 'n' products are 'reserved' and customer #2 will see the reserved number deducted from the actual quantity. Then when the order is confirmed, delete the entry and decrement the inventory table by 'n'..." The problem here is that if customer #1 closes his browser or it crashes half way through, we are left with orphaned entries in the table.
Other people might shout, "simply run a CRON job or an admin page than cleans up orphan entries after a certain expire period..." The problem with that is customer #2 might want to order product 'x' after customer #1 bails out half way and cannot until the reserved number is liberated. Similarly, customer #1 might come back after a crash and see that he cannot order his product 'x' because there is effectively a lock on the reserved number until the orphan clean-up completes. Not very professional.
Still others might say, "what about using a session which keeps record of the reserved number of product 'x'. The session would be destroyed on the event of closing the browser or if it crashed..." The problem here is that customer #2 will not automatically see the number of products customer #1 has reserved.
Ideally, here is what we'd like.
Page 1
Customer #1 confirms his order and personal details
php-pseudo: $sql_1 = "INSERT INTO customer_orders ('order_status','product_ID','order_quantity',...etc) VALUES ('processing','x','n',...etc)"
Customer #1 presses 'proceed' to take hime to page 2
Page 2 (off-site 3rd party payment partner)
Customer #1 pays for his order using WorldPay, or similar
At this point customer #2 would only be able to order the actual quantity LESS the reserved quantity from the customer_orders table
WorldPay server redirects a succesful payment to our page 3a, or an unsuccesful one to 3b
Page 3a
Customer #1 receives confirmation of his order
php-pseudo: $sql_2 = "UPDATE customer_orders SET order_status = 'complete',...etc"
php-pseudo: $sql_3 = "UPDATE inventory SET quantity = ($actual_quantity - $order_quantity),...etc"
Page 3b
Customer #1 receives notification that his order was unsuccesful
php-pseudo: $sql_4 = "UPDATE customer_orders SET order_status = 'unsuccesful',...etc"
END
I see the main problem being in the event of the browser being closed or crashing, we'd like the equivalent of $sql_4 UPDATE statement to execute.
Now, any ideas?
Robbie
For previous threads, see:
Transactions over multiple php pages. How?
http://www.phpbuilder.com/board/showthread.php?s=&threadid=10259261
Transactions over several pages
http://www.phpbuilder.com/board/showthread.php?s=&threadid=10278923
Web Transactions in SSJS
http://developer.netscape.com/docs/technote/ssjs/webtrans/webtrans.html