Extending Magento Payment Module: Displaying Advanced Payment Information
Overview
When implementing a custom payment module for Magento, you might want to display detailed information about each transaction in the backend. This includes data like Card Validation Status, Address Validation Status, and Last Transaction ID. In this guide, we’ll walk through how to achieve this by extending an existing payment method.
Understanding the Payment Method Structure
Magento follows a specific structure for payment methods located at app/code/local/Mage/Payment/etc/config.xml. Each payment method is defined under <payment_modes> with attributes like model, title, and code.
For our custom module, let’s assume we have a file named My_Custom_Payment under app/code/local. The configuration for this payment method would look something like this:
<payment_modes>
<my_custom_payment>
<model>custompayment/my_capture_model</model>
<title>Custom Payment Method</title>
<code>my_custom_payment</code>
<sort_order>0</sort_order>
<active>1</active>
</my_custom_payment>
</payment_modes>
Setting the Payment Information
To set advanced payment information, we need to override the Mage_Payment_Model_Info class. This class is responsible for displaying transaction details in the backend.
We can create a new block file under our custom module’s directory:
app/code/local/CustomPayment/Block/Payment/Info.php
In this file, we extend Mage_Core_Block_Text_List:
class CustomPayment_Block_Payment_Info extends Mage_Core_Block_Text_List {
// Override the default template
protected function _toHtml() {
return $this->getChildHtml('info');
}
}
Next, we need to specify our new block in the layout XML:
<layout version="0.1.0">
<payment_info_index>
<block type="core/text_list" name="my_custom_payment_info" as="info" template="custompayment/payment/info.phtml">
<action method="setItemClassName"><value>item</value></action>
</block>
</payment_info_index>
</layout>
Displaying Advanced Payment Information
To display the desired information (like Card Validation Status, Address Validation Status), we need to update the info.phtml template. This file should already exist under our custom module’s directory:
app/design/adminhtml/default/default/template/custompayment/payment/
We can fetch and display additional data from the payment transaction object using Magento’s native functions:
<?php if ($this->getItem()): ?>
<table class="data-table" cellpadding="0" cellspacing="0">
<tr>
<th><?= $this->escapeHtml($this->__('Card Validation Status')) ?></th>
<td><?= $this->escapeHtml($this->getItem()->getCvnStatus()) ?></td>
</tr>
<tr>
<th><?= $this->escapeHtml($this->__('Address Validation Status')) ?></th>
<td><?= $this->escapeHtml($this->getItem()->getAvsStatus()) ?></td>
</tr>
<tr>
<th><?= $this->escapeHtml($this->__('Last Transaction ID')) ?></th>
<td><?= $this->escapeHtml($this->getItem()->getTransactionId()) ?></td>
</tr>
</table>
<?php endif; ?>
Conclusion
Displaying advanced payment information in the Magento backend requires a combination of extending the core Mage_Payment_Model_Info class, creating custom block and template files, and updating the layout XML.
Remember to clear your cache after making these changes for the updates to take effect. Also, ensure you follow best practices like using proper version control systems (like Git) when working with Magento extensions.
For more in-depth guides on extending Magento functionality, I highly recommend checking out our Magento Extensions Guide.