Prevention is best here. What I do with new or small clients where I'm concerned about this possibility is require them to work off a retainer agreement—they prepay some block of hours or days or weeks, however I'm billing them, and I send them a bill at some frequency <= block of time (e.g. if they prepay for 2 weeks, I send them the bill weekly). The retainer is a clock ticking towards 0, and when the countdown hits zero, I stop working until the bill is paid. If I like them or there's trust building, I'll let it go a little into the red, but never more than I'm comfortable walking away from. I've learned this lesson way too many times and give clients very little rope to hang themselves with here.
The other thing to build into your contracts is that IP transfer is contingent upon payment. There have been a small but very educational number of times where I've been able to suspend a client's license to my code for non-payment. Enforcing it is somewhat more difficult if you don't also retain control of infrastructure (and this should also be spelled out in your contract), but just the understanding that they have no legal rights to the code without the consideration enumerated in the contract is enough to keep them at the table unless they're going out of business anyway (which is a different thing altogether.) And if the amounts are great enough it can be worth going to court for an injunction and you have the tools to do it this way.
The other thing to build into your contracts is that IP transfer is contingent upon payment. There have been a small but very educational number of times where I've been able to suspend a client's license to my code for non-payment. Enforcing it is somewhat more difficult if you don't also retain control of infrastructure (and this should also be spelled out in your contract), but just the understanding that they have no legal rights to the code without the consideration enumerated in the contract is enough to keep them at the table unless they're going out of business anyway (which is a different thing altogether.) And if the amounts are great enough it can be worth going to court for an injunction and you have the tools to do it this way.