#lumen-payment-redirect

1 messages · Page 1 of 1 (latest)

still stump
#

Hey @steady flame yes this is a valid approach, that's what I would do myself. You need to make sure you don't handle the code twice (like during the redirect and during the webhook

#

but otherwise that's exactly how I'd build this too

steady flame
#

To make sure I don't update the database twice, at the beginning of purchaseCourse() function I check whether the user already owns the course. If they do - that means the function has already been executed before, and the course has already been delivered, so I do nothing.

#

Does that sound about right?

still stump
#

yeah though you could have a "race condition" where both code "checks" and find not record and then does a database insert. But shouldn't be a big deal

steady flame
#

Yeah, I don't think it would be a big deal. In the unlikely event the code runs twice, nothing bad will happen, the DB updating code will just run once more, but result in no changes to the DB.

Just for future reference, are there ways to avoid this kind of race conditions?

still stump
#

You could use a "database lock" for example so that the second bit of code can't write until the first part is done, but that doesn't scale very well
You can also do "transactions" where basically you insert something, and before you finish you check if something else was inserting something and if so you decide which ones "wins" and delete the other one

steady flame