All about Magento E-commerce Store.......MagentoForum: Adding New Payment Module In Magento

Monday, May 2, 2011

Adding New Payment Module In Magento


There are certain cases where you find the need to add new Payment Method (Payment Gateway) in a Magento store. Especially if the Magento store owner wishes to use a specific payment gateway not available in default Magento installation. Adding a new Magento Payment Module is not difficult but requires a little bit of programming at your end. In this article i am going to elaborate on how to add a new payment module (Payment Gateway) to your existing Magento Installation which will accept credit cards, authorize credit card when the order is placed and saves order ID in payment record.


In this example i will call it “Paymentpro” but you can change every occurrence of“Paymentpro” with the payment gateway you are integrating for e.g. “RBSDirect” or something else.
Few Notes:
  1. Just Replace all instances of “Paymentpro” with your payment module name.
  2. Just replace all instance of “Magik” with your company/namespace name.
  3. Ensure that the path of the PHP include_path is /app/code/local/
  4. Clean your cache after modifying the config xml files.
Now, you are all set to create a new payment module of your own. Following is a step by step guide to build your own shipping module, just follow them in exact order and make sure that the above requirements are taken care of.

Define new Magento Payment Module

First of all lets create a new payment moduleapp/etc/modules/Magik_Paymentpro.xml and create the following xml file:
1
2
3
4
5
6
7
8
9
10
11
<config>
  <modules>
    <Magik_Paymentpro>
      <active>true</active>
      <codePool>local</codePool>
      <depends>
        <Mage_Payment />
      </depends>
    </Magik_Paymentpro>
  </modules>
</config>
The above code will tell Magento that there is a new Payment module enabled in the system. * Lean how to enable or disable a Magento module.

Configure new Magento Payment Module

To configure your newly created Magento payment module you need to createapp/code/local/Magik/Paymentpro/etc/config.xml and add the following code in it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?xml version="1.0"?>
  <config>
    <modules>
      <Magik_PaymentPro>
        <version>0.1.0</version>
      </Magik_Paymentpro>
    </modules>
    <global>
      <blocks>
       <paymentpro>
        <class>Magik_Paymentpro_Block</class>
       </paymentpro>
      </blocks>
      <models>
       <paymentpro>
         <class>Magik_Paymentpro_Model</class>
       </paymentpro>
      </models>
      <resources>
        <paymentpro_setup>
          <setup>
            <module>Magik_Paymentpro</module>
          </setup>
          <connection>
            <use>core_setup</use>
          </connection>
        </paymentpro_setup>
        <paymentpro_write>
          <connection>
            <use>core_write</use>
          </connection>
        </paymentpro_write>
        <paymentpro_read>
          <connection>
            <use>core_read</use>
          </connection>
        </paymentpro_read>
      </resources>
    </global>
    <default>
      <payment>
        <paymentpro>
          <active>0</active>
          <model>paymentpro/paymentMethod</model>
          <order_status>pending</order_status>
          <title>Credit Card (Authorize.net)</title>
          <cctypes>AE,VI,MC,DI</cctypes>
          <payment_action>authorize</payment_action>
          <allowspecific>0</allowspecific>
        </paymentpro>
      </payment>
    </default>
 </config>
The above file will install all the predefined variables and values, you are open to change the default values under “defaul” parameters.

Admin Panel Configuration Options

We will create another file to view the configuration options in Magento admin panel under System => Configuration. To do this lets create one fileapp/code/local/CompanyName/NewModule/etc/system.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0"?>
    <config>
      <sections>
        <payment>
          <groups>
            <paymentpro translate="label" module="paygate">
              <label>Payment Pro</label>
              <sort_order>670</sort_order>
              <show_in_default>1</show_in_default>
              <show_in_website>1</show_in_website>
              <show_in_store>0</show_in_store>
              <fields>
                <active translate="label">
                  <label>Enabled</label>
                  <frontend_type>select</frontend_type>
                  <source_model>adminhtml/system_config_source_yesno</source_model>
                  <sort_order>1</sort_order>
                  <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>
            </paymentpro>
          </groups>
        </payment>
      </sections>
    </config>
