Magento支付方式与表单字段。错误:无法检索付款方式模型对象

时间:2015-05-05 07:42:01

标签: magento payment-gateway

我正在为结帐时有一个表单字段的新支付网关(Pay On Account)创建一个模块(仅在用户登录并拥有帐户时显示)。 一切都工作正常:结账时正确捕获表单字段并保存在sales_flat_order& sales_flat_quote表。 订单代码也正在表格中正确记录(payonaccount) 问题是,当我尝试在管理员中查看订单时,我收到以下错误:

Cannot retrieve the payment method model object.

其他SO回答提到我应该更正数据库中的付款类型,因为付款类型是正确的

这是我的代码(全部!抱歉,谢谢!):

文件结构:

enter image description here

应用\代码\本地\萨勒曼\ PayOnAccount \块\表格\ Payonaccount.php

<?php

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

应用\代码\本地\萨勒曼\ PayOnAccount \块\信息\ Payonaccount.php

<?php

class Sulman_PayOnAccount_Block_Info_Payonaccount extends Mage_Payment_Block_Form
{
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('payment/info/payonaccount.phtml');
    }

    public function getMethod()
    {
        return parent::getMethod();
    }
}

应用\代码\本地\萨勒曼\ PayOnAccount \等\ config.xml中

<?xml version="1.0"?>
<config>
    <modules>
        <Sulman_PayOnAccount>
            <version>0.1.0</version>
        </Sulman_PayOnAccount>
    </modules>
    <global>

        <blocks>
            <payonaccount>
                <class>Sulman_PayOnAccount_Block</class>
            </payonaccount>
        </blocks>


        <models>
            <payonaccount>
                <class>Sulman_PayOnAccount_Model</class>
            </payonaccount>
        </models>
        <helpers>
            <payonaccount>
                <class>Sulman_PayOnAccount_Helper</class>
            </payonaccount>
        </helpers>
        <payment>
            <groups>
                <payonaccount>PayOnAccount</payonaccount>
            </groups>
        </payment>

        <!-- define install scripts -->
        <resources>
            <payonaccount_setup>
                <setup>
                    <module>Sulman_PayOnAccount</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </payonaccount_setup>
        </resources>
        <fieldsets>
            <sales_convert_quote_payment>
                <poa_ponumber>
                    <to_order_payment>*</to_order_payment>
                </poa_ponumber>
            </sales_convert_quote_payment>
            <sales_convert_quote_payment>
                <poa_ponumber>
                    <to_order_payment>*</to_order_payment>
                </poa_ponumber>
            </sales_convert_quote_payment>

            <sales_convert_quote>
                <poa_ponumber>
                    <to_order>*</to_order>
                </poa_ponumber>
            </sales_convert_quote>

        </fieldsets>
    </global>
    <default>
        <payment>
            <payonaccount>
                <model>payonaccount/payonaccount</model>
                <active>1</active>
                <order_status>pending</order_status>
                <title>Pay On Account</title>
                <payment_action>sale</payment_action>
                <allowspecific>0</allowspecific>
                <sort_order>1</sort_order>
            </payonaccount>
        </payment>
    </default>
</config>

应用\代码\本地\萨勒曼\ PayOnAccount \模型\ Payonaccount.php

<?php
class Sulman_PayOnAccount_Model_Payonaccount extends Mage_Payment_Model_Method_Abstract
{
    protected $_code = 'payonaccount';
    protected $_formBlockType = 'payonaccount/form_payonaccount';
    protected $_infoBlockType = 'payonaccount/info_payonaccount';


    public function isAvailable($quote = null) {
        $isLoggedIn = Mage::helper('customer')->isLoggedIn();
        $canPayOnAccount = false;

        if(Mage::getSingleton('customer/session')->isLoggedIn()) {
            $customerData = Mage::getSingleton('customer/session')->getCustomer();
            $customerObj = Mage::getModel('customer/customer')->load($customerData->getId());
            $hasAccount = $customerObj->getData('has_account'); // 1 = No, 2 = Yes

            if( is_null($hasAccount) OR $hasAccount == 1){
                $canPayOnAccount = false;
            } elseif($hasAccount == 2){
                $canPayOnAccount = true;
            }
        }       

        return parent::isAvailable($quote) && $canPayOnAccount;
    }

    public function assignData($data)
    {
        if (!($data instanceof Varien_Object)) {
            $data = new Varien_Object($data);
        }



        Mage::log("getPoaPonumber: ".$data->getPoaPonumber(), null, 'sulman.log');
        $this->getInfoInstance()->setPoaPonumber($data->getPoaPonumber());
        return $this;
    }
}

应用\代码\本地\萨勒曼\ PayOnAccount \ SQL \ payonaccount_setup \ mysql4安装-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();
$installer->run("

ALTER TABLE `{$installer->getTable('sales/quote')}` ADD `poa_ponumber` VARCHAR( 255 ) NOT NULL ;
ALTER TABLE `{$installer->getTable('sales/order')}` ADD `poa_ponumber` VARCHAR( 255 ) NOT NULL ;
");
$installer->endSetup();

应用程序\设计\ adminhtml \ DEFAULT \ DEFAULT \模板\金\工具\ payonaccount.phtml

<?php
echo $this->getMethod();
?>

<p><?php //echo $this->escapeHtml($this->getMethod()->getTitle()) ?></p>

应用\设计\前端\ cmtgroup \默认\模板\付款\形式\ payonaccount.phtml

<ul class="form-list" id="payment_form_<?php echo $this->getMethodCode() ?>" style="display:none;">
    <li>
        <label for="poa_ponumber" class="required"><em>*</em><?php echo $this->__('Purchase Order Number') ?></label>
        <div class="input-box">
            <input type="text" id="poa_ponumber" name="payment[poa_ponumber]" title="<?php echo $this->__('Purchase Order Number') ?>" class="input-text" value="<?php echo $this->escapeHtml($this->getInfoData('poa_ponumber')) ?>" />
        </div>
    </li>
</ul>

如果我尝试在其他地方加载订单并获得poa_number,那么:

$_order->getPoaNumber();

它没有任何回报。

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:0)

好的,所以我已经解决了这个问题......我在sales_order_save_after事件中添加了一个观察员,它获取了付款信息并根据订单记录了poa_ponumber:

public function saveBefore(Varien_Event_Observer $observer){
    // get the quote of the order so we can get the quote payment type poa_ponumber
    $order = $observer->getEvent()->getOrder();
    $quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($order->getQuoteId());
    $poaponumber = $quote->getPayment()->getPoaPonumber();
    $order->setPoaPonumber($poaponumber);
}

这会将poa_ponumber保存到平面订单表中,然后可以通过正常方式轻松提取:$_order->getPoaPonumber();

我确信这不是正确的方法,但它有效。

答案 1 :(得分:0)

请检查付款方式模型中是否存在$ _code。

您可以参考:

Mage_Payment_Model_Method_Abstract

行:343

public function getCode() { if (empty($this->_code)) { Mage::throwException(Mage::helper('payment')->__('Cannot retrieve the payment method code.')); } return $this->_code; }