How to generate auto invoice by payment_action for custom payment methods in magento 2?

You can Generate an auto invoice after the order is placed successfully in Magento with the simple configuration.

You can use <payment_action> configuration in the config.xml file to set auto invoice for your custom payment method.

You can check programmatically Convert Order to Invoice Programmatically in Magento 2.

Using payment_action, You don’t need to generate manual action to create an invoice for the order.

You have to just create a config.xml file under your module etc directory.

Supported payment_action type will be authorize_capture or authorize.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <payment>
            <cashondelivery>
                <payment_action>authorize_capture</payment_action>
            </cashondelivery>
        </payment>
    </default>
</config>

In the above file, Just replace <cashondelivery> with your custom payment method code.

This is a simple way to save a lot of time to generate invoices manually or via script. Once your order is placed successfully, Your invoice generates automatically.