(probably) another session question
Currently I'm working on a project which includes the possibility to buy things on the website. Once you click on something like 'buy it now', the item gets assigned to you (the user), so that other users cannot buy it.
Now, I've got that all worked out. The item-object has a field status in the database which is 0 for open, 1 for reserved or 2 for bought. Once I buy the item, no one else can click on 'buy it now', and once I have paid, the status changes to 2 and everybody is happy.
But, where I am stuck at is the bit in the middle. How do I go about setting a time-limit to the reserved status? That once I have clicked the button, I need to complete the payment in say 20 minutes? And if I don't complete it within those 20 minutes, the item gets put back on the market with status=0? Or, if I don't fancy buying it anyway and just press the browsers 'back' button to return to the items overview, how do I intercept that and change the status to 0 immediately?
One option is to have a separate table to contain 'held' items, which is being checked every time someone visits a page. If the query finds an item which has been put on hold for longer than 20 minutes it will change the status back to 0. That's all good fun for the 'hmm I'm taking too long to pay' thing, but it won't put an item back on the market as soon as someone decides to skip the sale.
I have been searching for it the whole evening, but didn't get very far. I looked at Claweds' thread too, which pretty much handles the first 20 minutes check, but doesn't involve the second bit.
If you do have a lead I can follow, please let me know.
Many thanks in advance,
If it were me, I'd add a column to your table alongside your status called status_changed. This would store a timestamp that gets updated to current datetime any time the status changes.
Then the tricky part comes in what mechanism do you use to check that timestamp and take action when 20 minutes is up? Well you can't count on the client at all. He could simply turn off his PC, you'd never know (unless you had Ajax pinging you back, which is usually not a good solution).
So, how about a CRON job that runs every minute? It would do just the one task: query the entire database table looking for rows with a "reserved" status and a status_changed timestamp older than 20 minutes. If it finds anything, update the status back to available and maybe send an email to the buyer that he missed his chance at buying the item.
Thanks for the reply! I hadn't thought of that solution, it also adds the possibility to track when the item was bought, double bonus!
The cron job would be possible, the process will never take longer than a minute so it won't get into trouble with itself.
Remains the issue of the user hitting the browsers back button. Any ideas?
The page the user lands on when they hit the back button, "product overview", you called it, is where you'll need to evaluate reserved items. Make a call on that page to a function that checks to see if the user has any items reserved, and take action if you find something-- either dropping the reservation, or maybe even alert the user they have an item reserved, do they want to cancel it? Something along those lines.
Brilliant! Now why didn't I think of that myself... Thanks!
|All times are GMT +1. The time now is 11:25 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.