Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a custom payment method in magento

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 762
    Comment on it

    Magento provides many payment methods by default but sometime we need to add some custom payment methods . So in this blog I am explaining how do we create basic custom payment method.


    First need to understand the structure of the files -

    1) app/etc/modules/Custom_Paymentmethod.xml
    2) app/code/local/Custom/Paymentmethod/etc/config.xml
    3) app/code/local/Custom/Paymentmethod/etc/system.xml
    4) app/code/local/Custom/Paymentmethod/sql/paymentmethod_setup/install-0.1.0.php
    5) app/code/local/Custom/Paymentmethod/Helper/Data.php
    6) app/code/local/Custom/Paymentmethod/Model/Paymentmethod.php
    7) app/code/local/Custom/Paymentmethod/Block/Form/Paymentmethod.php
    8) app/design/frontend/theme/default/template/paymentmethod/paymentmethod.phtml
    9) app/code/local/Custom/Paymentmethod/Block/Info/Paymentmethod.php

    We will create all files step by step -

    1) We will create the module file "Custom_Paymentmethod.xml" in "app/etc/modules/" folder with the following content -

    <?xml version="1.0"?>
    <config>
        <modules>
            <Custom_Paymentmethod>
                  <active>true</active>
                  <codePool>local</codePool>       
                  <depends>
                    <Mage_Payment />
                  </depends>
            </Custom_Paymentmethod>
          </modules>
    </config>
    

    2) Then we will create module configuration file "config.xml" in folder "app/code/local/Custom/Paymentmethod/etc/" folder with the following content -

        <?xml version="1.0"?>
        <config>
          <modules>
            <Custom_Paymentmethod>
              <version>0.1.0</version>
            </Custom_Paymentmethod>
          </modules>
      <global>
        <fieldsets>
          <sales_convert_quote_payment>
            <cstm_paymentamnt>
              <to_order_payment>*</to_order_payment>
            </cstm_paymentamnt>
          </sales_convert_quote_payment>
        </fieldsets>
    
        <helpers>
          <paymentmethod>
            <class>Custom_Paymentmethod_Helper</class>
          </paymentmethod>
        </helpers>
    
        <blocks>
          <paymentmethod>
            <class>Custom_Paymentmethod_Block</class>
          </paymentmethod>
        </blocks>
    
        <models>
          <paymentmethod>
            <class>Custom_Paymentmethod_Model</class>
          </paymentmethod>
        </models>
    
        <resources>
          <paymentmethod_setup>
            <setup>
              <module>Custom_Paymentmethod</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
          </paymentmethod_setup>
          <paymentmethod_write>
            <use>core_write</use>
          </paymentmethod_write>
          <paymentmethod_read>
            <use>core_read</use>
          </paymentmethod_read>
        </resources>
      </global>
    
      <default>
        <payment>
          <paymentmethod>
            <active>1</active>
            <model>paymentmethod/paymentmethod</model>
            <order_status>pending</order_status>
            <title>PaymentMethod</title>
            <allowspecific>0</allowspecific>
            <payment_action>sale</payment_action>
          </paymentmethod>
        </payment>
      </default>
    
      <frontend>
        <routers>
          <paymentmethod>
            <use>standard</use>
            <args>
              <module>Custom_Paymentmethod</module>
              <frontName>paymentmethod</frontName>
            </args>
          </paymentmethod>
        </routers>
      </frontend>
    </config>
    

    3) Create module system configuration file needed for backend settings "system.xml" in folder "app/code/local/Custom/Paymentmethod/etc/" folder with the following content -

    <?xml version="1.0"?>
    <config>    
      <sections>
        <payment>
          <groups>
            <paymentmethod translate="label" module="paymentmethod">
              <label>New custom payment</label>
              <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>
              <fields>
                <title translate="label">
                  <label>Title</label>
                  <frontend_type>text</frontend_type>
                  <show_in_default>1</show_in_default>
                  <show_in_website>1</show_in_website>
                  <show_in_store>0</show_in_store>
                  <sort_order>1</sort_order>
                </title>
                <active translate="label">
                  <label>Enabled</label>
                  <frontend_type>select</frontend_type>
                  <source_model>adminhtml/system_config_source_yesno</source_model>
                  <show_in_default>1</show_in_default>
                  <show_in_website>1</show_in_website>
                  <show_in_store>0</show_in_store>
                  <sort_order>2</sort_order>
                </active>
                <order_status translate="label">
                  <label>New order status</label>
                  <frontend_type>select</frontend_type>
                  <source_model>adminhtml/system_config_source_order_status</source_model>
                  <show_in_default>1</show_in_default>
                  <show_in_website>1</show_in_website>
                  <show_in_store>0</show_in_store>
                  <sort_order>3</sort_order>
                </order_status>
    
              </fields>
            </paymentmethod>
          </groups>
        </payment>
      </sections>
    </config>
    

    4) Now, we need to create the custom table fields to store the values. To modify the table we need to create the mysql installation file "install-0.1.0.php" in "app/code/local/Custom/Paymentmethod/sql/paymentmethod_setup/" folder with the following content -

    <?php
    $installer = $this;
    $installer->startSetup();
    
    $installer->run("
    ALTER TABLE `{$installer->getTable('sales/quote_payment')}` 
    ADD `cstm_paymentamnt` VARCHAR( 255 ) NOT NULL;
    
    ALTER TABLE `{$installer->getTable('sales/order_payment')}` 
    ADD `cstm_paymentamnt` VARCHAR( 255 ) NOT NULL;
    ");
    $installer->endSetup();
    

    5) Create helper file "Data.php" in "app/code/local/Custom/Paymentmethod/Helper/" with the following content -

    <?php
    class Custom_Paymentmethod_Helper_Data extends Mage_Core_Helper_Abstract
    {}
    

    6) Create model file "Paymentmethod.php" of the module inside folder "app/code/local/Custom/Paymentmethod/Model/" with the following content -

    <?php
    class Custom_Paymentmethod_Model_Paymentmethod extends Mage_Payment_Model_Method_Abstract
    {
      protected $_code  = 'paymentmethod';
      protected $_formBlockType = 'paymentmethod/form_paymentmethod';
      protected $_infoBlockType = 'paymentmethod/info_paymentmethod';
    
      public function assignData($data)
      {
    
        if (!($data instanceof Varien_Object)) {
                $data = new Varien_Object($data);
        }
    
        $info = $this->getInfoInstance();
    
        if ($data->getCstmPaymentamnt())
        {
          $info->setCstmPaymentamnt($data->getCstmPaymentamnt());
        }
    
    
        return $this;
      }
    
      public function validate()
      {
        parent::validate();
        $info = $this->getInfoInstance();
    
        if (!$info->getCstmPaymentamnt())
        {
          $errorCode = 'invalid';
          $errorMsg = $this->_getHelper()->__("Payment amount is required.");
        }
    
        if ($errorMsg) 
        {
          Mage::throwException($errorMsg);
        }
    
        return $this;
      }
    } 
    

    7) Now, create block file "Paymentmethod.php" in which we assign template file to show content in "app/code/local/Custom/Paymentmethod/Block/Form/" folder with the following content -

    <?php
    class Custom_Paymentmethod_Block_Form_Paymentmethod extends Mage_Payment_Block_Form
    {
        protected function _construct()
        {
            parent::_construct();
            $this->setTemplate('paymentmethod/form/paymentmethod.phtml');
        }
    }
    

    8) Create template file "paymentmethod.phtml" which is needed to show the payment form on selection of the payment method "app/design/frontend/theme/default/template/paymentmethod/" -

    <?php $_code=$this->getMethodCode(); ?>
    <div class="form-list" id="payment_form_<?php echo $_code; ?>" style="display:none;">
      <div>
        <label><?php echo $this->__('Custom Amount') ?>*</label>
        <span>
          <input type="text" title="<?php echo $this->__('Custom amount') ?>" name="payment[cstm_paymentamnt]" value="<?php echo $this->htmlEscape($this->getInfoData('cstm_paymentamnt')) ?>" id="<?php echo $_code ?>_cstm_paymntamnt" />
        </span>
      </div>
     </div>
    <div>
        <?php echo $this->getMethod()->getConfigData('message');?>
    </div>
    

    9) Create file Block file "Paymentmethod.php" which is needed to show the custom amount price in sidebar of the checkout page "app/code/local/Custom/Paymentmethod/Block/Info/" with the following content -

    <?php
    class Custom_Paymentmethod_Block_Info_Paymentmethod extends Mage_Payment_Block_Info
    {
      protected function _prepareSpecificInformation($transport = null)
      {
        if (null !== $this->_paymentSpecificInformation) 
        {
          return $this->_paymentSpecificInformation;
        }
    
            $info = $this->getInfo();
    
            $transport = new Varien_Object();
            $transport = parent::_prepareSpecificInformation($transport);
    
            $transport->addData(array(
                Mage::helper('payment')->__('Payment Amount') => $this->getInfo()->getCstmPaymentamnt(),
            ));
    
            return $transport;
      }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: