An application I've developed does online event registration. Many events have a fee that can be paid by credit card. The system works very well most of the time, but users do sometimes manage to make multiple payments (by clicking refresh) or to have their credit cards charged without us having any record of the transaction. The process I'm using to enter registrations is as follows (pseudo-code):
INSERT INTO registrations (to reserve spot for the registration)
Generate unique key for payment that is included in all subsequent com
+munication from browser
Collect information for registration
Check that payment with unique key hasn't been entered
Get credit authorization
UPDATE registration record with collected information including a fina
+lized flag
INSERT INTO payments (information from credit processor with transacti
+on code, unqiue key from previous step, etc)
There are several spots where things can go wrong. Sometimes a user will click refresh in the moment between submitting the transaction for approval and inserting the payment information in the payments table. This prevents the unique key check from stopping the second payment. Sometimes a database error after transaction approval prevents the information being stored properly.
Is there a good way to make this process atomic?
Thanks!