Can anyone find the bug? This function worked for years, but then I did something very far away (i.e. I didn't change this function at all) which broke it.
This is the only error I could spot in the code you posted
$sendableUserMerit -= $excessSent;
While the solution was to be
$sendableUserMerit = max(0, $sendableUserMerit - $excessSent);