Allah Almighty says: "Then if they suckle (the child) for you, give them their wages." [Surah al-Talaq: 6]
Thus Allah Almighty permitted ijarah (a hire contract) for suckling.
And He said: "One of the two women said: O my father, hire him; indeed the best one you can hire is the strong, the trustworthy." [Surah al-Qasas: 26] to the end of the story.
Imam al-Shafi'i (may Allah have mercy on him) said: "So Allah Almighty mentioned that one of His prophets hired himself out for a fixed number of years, in return for which he gained the right to marry a woman; this indicates the permissibility of ijarah."
He said: "And it has been said that he hired him on the condition that he would herd (goats) for him, and Allah knows best."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب المساقاة / Q11635