The death punishment upon Onan was not for his failure to raise up posterity for his deceased brother, but for the crime of wasting the seed upon the ground – a primitive and vulgar form of contraception.
Vatican II declared unequivocally that “In questions of birth regulation, the sons of the Church, faithful to these principles, are forbidden to use methods disapproved of by the teaching authority of the Church in its interpretation of the divine law.” Gaudium et Spes, 51; footnote 14 refers to* Casti Connubii *and further teaching by Pius XI and Paul VI]. So the bishops of the Church in Ecumenical Council, approved by the Pope, have unequivocally taught this.
In Evangelium Vitae, St John Paul II, 1995, reiterates: “It is therefore morally unacceptable to encourage, let alone impose, the use of methods such as contraception, sterilization and abortion in order to regulate births.” #91].