If you are talking about an existing gateway, you should not change the transaction ID to the invoice number, as that will stop the callback or payment confirmation from working.
When paying an invoice, the built-in variable $document_no is populated before the gateway is called, so you can check that variable for a value and pass it to the payment gateway if required. However, for order forms, the invoice is usually not created until after the payment has been confirmed by the payment gateway, so there is no invoice number yet.