Remember, Onan wasn't punished for rubbing one out, he was punished for disobeying God. He could have been told to water the garden and failing that, got the same punishment. He just happened to be told to have impregnate his sister-in-law and didn't.
Onan being the son of Judah, from whose line would come our Messiah! (as in The Lion of the tribe of Judah!) No wonder God the Father was unhappy with Onan. Satan’s first opportunity to try to genetically mess up the line from whence Jesus would come!