On the story of Balaam as background to the Gospel Matthew, see Raymond E. Brown, The Birth of the Messiah (Garden City, NY: Doubleday, 1977), pp. 190–196. Also see Jerome’s Commentary on the Gospel of Matthew 1.2.