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."
It is reported from Sayyiduna Umar bin al-Khattab (may Allah be pleased with him) concerning this story that the father of those (women) (Shu'ayb, may Allah have mercy on him) asked: "How do you know of his strength and his trustworthiness?" She said: "As for his strength, he lifted by himself the stone that only ten men could lift; and as for his trustworthiness, it is his saying: 'Walk behind me and describe the way to me,' so that the wind would not reveal your body to me."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب المساقاة / 11635