Table of Contents

Check this thread for more info. http://www.magentocommerce.com/boards/viewthread/832/P60/

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:

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 clean it after modifying config xml files by deleting the contents of var/cache/config/

Module Declaration

Create app/etc/modules/Mage_NewModule.xml:

  1. <config>
  2.     <modules>
  3. <!-- declare Mage_NewModule module -->
  4.         <Mage_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.         </Mage_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/Mage/NewModule/etc/config.xml:

  1. <?xml version="1.0"?>
  2. <config>
  3.     <global>
  4. <!-- declare model group for new module -->
  5.         <models>
  6. <!-- model group alias to be used in Mage::getModel('newmodule/...') -->
  7.             <newmodule>
  8. <!-- base class name for the model group -->
  9.                 <class>Mage_NewModule_Model</class>
  10.             </newmodule>
  11.         </models>
  12.  
  13. <!-- declare resource setup for new module -->
  14.         <resources>
  15. <!-- resource identifier -->
  16.             <newmodule_setup>
  17. <!-- specify that this resource is a setup resource and used for upgrades -->
  18.                 <setup>
  19. <!-- which module to look for install/upgrade files in -->
  20.                     <module>Mage_NewModule</module>
  21.                 </setup>
  22. <!-- specify database connection for this resource -->
  23.                 <connection>
  24. <!-- do not create new connection, use predefined core setup connection -->
  25.                     <use>core_setup</use>
  26.                 </connection>
  27.             </newmodule_setup>
  28.             <newmodule_write>
  29.                 <use>core_write</use>
  30.             </newmodule_write>
  31.             <newmodule_read>
  32.                 <use>core_read</use>
  33.             </newmodule_read>
  34.         </resources>
  35.     </global>
  36.  
  37. <!-- declare default configuration values for this module -->
  38.     <default>
  39. <!-- 'payment' configuration section (tab) -->
  40.         <payment>
  41. <!-- 'newmodule' configuration group (fieldset) -->
  42.             <newmodule>
  43. <!-- by default this payment method is inactive -->
  44.                 <active>0</active>
  45. <!-- model to handle logic for this payment method -->
  46.                 <model>newmodule/paymentMethod</model>
  47. <!-- order status for new orders paid by this payment method -->
  48.                 <order_status>1</order_status>
  49. <!-- default title for payment checkout page and order view page -->
  50.                 <title>Credit Card (Authorize.net)</title>
  51.             </newmodule>
  52.          </payment>
  53.     </default>
  54. </config>

Adapter model

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

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

  1. <?php
  2.  
  3. /**
  4. * Our test CC module adapter
  5. */
  6. class Mage_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. }

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/Mage/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/Mage/NewModule/sql/newmodule_setup/mysql4-install-0.1.0.php:

  1. <?php
  2. // here are the table creation/updates for this module