Devising a smooth payment method in an Ecommerce website is crucial in converting a casual visitor into a customer. Any unpleasant experience, especially in the payment process, will raise questions on the integrity and security of the website.

Magento provides a wide array of payment modules built-in with the system.  With the changing market scenarios, we have different kinds of payments methods and gateways in the market. In few instances, we have noticed that there are few unique payment gateways which require additional information for security. We were not able to find a suitable module in the marketplace. In such cases, it’s recommended to build a customized module that handles the complete information required for the payment gateway.

Let’s have a look at building one such customized payment gateway in Magento.

MAGENTO CUSTOM PAYMENT MODULE

To begin, we need to create our own Namespace_Module.xml file with our code pool.

Create config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.1.0</version>
        </Namespace_Module>
    </modules>
    <global>
       <blocks>
           <namespace_module>
               <class>Namespace_Module_Block</class>
           </namespace_module>
        </blocks>
        <helpers>
            <namespace_module>
                <class>Namespace_Module_Helper</class></namespace_module>
        </helpers>
        <models>
            <namespace_module><class>Namespace_Module_Model</class>     
            </namespace_module>
        </models>
    </global>
    <frontend>
        <routers>
            <namespace_module>
                <use>standard</use>
                <args>
                    <module>Namespace_Module</module>
                    <frontName>your own frontname</frontName>
                </args>
            </namespace_module>
        </routers>
        <layout>
            <updates>
                <namespace_module>
                    <file>modulename/layout.xml</file>
                </namespace_module>
            </updates>
        </layout>
    </frontend>
    <default>
         <payment>
             <modulename_paymentmethodname>
                 <active>1</active>
                 <model>namespace_module/method_paymentmethodname</model>
                 <title>Payment Gateway</title>
                 <order_status>pending</order_status>
                 <language>EN</language>
                 <url>gateway url</url>
                 <allowspecific>0</allowspecific>
                 <group>offline</group>
             </modulename_paymentmethodname>
         </payment>
     </default>
</config>

MAGENTO CUSTOM PAYMENT MODULE

Create system.xml file

