Situation
A payment provider redirects customers back to Magento after a successful payment. At the same time the payment provider also sends a push to Magento. Both of these actions result in the same order being loaded, data being changed and the order being saved.
Problem
The above actions both load the same order, process different data and save the order. When the customer gets back to Magento at the same time as the push is sent the process that saves last overwrites the earlier process' already saved data.
- flow a: load order
- flow b: load order
- flow a: process order (set status 'a')
- flow b: process order (doesn't change status)
- flow a: save order (order now correctly has status 'a')
- flow b: save order (and overwrite the order data saved at step 5, reverting the status back to the old status)
Tried solutions
I have tried the following solutions without success.
- Putting a sleep() in the push handling, this does not work due to the random timing of the user redirect. Anywhere between 1 second and 30 seconds of sleep and this problem still happens.
- Reloading order object before save, this means I have to do all processing again and is basically an expensive sleep()
Question
The only way I can think of is setting some kind of flag file on order load and removing it on order save which would stop anything from loading the order while it's being processed. I am afraid this would cause problems elsewhere and is generally not a neat solution.
Is there a way to ensure that both sets of order data are processed without anything being lost?