It wasnt just a matter of failing to fulfill the Levirate obligation, because the punishment for that is described in detail in Deuteronomy 25: 5-10: it consists of a public insult: the woman can take off the sandal of the man who refused to impregnate her, and spit in his face.
That's it: public disgrace of the man at the city gate.
But Onan was not just given public disgrace: God judged him worthy of death.
Why? Because it wasn't just his refusal of his Levirate obligation-- that only merited a public shaming --- but the WAY he did it: by performing a perverted, contracepted act.
Scripture says "And the thing which he did displeased the Lord; therefore He killed him also."
It doesn't say Onan was punished for "the thing he didn't do" (raise up a son for his deceased brother). It says he was punished for the thing he DID do.
And what did he do? He contracepted an act of intercourse.
That's why God slew him. God thought was he did was detestable. The point is this: for 1900 years, all Christians---Protestant, Orthodox, Reformed, Catholic, Evangelical, all of them--- understood God's law as forbidding any kind of altered sex act, including both Onanism and Sodomy. Now, since 1930, most of them ignore, have redefined, or have accepted, Onanism.
Give them another 15 years, and most of the contraceptors will redefine and accept Sodomy as well.
Many already do. Right here on this forum, you have FReepers already saying that God didn't explicitly condemn anal ejaculation, and it must be OK, because they do it to their wives.
Though I disagree with how you got here I'll skip the steps and cut to the chase. It really seems that you are saying that the bible says that oral sex between husband and wife is forbidden.
And what did he do? He contracepted an act of intercourse.
You brought this idea to the passage, likely because Rome taught it. It is false.
(Referring specifically to why God condemned him)