Magento 1.9 – Fix Custom Payment Module Error on Frontend

magento-1.9payment

I learn the Magento 7 days ago, and I try to create a custom model for payment method. I have a problem and I working 3 days, but I cant resolve.
If I change in config.xml:

<models>
    <internship_payment>
        <class>Internship_Payment_Model</class>
    </internship_payment>
</models>

<internship_payment> in <payment> admin/system/confuguration/payment methods show me this error:

Call to a member function setAllowedTypes() on boolean in
C:\xampp\htdocs\intership\magento\app\code\core\Mage\Paypal\Model\Config.php
on line 1092

If I keep the internship_payment, admin work, but i get this error on frontend:

Call to a member function setMethod() on boolean in
C:\xampp\htdocs\intership\magento\app\code\core\Mage\Payment\Helper\Data.php
on line 106

I follow this tutorial.

  • Namespace: Internship
  • Module: Payment

config.xml (app/Code/Local/Internship/Payment/etc/config.xml)

<config>
    <modules>
        <Internship_Payment>
            <version>0.0.1</version>
        </Internship_Payment>
    </modules>

    <default>
        <payment>
            <payment>
                <active>1</active>
                <model>payment/pay</model>
                <order_status>processing</order_status>
                <title>Excellence Payment Method2</title>
            </payment>
        </payment>
    </default>

    <global>
        <fieldsets>
            <sales_convert_quote_payment>
                <check_no>
                    <to_order_payment>*</to_order_payment>
                </check_no>
                <check_date>
                    <to_order_payment>*</to_order_payment>
                </check_date>
            </sales_convert_quote_payment>
        </fieldsets>

        <blocks>
            <payment>
                <class>Internship_Payment_Block</class>
            </payment>
        </blocks>

        <models>
            <internship_payment>
                <class>Internship_Payment_Model</class>
            </internship_payment>
        </models>

        <resources>
            <payment_custom_setup>
                <setup>
                    <module>Internship_Payment</module>
                </setup>
            </payment_custom_setup>
        </resources>
    </global>

    <frontend>
        <routers>
            <payment>
                <use>standard</use>
                <args>
                    <module>Internship_Payment</module>
                    <frontName>payment</frontName>
                </args>
            </payment>
        </routers>
    </frontend>
</config>

system.xml (app/Code/Local/Internship/Payment/etc/config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <sections>
        <payment>
            <groups>
                <payment translate="label" module="payment">
                    <label>My Payment Module</label>
                    <sort_order>670</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>0</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>
                            <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>3</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </title>
                    </fields>
                </payment>
            </groups>
        </payment>
    </sections>
</config>

Pay.php (Model)

class Internship_Payment_Model_Pay extends Mage_Payment_Model_Method_Abstract
{
    protected $_code = 'payment';

    protected $_formBlockType = 'payment/form_pay';

    protected $_infoBlockType = 'payment/info_pay';

    public function assignData($data)
    {
        if (!($data instanceof Varien_Object)) {
            $data = new Varien_Object($data);
        }
        $info = $this->getInfoInstance();
        $info->setCheckNo($data->getCheckNo())
            ->setCheckDate($data->getCheckDate());
        return $this;
    }


    public function validate()
    {
        parent::validate();

        $info = $this->getInfoInstance();

        $no = $info->getCheckNo();
        $date = $info->getCheckDate();
        if (empty($no) || empty($date)) {
            $errorCode = 'invalid_data';
            $errorMsg = $this->_getHelper()->__('Check No and Date are required fields');
        }

        if ($errorMsg) {
            Mage::throwException($errorMsg);
        }
        return $this;
    }

}

Pay.php (Block/Form)

class Internship_Payment_Block_Form_Pay extends Mage_Payment_Block_Form
{
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('payment/form/pay.phtml');
    }
}

Pay.php (Block/Info)

class Internship_Payment_Block_Info_Pay 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')->__('Check No#') => $info->getCheckNo(),
            Mage::helper('payment')->__('Check Date') => $info->getCheckDate()
        ));
        return $transport;
    }
}

Best Answer

You should apply the following changes to your code:

Internship_Payment.xml (app/etc/modules/Internship_Payment.xml):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Internship_Payment>
            <active>true</active>
            <codePool>local</codePool>
        </Internship_Payment>
    </modules>
</config>

