Second fix I've come up with for you in the past hour of being awake (spent the first 30 mins of it freaking out after seeing btc-e price too). Use a unique change address for each withdrawal. Makes it easy for you to know if a withdrawal went though
if (deposit to change address)
{
withdraw succeeded
}
else
{
reissue transaction
}
That could be tricked by the attacker making a payment to the change address if the amount of the change is small relative to the amount of the payment. There is nothing wrong with using tx id for record keeping just understand tx id can be changed. It is very rare and it requires a deliberate act (tx hashes aren't just being randomly changed by the thousands as the propagate the network).
When a new block is received:
For all unconfirmed transactions:
If the block contains a tx with a matching hash, the tx is now confirmed.
Update internal records and set number of confirmations to one.
If the block contains a tx with the same inputs & outputs but has a different tx hash.
Update internal records and set the tx hash to the new hash value and set number of confirmations to one.
For all confirmed transactions:
If the number of recorded confirmations in internal records is less than the # your business requires (i.e. <6) check that the new block extends the chain containing the block containing the tx
Update number of confirmations to be the difference in block height.