在Magento中添加一个自己的支付模块----第一部分

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Magento中添加一个自己的支付模块----第一部分相关的知识,希望对你有一定的参考价值。

https://code.tutsplus.com/tutorials/create-a-custom-payment-method-module-in-magento-part-one--cms-23464

首先,要对Magento的基础模型创建程序很熟悉;

国外的一个网址:https://code.tutsplus.com/tutorials/magento-custom-module-development--cms-20643;

这个是国内的比较好的博客文章:http://blog.csdn.net/suese/article/category/1916481

 

自定义模块的代码池:local

自定义模块的命名空间:Envato

自定义模块的名字:Custompaymentmethod

需要创建的文件:

  • app/etc/modules/Envato_All.xml: 这个文件让自定义模块可用;
  • app/code/local/Envato/Custompaymentmethod/etc/config.xml: 这个配置文件用来声明自定义模块;
  • app/code/local/Envato/Custompaymentmethod/etc/system.xml: 这个是系统配置文件,用来为自定义的支付方式设置配置项;
  • app/code/local/Envato/Custompaymentmethod/sql/custompaymentmethod_setup/install--1.0.0.0.php:这个是和数据库相关的文件,用来给自定义支付方式创建自定义的字段;

创建app/etc/modules/Envato_All.xml   

<?xml version="1.0"?>

<config>
  <modules>
    <Envato_Custompaymentmethod>
      <active>true</active>
      <codePool>local</codePool>
      <depends>
        <Mage_Payment />
      </depends>
    </Envato_Custompaymentmethod>
  </modules>
</config>
  说明:这个配置文件指定了代码池 ,依赖的核心模块
创建app/code/local/Envato/Custompaymentmethod/sql/custompaymentmethod_setup/install--1.0.0.0.php
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
ALTER TABLE `{$installer->getTable(‘sales/quote_payment‘)}`
ADD `custom_field_one` VARCHAR( 255 ) NOT NULL,
ADD `custom_field_two` VARCHAR( 255 ) NOT NULL;
ALTER TABLE `{$installer->getTable(‘sales/order_payment‘)}`
ADD `custom_field_one` VARCHAR( 255 ) NOT NULL,
ADD `custom_field_two` VARCHAR( 255 ) NOT NULL;
");
$installer->endSetup();
  说明:这里在支付模块相关的表单中创建了两个字段;
创建 app/code/local/Envato/Custompaymentmethod/etc/config.xml
<?xml version="1.0"?>
<config>
  <modules>
    <Envato_Custompaymentmethod>
      <version>1.0.0.0</version>
    </Envato_Custompaymentmethod>
  </modules>
  <global>
    <fieldsets>
      <sales_convert_quote_payment>
        <custom_field_one>
          <to_order_payment>*</to_order_payment>
        </custom_field_one>
        <custom_field_two>
          <to_order_payment>*</to_order_payment>
        </custom_field_two>
      </sales_convert_quote_payment>
    </fieldsets>
    <helpers>
      <custompaymentmethod>
        <class>Envato_Custompaymentmethod_Helper</class>
      </custompaymentmethod>
    </helpers>
    <blocks>
      <custompaymentmethod>
        <class>Envato_Custompaymentmethod_Block</class>
      </custompaymentmethod>
    </blocks>
    <models>
      <custompaymentmethod>
        <class>Envato_Custompaymentmethod_Model</class>
      </custompaymentmethod>
    </models>
    <resources>
      <custompaymentmethod_setup>
        <setup>
          <module>Envato_Custompaymentmethod</module>
        </setup>
      </custompaymentmethod_setup>
    </resources>
  </global>
  <default>
    <payment>
      <custompaymentmethod>
        <active>1</active>
        <model>custompaymentmethod/paymentmethod</model>
        <order_status>pending</order_status>
        <title>CustomPaymentMethod</title>
        <allowspecific>0</allowspecific>
        <payment_action>sale</payment_action>
      </custompaymentmethod>
    </payment>
  </default>
  <frontend>
    <routers>
      <custompaymentmethod>
        <use>standard</use>
        <args>
          <module>Envato_Custompaymentmethod</module>
          <frontName>custompaymentmethod</frontName>
        </args>
      </custompaymentmethod>
    </routers>
  </frontend>
</config>
说明:
1、在用户确认订单的时候,希望用户可以看见我们的支付方式,当用户选择我们的支付方式的时候,给用户提供两输入信息的提示框;
2、<fieldsets> 、<sales_convert_quote_payment>这两个标签告诉Magento将这些字段信息和客户订单信息一起保存;
3、helpers、blocks、models标签是其他部分的补充;
4、resources用来定义资源,使用的是<custompaymentmethod_setup>标签;Magento会检测这个文件,然后为我们的模块安装必要的SQL脚本;
5、 <payment> 、<custompaymentmethod>将我们的支付模块插入到Magento的支付模块中;这标签里面可以继续创建支付模块的字段,如支付的状态、订单的状态等;
6、<frontend>、<routers>在前端给支付方式指定了路由;
 
创建 app/code/local/Envato/Custompaymentmethod/etc/system.xml
<?xml version="1.0"?>
<config>   
  <sections>
    <payment>
      <groups>
        <custompaymentmethod translate="label" module="custompaymentmethod">
          <label>CustomPaymentMethod Module</label>
          <sort_order>1000</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>
            <allowspecific translate="label">
              <label>Payment from applicable countries</label>
              <frontend_type>allowspecific</frontend_type>
              <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>1</show_in_store>
              <sort_order>4</sort_order>
            </allowspecific>
            <specificcountry translate="label">
              <label>Payment from Specific countries</label>
              <frontend_type>multiselect</frontend_type>
              <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>1</show_in_store>
              <sort_order>5</sort_order>
            </specificcountry>
          </fields>
        </custompaymentmethod>
      </groups>
    </payment>
  </sections>
</config>
  说明:为我们的支付方式配置字段的;
创建/app/code/local/Envato/Custompaymentmethod/Helper/Data.php
class Envato_Custompaymentmethod_Helper_Data extends Mage_Core_Helper_Abstract{
  //
}

验证: System > Configuration > Sales > Payment Methods查看我们自己的支付方式会显示在列表中;
 
原文地址:https://code.tutsplus.com/tutorials/create-a-custom-payment-method-module-in-magento-part-one--cms-23464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

以上是关于在Magento中添加一个自己的支付模块----第一部分的主要内容,如果未能解决你的问题,请参考以下文章

Magento:使用 PayPal Express 时添加支付费用 [关闭]

Magento 2在结帐页面phtml中发送到支付网关之前获取订单ID?

Magento:如何为部分付款创建模块?

用于多运输的 magento 自定义支付模块

Magento 自定义支付网关

Magento 支付工作流程和事件订单已支付