Create Payment Method Module

Last modified by Matchello on Fri, June 25, 2010 09:23
Source|Old Revisions  

This is an old revision of the document!


Introduction

Each payment method can be done as separate module or few methods can be combined in same module if they share functionality or could be used together.

Let’s create a module with one payment method that will:

  • accept credit card information
  • authorize it when order is submitted
  • save transaction ID in order payment record

Our new module will be called NewModule.

Replace all instances of ‘NewModule’ with name of your module and ‘newmodule’ with simplified code, that contains only alphanumeric characters and underscore.

To make this tutorial most concise, it’s implied that mentioned folders will be created when needed.

Make sure that app/code/local is in include_path.

If you are using configuration cache, don’t forget to reset it after modifying config xml files by deleting the contents of var/cache/config/

A good piece of advice: Disable caching before developing a new module.

Module Declaration

Create app/etc/modules/CompanyName_NewModule.xml:

  1. <config>
  2.     <modules>
  3. <!-- declare CompanyName_NewModule module -->
  4.         <CompanyName_NewModule>
  5. <!-- this is an active module -->
  6.             <active>true</active>
  7. <!-- this module will be located in app/code/local code pool -->
  8.             <codePool>local</codePool>
  9. <!-- specify dependencies for correct module loading order -->
  10.             <depends>
  11.                 <Mage_Payment />
  12.             </depends>
  13.         </CompanyName_NewModule>
  14.     </modules>
  15. </config>

Now that the application is aware of the module, we will let Magento know about details of our module.

Module Configuration

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

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.        <CompanyName_NewModule>
  5. <!-- declare module's version information for database updates -->
  6.           <version>0.1.0</version>
  7.        </CompanyName_NewModule>
  8.     </modules>
  9.  
  10.     <global>
  11. <!-- 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 -->
  12.     <blocks>
  13.         <newmodule>
  14.             <class>CompanyName_NewModule_Block</class>
  15.         </newmodule>
  16.     </blocks>
  17.  
  18. <!-- declare model group for new module -->
  19.         <models>
  20. <!-- model group alias to be used in Mage::getModel('newmodule/...') -->
  21.             <newmodule>
  22. <!-- base class name for the model group -->
  23.                 <class>CompanyName_NewModule_Model</class>
  24.             </newmodule>
  25.         </models>
  26.  
  27. <!-- declare resource setup for new module -->
  28.         <resources>
  29. <!-- resource identifier -->
  30.             <newmodule_setup>
  31. <!-- specify that this resource is a setup resource and used for upgrades -->
  32.                 <setup>
  33. <!-- which module to look for install/upgrade files in -->
  34.                     <module>CompanyName_NewModule</module>
  35.                 </setup>
  36. <!-- specify database connection for this resource -->
  37.                 <connection>
  38. <!-- do not create new connection, use predefined core setup connection -->
  39.                     <use>core_setup</use>
  40.                 </connection>
  41.             </newmodule_setup>
  42.             <newmodule_write>
  43.                 <connection>
  44.                   <use>core_write</use>
  45.                 </connection>
  46.             </newmodule_write>
  47.             <newmodule_read>
  48.                <connection>
  49.                 <use>core_read</use>
  50.               </connection>
  51.             </newmodule_read>
  52.         </resources>
  53.     </global>
  54.  
  55. <!-- declare default configuration values for this module -->
  56.     <default>
  57. <!-- 'payment' configuration section (tab) -->
  58.         <payment>
  59. <!-- 'newmodule' configuration group (fieldset) -->
  60.             <newmodule>
  61. <!-- by default this payment method is inactive -->
  62.                 <active>0</active>
  63. <!-- model to handle logic for this payment method -->
  64.                 <model>newmodule/paymentMethod</model>
  65. <!-- order status for new orders paid by this payment method -->
  66.                 <order_status>pending</order_status>
  67. <!-- default title for payment checkout page and order view page -->
  68.                 <title>Credit Card (Authorize.net)</title>
  69.  
  70.                 <cctypes>AE,VI,MC,DI</cctypes>
  71.                 <payment_action>authorize</payment_action>
  72.                 <allowspecific>0</allowspecific>
  73.             </newmodule>
  74.          </payment>
  75.     </default>
  76. </config>

Declare configuration options for admin panel

This file will define how you see configuration options in Magento admin panel System > Configuration

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

  1. <?xml version="1.0"?>
  2. <config>
  3.    <sections>
  4. <!-- payment tab -->
  5.         <payment>
  6.             <groups>
  7. <!-- newmodule fieldset -->
  8.                 <newmodule translate="label" module="paygate">
  9. <!-- will have title 'New Module' -->
  10.                     <label>New Module</label>
  11. <!-- position between other payment methods -->
  12.                     <sort_order>670</sort_order>
  13. <!-- do not show this configuration options in store scope -->
  14.                     <show_in_default>1</show_in_default>
  15.                     <show_in_website>1</show_in_website>
  16.                     <show_in_store>0</show_in_store>
  17.                     <fields>
  18. <!-- is this payment method active for the website? -->
  19.                         <active translate="label">
  20. <!-- label for the field -->
  21.                             <label>Enabled</label>
  22. <!-- input type for configuration value -->
  23.                             <frontend_type>select</frontend_type>
  24. <!-- model to take the option values from -->
  25.                             <source_model>adminhtml/system_config_source_yesno</source_model>
  26. <!-- field position -->
  27.                             <sort_order>1</sort_order>
  28. <!-- do not show this field in store scope -->
  29.                             <show_in_default>1</show_in_default>
  30.                             <show_in_website>1</show_in_website>
  31.                             <show_in_store>0</show_in_store>
  32.                         </active>
  33.                         <order_status translate="label">
  34.                             <label>New order status</label>
  35.                             <frontend_type>select</frontend_type>
  36.                             <source_model>adminhtml/system_config_source_order_status_processing</source_model>
  37.                             <sort_order>4</sort_order>
  38.                             <show_in_default>1</show_in_default>
  39.                             <show_in_website>1</show_in_website>
  40.                             <show_in_store>0</show_in_store>
  41.                         </order_status>
  42.                         <title translate="label">
  43.                             <label>Title</label>
  44.                             <frontend_type>text</frontend_type>
  45.                             <sort_order>2</sort_order>
  46.                             <show_in_default>1</show_in_default>
  47.                             <show_in_website>1</show_in_website>
  48.                             <show_in_store>0</show_in_store>
  49.                         </title>
  50.                     </fields>
  51.                 </newmodule>
  52.             </groups>
  53.         </payment>
  54.     </sections>
  55. </config>

If you go now to Admin / System / Configuration / Payment Methods, you should see “New Module” group. Enable it and try to checkout. On payment methods page you should see “New Module” payment method with credit card form.

Database updates

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

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

For database updates change module version in your confg.xml:

  1.     <modules>
  2.        <CompanyName_NewModule>
  3.           <version>0.2.0</version>
  4.        </CompanyName_NewModule>
  5.     </modules>

And then create app/code/local/CompanyName/NewModule/sql/newmodule_setup/mysql4-upgrade-0.1.0-0.2.0.php:

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

Matchello 2010/02/01 13:34

Troubleshooting

  • 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.



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs