天天看点

Create Payment Method Module

Javaeye的一些作者也有相同的文章(都是从官网直接贴过来的),不过我还打算自己也发布一下,方便读者不用到处找Magento的资料。

废话不说,要新增一个Magento支付模块的话,最好单独出来定义;当然也可以跟其他的module定义在同一个Module中如果有需要的话。

下面我们将新增一个带可以完成下面列表的功能的payment方法的module:

  • 读取信用卡的信息
  • 交易提交前进行验证
  • 在订单支付记录中记录该交易的ID

假设这个module叫做NewModule.,当然这个名字是任意的。

首先确保你的 app/code/local

 在 include_path中(你不能确定的话,请看我前面的文章有相关的代码完成该功能).

如果你的system->cache Management 中的configuration cache是开着的话,把它Disable,或者在更新*.xml配置文件时reset/reflash一下。

 新建 

app/etc/modules/CompanyName_NewModule.xml

:

<config>
    <modules> 
<!-- declare CompanyName_NewModule module -->
        <CompanyName_NewModule> 
<!-- this is an active module -->
            <active>true</active> 
<!-- this module will be located in app/code/local code pool -->
            <codePool>local</codePool> 
<!-- specify dependencies for correct module loading order -->
            <depends> 
                <Mage_Payment /> 
            </depends> 
        </CompanyName_NewModule> 
    </modules> 
</config>
           

Magento在初始化配置的时候自动到etc/目录下读取相关的xml文件。分析完上面的代码知道你新增一个module

Create

app/code/local/CompanyName/NewModule/etc/config.xml

:

<?xml version="1.0"?>
<config>
    <modules> 
       <CompanyName_NewModule> 
<!-- declare module's version information for database updates -->
          <version>0.1.0</version> 
       </CompanyName_NewModule> 
    </modules> 
 
    <global> 
<!-- IMPORTANT: if you use your own namespace (i.e. CompanyName) you also have to declare blocks group for new module. See topic: http://www.magentocommerce.com/boards/viewthread/22416/#t102732 -->
    <blocks> 
        <newmodule> 
            <class>CompanyName_NewModule_Block</class> 
        </newmodule> 
    </blocks> 
 
<!-- declare model group for new module -->
        <models> 
<!-- model group alias to be used in Mage::getModel('newmodule/...') -->
            <newmodule> 
<!-- base class name for the model group -->
                <class>CompanyName_NewModule_Model</class> 
            </newmodule> 
        </models> 
 
<!-- declare resource setup for new module -->
        <resources> 
<!-- resource identifier -->
            <newmodule_setup> 
<!-- specify that this resource is a setup resource and used for upgrades -->
                <setup> 
<!-- which module to look for install/upgrade files in -->
                    <module>CompanyName_NewModule</module> 
                </setup> 
<!-- specify database connection for this resource -->
                <connection> 
<!-- do not create new connection, use predefined core setup connection -->
                    <use>core_setup</use> 
                </connection> 
            </newmodule_setup> 
            <newmodule_write> 
                <connection> 
                  <use>core_write</use> 
                </connection> 
            </newmodule_write> 
            <newmodule_read> 
               <connection> 
                <use>core_read</use> 
              </connection> 
            </newmodule_read> 
        </resources> 
    </global> 
 
<!-- declare default configuration values for this module -->
    <default> 
<!-- 'payment' configuration section (tab) -->
        <payment> 
<!-- 'newmodule' configuration group (fieldset) -->
            <newmodule> 
<!-- by default this payment method is inactive -->
                <active>0</active> 
<!-- model to handle logic for this payment method -->
                <model>newmodule/paymentMethod</model> 
<!-- order status for new orders paid by this payment method -->
                <order_status>pending</order_status> 
<!-- default title for payment checkout page and order view page -->
                <title>Credit Card (Authorize.net)</title> 
 
                <cctypes>AE,VI,MC,DI</cctypes> 
                <payment_action>authorize</payment_action> 
                <allowspecific>0</allowspecific> 
            </newmodule> 
         </payment> 
    </default> 
</config>
           

上面的xml文档定义了一些配置选项,在Magento admin panel System > Configuration 你将会看到这些选项。

Create

app/code/local/CompanyName/NewModule/etc/system.xml

:

<?xml version="1.0"?>
<config>
   <sections> 
<!-- payment tab -->
        <payment> 
            <groups> 
<!-- newmodule fieldset -->
                <newmodule translate="label" module="paygate"> 
<!-- will have title 'New Module' -->
                    <label>New Module</label> 
<!-- position between other payment methods -->
                    <sort_order>670</sort_order> 
<!-- do not show this configuration options in store scope -->
                    <show_in_default>1</show_in_default> 
                    <show_in_website>1</show_in_website> 
                    <show_in_store>0</show_in_store> 
                    <fields> 
<!-- is this payment method active for the website? -->
                        <active translate="label"> 
<!-- label for the field -->
                            <label>Enabled</label> 
<!-- input type for configuration value -->
                            <frontend_type>select</frontend_type> 
<!-- model to take the option values from -->
                            <source_model>adminhtml/system_config_source_yesno</source_model> 
<!-- field position -->
                            <sort_order>1</sort_order> 
<!-- do not show this field in store scope -->
                            <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_processing</source_model> 
                            <sort_order>4</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>2</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> 
                </newmodule> 
            </groups> 
        </payment> 
    </sections> 
</config>
           

打开 Admin / System / Configuration / Payment Methods, 看到"New Module” group.Enable之,然后检测一下新模块是否可以工作了. 在payment methods可以看到"New Module” payment method并且带有填写信用卡信息的表单.

Create

app/code/local/CompanyName/NewModule/sql/newmodule_setup/mysql4-install-0.1.0.php

:

<?php
// here are the table creation for this module e.g.:
$this->startSetup();
$this->run("HERE YOUR SQL");
$this->endSetup();
           

定义版本信息:

<modules> 
       <CompanyName_NewModule> 
          <version>0.2.0</version> 
       </CompanyName_NewModule> 
    </modules> 
      

然后 create

app/code/local/CompanyName/NewModule/sql/newmodule_setup/mysql4-upgrade-0.1.0-0.2.0.php

:

<?php // here are the table updates for this module e.g.:
 $this->startSetup();
 $this->run("HERE YOUR UPDATE SQL");
 $this->endSetup();
           

 注意事项:

  • Dont put your module in /Mage. It belongs in

    app/code/community/

    or

    app/code/local

  • Make sure your module’s first letter is capitalized. newmodule apparently will not work, it must start with a capital letter Newmodule.
  • Also make sure that the recipient folder of the module’s folder (CompanyName in the example) is capitalized as well. companyName doesn’t seem to work, either.
  • If your module is not showing in configuration>advanced then check your config.xml
  • If your module shows in the list of modules (configuration>advanced) but not in the Payment Methods, your problem is probably in system.xml
  • Make sure you clear the cache.