'Rails - State machine and record being updated twice

I'm running into an issue : I use Stripe and AASM.

The way stripe is setup :

  1. You make a payment
  2. Payment succeeds
  3. You go back to the return_url (and here you should gather data and update your order's status accordingly)

In case the user never go back to the return_url, you are supposed to setup a webhook handling from stripe, which I did.

My issue is then with aasm (i manage state with this). The problem is that return_url handling and the webhook happens at the same time and both will for example trigger the order to go from created state to confirmed state. Them happening at the same time will not give enough time for each other to know that a transition is occuring already, making them both execute at the same time. That's my issue !

I read aasm's doc and they are talking about Pessimistic lock, this sounds like what I should need. Thus, when I activate it, I get the following error :

Event 'confirm' cannot transition from 'confirmed'.

THis was a struggle to explain, hopefully you get the point. Please help sexys. Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source