Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2012
    Posts
    109
    Thanks
    6
    Thanked 12 Times in 12 Posts

    Question (probably) another session question

    Hello all,

    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,
    Martin

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Nov 2012
    Posts
    109
    Thanks
    6
    Thanked 12 Times in 12 Posts
    Hi Fumigator,

    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?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Nov 2012
    Posts
    109
    Thanks
    6
    Thanked 12 Times in 12 Posts
    Brilliant! Now why didn't I think of that myself... Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •