Search code examples
phpmysqlipaypal-ipn

Update database after 5 minutes


I've created a IPN code that allows paypal to update my database for a item purchased. I am thinking ahead though that the person buying my item, might not confirm upon checkout, or that while they are confirming check out, someone else might click the buy it now button as well before it's confirmed via paypal.

The point to all this is, I want to figure out the best way to fix this. My idea is after they click the buy now button it sets the item to pending, and they have 5 minutes to confirm the purchase or it's sent back to available.

Please help.. I can provide my code if needed.


Solution

  • I'd suggest using something like Resque - there is a PHP port (https://github.com/chrisboulton/php-resque).

    • When the user goes to PayPal, set the status of the product to be 'reserved', and create a Resque job that's scheduled to run 5 minutes from now.
    • If the user buys the product, change the status from 'reserved' to 'sold'.
    • When the Resque job runs after 5 minutes, it checks whether the status is 'reserved', and if it is, it changes it back to 'available'.
    • If the status is 'sold', then it does nothing.

    If you don't want to use Resque, then you could do something a bit more simple like a cron job that selects all products that with a status of 'reserved' where the status was set more than 5 minutes ago. Then you change that status to 'available' again. You can run that cronjob every 60 seconds.

    However, this can get into complicated race conditions, and it will be less flexible, so I'd implement a queue-based system if you can.