Roughly speaking, draw up a contract in which they agree to write the program specified by the documentation in which you retain ownership (including copyright) of both the program and the source code, and they receive whatever it is they get out of the deal (payment, presumably). Then get them to sign it.