“And they attacked Midyan… and they killed Bilam the son of B’or” (Bamidbar 31:7-8). Being that after his failed attempt to curse Israel Bilam had returned home (24:25) to Aram (23:7), we wouldn’t have expected him to be in Midyan when G-d commanded Moshe to take revenge against them. The most widely quoted explanation for Bilam being back in the region is that he returned to collect the money owed to him because his advice to seduce the men of Israel had worked. Bilam had told Balk that G-d hates promiscuity, and that by getting Israel to sin He would punish them. After hearing that 24,000 had died from the plague that resulted from the sin of P’or, Bilam wanted his money, so came back to collect it. [This explanation is given in the Talmud (Sanhedrin 106) and in numerous Midrashim; among the commentators who reference it is Rashi, Ibn Ezra and Chizkuni.] Although this explains why Bilam was there, it doesn’t explain why Bilam went to collect his money from Midyan if the conversation about causing Israel to sin was only with Balak. The “elders of Midyan” had left after Bilam’s initial refusal to go with them to curse Israel (see Rashi on 22:7); why would Midyan owe him any money?
Mizrachi (31:8) fleshes this question out, and gives two answers. First he suggests that since it was Midyan who told Balak to hire Bilam, they were the ones Bilam contacted about collecting his debt from Moav. His second suggestion is that Bilam wasn’t trying to collect any money from Midyan, but was passing through Midyan on his way from Aram to Moav when Israel attacked. [Mizrachi admits that the wording of the Aggadah implies that Bilam’s destination was Midyan, not Moav, but says this shouldn’t preclude the possibility that his real destination was Moav. However, if Midyan is southeast of Israel (which is where Yisro’s Midyan seems to have been), the route from Aram, which is northeast of Israel, to Moav, which is on the eastern bank of the Jordan River, would not be through Midyan.]
Gur Aryeh assumes that Bilam was hired by both Moav and Midyan (although he doesn’t explain the basis for such a assumption), and says that since Moav only hired Bilam because they were afraid of Israel while Midyan hired him because of their hatred towards Israel (see Rashi on 31:2), Bilam figured that Midyan would be much more willing to pay what they owed him than Moav was. Although it could be said that Bilam intended on collecting his debt from both countries, and just went to Midyan first (where he was killed before he got a chance to try to collect from Moav), or that he had already collected his debt from Moav and was now trying to collect from Midyan, the Maharal makes it seem as if Bilam was only trying to collect the money that Midyan owed him.
Nachalas Yaakov says that even though Midyan didn’t hire Bilam to curse Israel (because they knew it was futile), Balak consulted with them (again) before following Bilam’s advice about how to weaken Israel. (This is evident from the fact that the daughters of Midyan joined the daughters of Moav in their quest to get Israel to sin; see Maharsha on Sanhedrin for a similar idea.) Therefore, both Moav and Midyan were responsible for paying Bilam when his plan succeeded. As far as why Midyan would owe money to Bilam if they didn’t hire him for anything and weren’t involved in the conversation with Bilam about getting Israel to sin, Nachalas Yaakov points out that there isn’t any mention of Balak promising Bilam any money for his advice either; just as Balak owed him money when the advice worked, Moav owed him money as well. (I would question why either owed him money, but apparently Nachalas Yaakov assumes that being paid for advice that works is a given, even if it wasn’t spelled out ahead of time.) Yalkut Shimoni (785) says that Bilam only gave Balak the advice on condition that he would get paid if it worked. Although this Midrash doesn’t explain why Bilam went to Midyan instead of directly to Balak, once Balak got Midyan involved in the plan (as Nachalas Yaakov suggests), he likely got them to agree to help pay for it as well.
Once we have a reasonable explanation as to why Bilam would try to collect from Midyan, there’s no need to explain why he went there before going to Moav. After all, he had to go to one of them first! Besides, he might have actually gone to Moav first, and was killed when he went to Midyan afterwards. Nevertheless, Nachalas Yaakov adds that he might have gone to Midyan rather than Moav because there was an additional reason to go to there — to help them defend against the avenging Israelites. However, even if Bilam was able to find out that his advice had worked while he was in faraway Aram (there was a time lag between the sin of P’or and the commandment to attack Midyan, enough to take a census of the entire nation as well as teach them some new laws), how did he know that Midyan was going to be attacked before he left?
The Midrash quoted by Yalkut Shimoni starts by explaining why G-d told Moshe to attack Moav now; “while Bilam is there, go upon them.” Besides making it clear that the attack on Midyan didn’t start until after Bilam had already traveled there, this Midrash is explaining why it was commanded again now, even though it had already been commanded right after the incident (25:16-18); by attacking now, they could kill Bilam at the same time. Putting all these things together, we can try to reconstruct what may have happened in a way that addresses the issues that have been raised.
Balak was originally an officer in Midyan (see Rashi on 22:4; see also B’er Ba’Sadeh’s commentary, where he suggests that Balak went back home to Midyan the same time that Bilam went home to Aram, as he was no longer the king of Moav), and had a working relationship with the Midyanim. (Although B’er Ba’Sadeh incorporates the fact that there are Midrashim that say that Tzur, one of the Midyanite kings who was killed, and whose daughter tempted Zimri, was Balak, Rashi does not bring this Midrash — even though there are several places where it would have been appropriate to do so. I am therefore avoiding including it in this scenario.) Before agreeing to follow Bilam’s advice, and commiting to paying Bilam if it worked, Balak consulted with his “landsmen,” and when they agreed to follow Bilam’s plan, they also committed to paying Bilam’s fee if it worked.
Bilam knew this before he left (not necessarily that Midyan was on board, but that Balak was going to consult with them and would only follow his advice if they agreed to join him), so when he heard that it worked, he knew that Midyan owed him money. It’s a long trip from Aram to Midyan, so he wasn’t necessarily going to make a special trip just to collect his debt, figuring that when he had another reason to be in the area, he would get it then. However, if Midyan was about to be attacked by Israel, he figured he better try collecting his money now, as if he didn’t collect it before Midyan was obliterated, he would never be able to. (Even if he didn’t know they would be obliterated, Midyan being attacked and suffering as a result of following Bilam’s advice would nullify their obligation to pay him anything, as following it would end up being a negative, not a positive.)
G-d wanted to punish Midyan (and prevent them from ever trying to cause Israel to sin again), but also wanted to punish Bilam. Therefore, He made it known to Bilam (who received divine messages) that He had commanded Israel to take revenge against Midyan (or was about to), knowing that this would motivate Bilam to try to collect his debt before it was too late. (As per Rashi, based on the Sifre, he also tried to talk Israel out of attacking, possibly because he didn’t want to lose any future business from Midyan or from others who would see what following his advice could lead to.) Bilam took the bait, and headed straight for Midyan to collect his money before they would no longer (be around to) pay it.