Create Payment Method Module

Last modified by FerdiUSA 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/

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

Adapter model

Note: PaymentMethod name is arbitrary and is up to your decision.

Create app/code/local/CompanyName/NewModule/Model/PaymentMethod.php:

  1. <?php
  2.  
  3. /**
  4. * Our test CC module adapter
  5. */
  6. class CompanyName_NewModule_Model_PaymentMethod extends Mage_Payment_Model_Method_Cc
  7. {
  8.     /**
  9.     * unique internal payment method identifier
  10.     *
  11.     * @var string [a-z0-9_]
  12.     */
  13.     protected $_code = 'newmodule';
  14.  
  15.     /**
  16.      * Here are examples of flags that will determine functionality availability
  17.      * of this module to be used by frontend and backend.
  18.      *
  19.      * @see all flags and their defaults in Mage_Payment_Model_Method_Abstract
  20.      *
  21.      * It is possible to have a custom dynamic logic by overloading
  22.      * public function can* for each flag respectively
  23.      */
  24.      
  25.     /**
  26.      * Is this payment method a gateway (online auth/charge) ?
  27.      */
  28.     protected $_isGateway               = true;
  29.  
  30.     /**
  31.      * Can authorize online?
  32.      */
  33.     protected $_canAuthorize            = true;
  34.  
  35.     /**
  36.      * Can capture funds online?
  37.      */
  38.     protected $_canCapture              = true;
  39.  
  40.     /**
  41.      * Can capture partial amounts online?
  42.      */
  43.     protected $_canCapturePartial       = false;
  44.  
  45.     /**
  46.      * Can refund online?
  47.      */
  48.     protected $_canRefund               = false;
  49.  
  50.     /**
  51.      * Can void transactions online?
  52.      */
  53.     protected $_canVoid                 = true;
  54.  
  55.     /**
  56.      * Can use this payment method in administration panel?
  57.      */
  58.     protected $_canUseInternal          = true;
  59.  
  60.     /**
  61.      * Can show this payment method as an option on checkout payment page?
  62.      */
  63.     protected $_canUseCheckout          = true;
  64.  
  65.     /**
  66.      * Is this payment method suitable for multi-shipping checkout?
  67.      */
  68.     protected $_canUseForMultishipping  = true;
  69.  
  70.     /**
  71.      * Can save credit card information for future processing?
  72.      */
  73.     protected $_canSaveCc = false;
  74.  
  75.     /**
  76.      * Here you will need to implement authorize, capture and void public methods
  77.      *
  78.      * @see examples of transaction specific public methods such as
  79.      * authorize, capture and void in Mage_Paygate_Model_Authorizenet
  80.      */
  81. }
  82. ?>

This is the class that will be doing all the actual work of communicating with your payment gateway. When someone creates an order either the authorize or capture methods of your class will be called. Which one depends on the settings you have. If in your admin pages you have selected “Authorize Only”, the authorize method will be called. If you have chosen “Authorize and Capture”, confusingly enough only the capture method will be called. If you need to declare an error in these methods use:

Mage::throwException("My error/debug message here");

This will cause Magento to display a javascript popup with your message and stop the order processing.

Now that we have the model let’s give admin a way to configure it and also make checkout process aware of this method.

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/updates for this module

Troubleshooting

  • Dont put your module in /Mage. It belongs in app/code/community/
  • Make sure your module’s first letter is capitlized. newmodule apparently will not work, it must start with a capital letter Newmodule.
  • 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