config.xml (app/code/local/Internship/Payment/etc/config.xml):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Internship_Payment>
            <version>0.0.1</version>
        </Internship_Payment>
    </modules>

    <default>
        <payment>
            <internship_payment>
                <active>1</active>
                <model>internship_payment/pay</model>
                <order_status>processing</order_status>
                <title>Excellence Payment Method2</title>
            </internship_payment>
        </payment>
    </default>

    <global>
        <fieldsets>
            <sales_convert_quote_payment>
                <check_no>
                    <to_order_payment>*</to_order_payment>
                </check_no>
                <check_date>
                    <to_order_payment>*</to_order_payment>
                </check_date>
            </sales_convert_quote_payment>
        </fieldsets>

        <blocks>
            <internship_payment>
                <class>Internship_Payment_Block</class>
            </internship_payment>
        </blocks>

        <models>
            <internship_payment>
                <class>Internship_Payment_Model</class>
            </internship_payment>
        </models>

        <resources>
            <internship_payment_setup>
                <setup>
                    <module>Internship_Payment</module>
                </setup>
            </internship_payment_setup>
        </resources>

        <helpers>
            <internship_payment>
                <class>Internship_Payment_Helper</class>
            </internship_payment>
        </helpers>

    </global>

    <frontend>
        <routers>
            <internship_payment>
                <use>standard</use>
                <args>
                    <module>Internship_Payment</module>
                    <frontName>internship_payment</frontName>
                </args>
            </internship_payment>
        </routers>
    </frontend>
</config>

system.xml (app/code/local/Internship/Payment/etc/system.xml):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <sections>
        <payment>
            <groups>
                <internship_payment translate="label" module="internship_payment">
                    <label>My Payment Module</label>
                    <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>
                    <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>1</show_in_store>
                        </active>
                        <order_status translate="label">
                            <label>New order status</label>
                            <frontend_type>select</frontend_type>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </order_status>
                        <title translate="label">
                            <label>Title</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>3</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </title>
                    </fields>
                </internship_payment>
            </groups>
        </payment>
    </sections>
</config>

Pay.php (app/code/local/Internship/Payment/Model/Pay.php) :

<?php

class Internship_Payment_Model_Pay extends Mage_Payment_Model_Method_Abstract
{
    protected $_code = 'internship_payment';
    protected $_formBlockType = 'internship_payment/form_pay';
    protected $_infoBlockType = 'internship_payment/info_pay';

    public function assignData($data)
    {
        if (!($data instanceof Varien_Object)) {
            $data = new Varien_Object($data);
        }
        $info = $this->getInfoInstance();
        $info->setCheckNo($data->getCheckNo())
            ->setCheckDate($data->getCheckDate());
        return $this;
    }

    public function validate()
    {
        parent::validate();

        $info = $this->getInfoInstance();

        $no = $info->getCheckNo();
        $date = $info->getCheckDate();
        if (empty($no) || empty($date)) {
            $errorCode = 'invalid_data';
            $errorMsg = $this->_getHelper()->__('Check No and Date are required fields');
        }

        if ($errorMsg) {
            Mage::throwException($errorMsg);
        }
        return $this;
    }

}

Data.php (app/code/local/Internship/Payment/Helper/Data.php):

<?php

class Internship_Payment_Helper_Data extends Mage_Core_Helper_Abstract
{

}

Pay.php (app/code/local/Internship/Payment/Block/Info/Pay.php):

<?php

class Internship_Payment_Block_Info_Pay 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('internship_payment')->__('Check No#') => $info->getCheckNo(),
            Mage::helper('internship_payment')->__('Check Date') => $info->getCheckDate()
        ));

        return $transport;
    }
}

Pay.php (app/code/local/Internship/Payment/Block/Form/Pay.php):

<?php

class Internship_Payment_Block_Form_Pay extends Mage_Payment_Block_Form
{
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('internship/payment/form/pay.phtml');
    }
}

pay.phtml (app/design/frontend/base/default/template/internship/payment/form/pay.phtml):

<?php
?>
<ul class="form-list" id="payment_form_<?php echo $this->getMethodCode() ?>">
    <li>
        <?php echo $this->__('My Payment Module') ?>
    </li>
</ul>

Note: Do not use the Magento core folders!

The main reason for errors because of difference in naming. You should always use a single unique name. Your initial payment name is not unique, because the module Mage_Payment has the same name and it is impossible to use it. Everything else works correctly.

We checked your payment methods and it works:

enter image description here enter image description here

Related Topic