Magento 2 – Create Custom Admin Credit Card Form

magento2payment

I have a requirement of a client to add an employee pin prior to processing a credit card payment via the admin order screen.

Validating the pin isn't an issue but I am having a hard time figuring out how to utilize my custom form instead of the default magento cc-form with the Magento 2 xml and template system on the admin side.

Any assistance or example that overrides the standard cc-form in the administration order section would be greatly appreciated.

In Magento 1.x I would set a custom form template for the admin via:

class Packagename_Modulename_Block_Form_Customform extends Mage_Payment_Block_Form
{
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('somefolder/form/customform.phtml');
    }
}

Best Answer

To me I think you should use the setMethodFormTemplate from \Magento\Payment\Block\Form\Container.php:

public function setMethodFormTemplate($method = '', $template = '')
{
    if (!empty($method) && !empty($template)) {
        if ($block = $this->getChildBlock('payment.method.' . $method)) {
            $block->setTemplate($template);
        }
    }
    return $this;
}

So I reckon you can create the following layout file in your module: Vendor\Module\view\adminhtml\layout\sales_order_create_index.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="order_create_billing_form">
            <action method="setMethodFormTemplate">
                <argument name="method" xsi:type="string">cc</argument>
                <argument name="template" xsi:type="string">Vendor_Module::cc/form.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Also create another layout file: Vendor\Module\view\adminhtml\layout\sales_order_create_load_block_billing_method.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="order.create.billing.method.form">
            <action method="setMethodFormTemplate">
                <argument name="method" xsi:type="string">cc</argument>
                <argument name="template" xsi:type="string">Vendor_Module::cc/form.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Then you can create your custom template under Vendor\Module\view\adminhtml\templates\cc\form.phtml.

Also you may need to add the following to your module XML:

<sequence>
   <module name="Magento_Payment"/>
</sequence>
Related Topic