"Why did God kill Onan for refusing to inseminate his dead brother's widow?"

Genesis 38:7-10 But Er, Judah's firstborn, was wicked in the sight of the Lord, and the Lord put him to death. Then Judah said to Onan, “Go in to your brother's wife and perform the duty of a brother-in-law to her, and raise up offspring for your brother.” But Onan knew that the offspring would not be his. So whenever he went in to his brother's wife he would waste the semen on the ground, so as not to give offspring to his brother. And what he did was wicked in the sight of the Lord, and he put him to death also.

Compare Verse Translations

We don't know what wickedness Er was guilty of to warrant his killing, but there are some very confusing events that follow. Judah tells Er's brother Onan to marry Er's widow and give her children. Why does God kill Onan for spilling his seed on the ground rather than inside his dead brother's wife? God wasn't even the one who originally asked Onan to do this. Is there anywhere else in the Bible that informs us God expects this behavior with a punishment of death for disobedience? How was Onan to know that God would find it wicked? We are also not told if Er's widow had any say in the matter. Why is God, or at least the author of Genesis, seemingly unconcerned with the wife's wellbeing or consent in this scenario? Even if this practice was traditional to the culture of the time, why would God not condemn it?

Christian Answers:

Secular Answers:

Referenced Verses:
Genesis 38:7, Genesis 38:8, Genesis 38:9, Genesis 38:10