<?xml version="1.0"?>
<config>
  <sections>
    <payment>
      <groups>
        <modulename_paymentmethodname translate="label">
          <label>Your Payment Title</label>
          <frontend_type>text</frontend_type>
          <sort_order>8000</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
          <fields>
            <active translate="label">
              <label>Enabled</label>
              <frontend_type>select</frontend_type>
              <source_model>adminhtml/
                system_config_source_yesno</source_model>
              <sort_order>1</sort_order>
              <show_in_default>1</show_in_default>
              <show_in_website>1</show_in_website>
              <show_in_store>0</show_in_store>
            </active>
           <order_status translate="label">
             <label>New Order Status</label>
             <frontend_type>select</frontend_type>
             <source_model>adminhtml
               /system_config_source_order_status_new</source_model>
             <sort_order>2</sort_order>
             <show_in_default>1</show_in_default>
             <show_in_website>1</show_in_website>
             <show_in_store>0</show_in_store>
           </order_status>
           <title translate="label">
             <label>Title</label>
             <frontend_type>text</frontend_type>
             <sort_order>1</sort_order>
             <show_in_default>1</show_in_default>
             <show_in_website>1</show_in_website>
             <show_in_store>1</show_in_store>
           </title>
          <url translate="label">
            <label>Gateway URL</label>
            <frontend_type>text</frontend_type>
            <sort_order>24</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
          </url>
          <guid translate="label comment">
            <label>Merchant GUID</label>
            <comment></comment>
            <backend_model>adminhtml/
              system_config_backend_encrypted</backend_model>
            <frontend_type>obscure</frontend_type>
            <sort_order>25</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </guid>
          <shared_key translate="label comment">
            <label>Shared Key</label>
            <comment></comment>
            <backend_model>adminhtml/system_config_backend_encrypted
            </backend_model>
            <frontend_type>obscure</frontend_type>
            <sort_order>26</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </shared_key>
          <error_email translate="label">
            <label>Error email recipient</label>
            <frontend_type>text</frontend_type>
            <sort_order>27</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </error_email>
          <debug translate="label">
            <label>Debug</label>
            <frontend_type>select</frontend_type>
            <source_model>adminhtml/system_config_source_yesno
            </source_model>
            <sort_order>28</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website> 
            <show_in_store>0</show_in_store>
          </debug>
          <cctypes translate="label">
            <label>Credit Card Types</label>
            <frontend_type>multiselect</frontend_type>
            <source_model>adminhtml/system_config_source_payment_cctype
            </source_model>
            <sort_order>40</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
            <can_be_empty>1</can_be_empty>
          </cctypes>
          <useccv translate="label">
            <label>Request Card Security Code</label>
            <frontend_type>select</frontend_type>
            <source_model>adminhtml/system_config_source_yesno
            </source_model>
            <sort_order>41</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </useccv>
          <allowspecific translate="label">
            <label>Payment from Applicable Countries</label>
            <frontend_type>allowspecific</frontend_type>
            <sort_order>50</sort_order> 
            <source_model>
              adminhtml/system_config_source_payment_allspecificcountries
            </source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </allowspecific>
          <specificcountry translate="label">
            <label>Payment from Specific Countries</label>
            <frontend_type>multiselect</frontend_type>
            <sort_order>51</sort_order>
            <source_model>
              adminhtml/system_config_source_country
            </source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
            <can_be_empty>1</can_be_empty>
          </specificcountry>
          <min_order_total translate="label">
            <label>Minimum Order Total</label>
            <frontend_type>text</frontend_type>
            <sort_order>98</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </min_order_total>
          <max_order_total translate="label">
            <label>Maximum Order Total</label>
            <frontend_type>text</frontend_type>
            <sort_order>99</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </max_order_total>
          <sort_order translate="label">
            <label>Sort Order</label>
            <frontend_type>text</frontend_type>
            <sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
          </sort_order>
          </fields>
        </modulename_paymentmethodname>
      </groups>
    </payment>
  </sections>
</config

Note: The Payment Module will just Store the Credit Card for further process.

In the config.xml file

<default>        <payment>    <modulename_paymentmethodname>  <model>namespace_module/method_paymentmethodname</model>

and system.xml file

<sections>      <payment>   <groups>  <modulename_paymentmethodname translate="label">

In all above three instances, paymentmethodname should be the same.

To display your payment method input fields. Create your own block and phtml file.

class Namespace_module_Block_Form_paymentmethodname extends     Mage_Payment_Block_Form_Cc
{
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('module/form/paymentmethodname.phtml');
    }
}

Finally create the payment model which will capture the credit card information and store for future use.

<?php
class Namespace_Module_Model_Method_Paymentmethodname extends Mage_Payment_Model_Method_Ccsave {
    /**
    * Assign data to info model instance
    *
    * @param mixed $data
    * @return Mage_Payment_Model_Info
    */
    public function assignData($data)
    {
        if (!($data instanceof Varien_Object)) 
        {
            $data = new Varien_Object($data);
        }
        $info = $this->getInfoInstance();
       $info->setCcType($data->getCcType())
       ->setCcExpMonth($data->getCcExpMonth())
       ->setCcExpYear($data->getCcExpYear())
       ->setCcNumber($data->getCcNumber())
       ->setCcLast4($this->_getLast4($data->getCcNumber()))
       ->setCcOwner($data->getCcOwner());
       return $this;
    }
}

Implementing the above mentioned code creates a custom payment module which enables retailers to get the necessary data from the customers. Once the data has been obtained, developers need to work on the business logic to use the data in an efficient way.

In a recent study by Forrester, 24% of shopping cart abandonment happens due to limited payment options. With new payment options and gateways surfacing in the market, it’s necessary for retailers to provide adequate payment options to empower a successful purchase.

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Request a Quote Today

Request a Quote