Admin – Add Last 4 Digits and Card Type to Payment Information in Magento Order View

adminauthorize.netmagento-2.0.7orderspayment-methods

After customer create order with Authorize.net, the info in Payment Information in admin view order says "Credit Card Direct Post (Authorize.net) The order was placed using USD.".
How can I add type of card and last 4 digits to this block also?

Best Answer

I created custom extension to achieve this

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Authorizenet\Model\Directpost" type="MyVendor\MyModule\Model\Directpost" />
</config>

Model\Directpost.php

namespace MyVendor\MyModule\Model;

class Directpost extends \Magento\Authorizenet\Model\Directpost
{
    protected function fillPaymentByResponse(\Magento\Framework\DataObject $payment)
    {
        parent::fillPaymentByResponse($payment);
        $response = $this->getResponse();
        $payment->setCcType($response->getXCardType());
    }
}

view/adminhtml/layout/sales_order_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="payment_additional_info">
            <block class="MyVendor\MyModule\Block\Payment" name="authorizenet_additional" template="MyVendor_MyModule::order/view/info/authorizenet_additional.phtml" />
        </referenceContainer>
    </body>
</page>

view/adminhtml/templates/order/view/info/authorizenet_additional.phtml

<p>Last 4 digits: <?php echo $block->getCcLast4(); ?></p>
<p>CC Type: <?php echo $block->getCcType(); ?></p>
<p>Total Amount: <?php echo $this->helper('Magento\Framework\Pricing\Helper\Data')->currency($block->getAmountPaid(),true,false); ?></p>

Block/Payment.php

namespace MyVendor\MyModule\Block;

class Payment extends \Magento\Sales\Block\Order\Info
{
    public function toHtml() {
        return $this->getOrder()->getPayment()->getMethod()=='authorizenet_directpost' ? parent::toHtml() : '';
    }
    public function getCcLast4()
    {
        return $this->getOrder()->getPayment()->decrypt($this->getOrder()->getPayment()->getCcLast4());
    }
    public function getAmountPaid()
    {
        return $this->getOrder()->getPayment()->getAmountPaid();
    }
    public function getCcType()
    {   
        return $this->getOrder()->getPayment()->getCcType();
    }
}   

So I found that last digits was stored in db already, and for saving card type I needed to extend Directpost model a little. And then just retrieve all required data from order payment model

Related Topic