Pretty cool, now your Magento store is setup to use new Payment Gateway. Just go toAdmin => System => Configuration => Payment Methods, you will notice a new payment gateway “Payment Pro”. Enable it and try to checkout. On payment methods page you should see “Payment Pro” payment method with credit card form.

Setting up Database Updates

Just create one fileapp/code/local/Magik/Paymentpro/sql/paymentpro_setup/mysql4-install-0.1.0.php and add the following lines of code
1
2
3
4
<?php
    $this->startSetup();
    $this->run("Your SQL QUERY");
    $this->endSetup();
To accommodate these change in database you have to change the version inconfig.xml file
1
2
3
4
5
<modules>
      <Magik_Paymentpro>
        <version>1.2.0</version>
      </Magik_Paymentpro>
 </modules>
Finally, createapp/code/local/Magik/Paymentpro/sql/paymentpro_setup/mysql4-upgrade-0.1.0-1.2.0.php and add the following lines of code
1
2
3
4
<?php
     $this->startSetup();
     $this->run("SQL UPDATE Query");
    $this->endSetup();
Few things to note while creating a new Magento payment gateway module
  1. New payment module should either be placed inside app/code/local orapp/code/community never put your new module under /Mage
  2. Just ensure that the module’s first letter is capitalized. paymentpro apparently will not work, it must start with a capital letter Paymentpro.
  3. Also make sure that the recipient folder of the module’s folder (Magik in the example) is capitalized as well.

Now, you have new payment gateway setup for your Magento store. Remaining logic of authorizing, verifying and accepting payments are out of scope as they differs from merchant to merchant. Some of the merchant requires the customer to be redirected to their site and some allows curl or HTTP_REQUEST based authentication to make purchases.
I would love to hear your views and experiences of setting up a new payment gateway. Please leave me a comment and let me know. Don't forget to subscribe our RSS to receive latest updates on Magento.

10 comments:

  1. Hello dear,
    thanks for providing such a nice article on magento.
    i have implemented the same but i am facing problem on how to enable this module in magento admin.
    my module is showing in
    magento admin->system->configuration->advanced>

    any help would be much appreciated
    thanks
    www.vaseemansari.com

    ReplyDelete
  2. tell me where you are facing exact problem

    ReplyDelete
  3. Hi..

    Im trying to create payment module for Barclaycard epdq cpi payment processing. But Im new to magento. I've found an extension for this payment method which supports magento 1.3 version, but the magento which im using is Magento 1.4.
    Can you help me in fixing this. When I try to put on extension key which Ive got from magento extensions on magento connect I get errors saying failed to install.
    I don't know what to do to fix this.
    Your help will be very much appreciated.

    Thanks,
    Neethi

    ReplyDelete
  4. @webbatsys:for your magento extension:first of all you must check with the provider that which extension key he gives you.i will solve your problem if you send me the exact error you are facing in magento connect.can you are able to install extension on magent 1.3?

    ReplyDelete
  5. Hi,
    Thank You for sharing this information.
    My module is displaying in admin->config->payment method but not showing in front end one page check out section.
    How can i fix this.
    Thank You

    ReplyDelete
  6. @rahul:: Just go toAdmin => System => Configuration => Payment Methods, you will notice a new payment gateway “Payment Pro”. Enable it and try to checkout.make sure that you enable for your current website.

    ReplyDelete
  7. The module does not appear in the front-end for me, either (yet it appears correctly in the admin sections). I can confirm that the directories have been setup properly and the module enabled. Any help would be appreciated. Thanks.

    ReplyDelete
  8. I am also not able to see module in frontend .Can somebody please help me sort it out .

    Thanks !!!

    ReplyDelete
  9. I got this problem just now ... i created a new module both with ModuleCreator (and configured the corresponding system.xml) and "by hand" following the guide on this page (and a similar one on the magento site) BUT at the end the problem is always the same:

    i can get the payment method on the checkout page

    ReplyDelete
  10. Hope this would help http://magentoforum.blogspot.com/2011/07/magento-custom-module-cash-on-delivery.html

    ReplyDelete