One simple way to avoid such payment issues is - always host code in your servers.
- Make things clear in the start, the workflows and Billing process.
- If you get small hints of late payments or slow payments, just stop the work for some time.
- Keep Milestones and proceed one step at a time. We follow checklists with all our clients at ninjaas.com
- Only after your final payments are cleared, consider moving code-base to the production servers or give them the access keys.
- Don't ever get nasty with any client.
If things got wrong, maybe it was your own mistake in the first place somewhere? You missed gathering requirements or something better be done next time to avoid such steps!
Finally Learn the process: We've learnt these things the hard way. There is no general formula. :)