The problem was that I did not use openInvoice correctly.
The way to call openInvoice using JavaScript is as follows:
window.Telegram.WebApp.openInvoice("linkOfInvoice","CallBack(optional)")
To obtain "linkOfInvoice", we can use the Telegram API and the createInvoiceLink method.