In this code, FOR UPDATE is the magic syntax. It forces the database to make other processes wait until this transaction is finished, ensuring that the balance is never calculated incorrectly due to timing.

# Otherwise, proceed with the payment logic result = execute_payment_logic(payment_details)