Deut. 25:7-10 states,
"7 However, if a man does not want to marry his brother’s wife, she shall go to the elders at the town gate and say, “My husband’s brother refuses to carry on his brother’s name in Israel. He will not fulfill the duty of a brother-in-law to me.” 8 Then the elders of his town shall summon him and talk to him. If he persists in saying, “I do not want to marry her,” 9 his brother’s widow shall go up to him in the presence of the elders, take off one of his sandals, spit in his face and say, “This is what is done to the man who will not build up his brother’s family line.” 10 That man’s line shall be known in Israel as The Family of the Unsandaled. ".
Nowhere does it say that death is the punishment for not raising offspring for your dead brother. A man, according to Deuteronomy 25, is to be humiliated in public. What Onan did, “displeased God”.
Let’s continue on in Deuteronomy 25. The next passage addresses two men fighting, the wife of one intervenes. " 11 If two men are fighting and the wife of one of them comes to rescue her husband from his assailant, and she reaches out and seizes him by his private parts, 12 you shall cut off her hand. Show her no pity.". Rather than thank his wife for her help, the husband would be obligated to cut off her hand. Why? Could it be because she runs the chance of making him infertile?

This idea would fit into line with Gen 38:10, if Onan was killed for wasting his seed.
If Onan was killed for not raising up offspring for his brother, then why wasn’t Judah killed for not letting her marry Shelah?
Here’s a great link,
matt1618.freeyellow.com/birthcontrol.html.