Creating your own Magento payment gateway module - Part I - Configuration Files


Activating our module

Creating a Magento module can be seen as a daunting task at first. But once you get the hang of  it you will understand a lot more of Magento itself and it will get easier and easier from there on.

The first thing we will do in order to create a module is creating a file located in /app/etc/modules called Example_Modulename.xml (where Example can be your company name, or just the name of your package of modules).

it's format will be quite simple:

 

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Example_Modulename>
            <active>true</active>
            <codePool>local</codePool>
        </Example_Modulename>
    </modules>
</config>

This file will simply tell magento that our module is active and will specify the codepool, which can be either community, local or core. The codepool is basically in which code folder our module will be, since we will be putting our files in /app/code/local we set it as "local"

More Configuration Files

Once our module is active, we can begin actually doing some stuff with it. Magento relies heavily on XML to override it's default parameters, classes, populating it's menues, and hooking up module helpers, blocks and much more. In this tutorial we will cover how to

We will now create the config.xml which contains the version information of the module, and defines a helper class ( which will only extend the basic Magento Helper class).

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <global>
        <helpers>
            <stripe>
                <class>Example_Modulename_Helper</class>
            </stripe>
        </helpers>
    </global>
    <modules>
        <Example_Modulename>
            <version>0.0.1</version>
        </Example_Modulename>
    </modules>
</config>

We will place this file in the folder /app/code/local/Example/Modulename/etc/ , this folder will have all the files related to our module configuration. You will see that in Magento each module follows the MVC pattern and will define it's models, controllers and views like it's a mini application.

Now you must be getting a little bored, but the following configuration file is a little more exciting, system.xml will register it's own configuration values ( to be used in the process of hooking up the payment gateway ) in magento and it will allow a configuration form to be displayed in the appropiate section.

system.xml is to be placed in the same folder as config.xml

<?xml version="1.0"?>

<config>
    <sections>
        <payment>
            <groups>
                <stripe translate="label" module="stripe">
                                    <label>Example Payment Module</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>13</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <fields>
                                <active translate="label">
                                    <label>Enabled</label>
                                    <frontend_type>select</frontend_type>
                                   
                                    <sort_order>10</sort_order>

                     <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>
                                </active>
                                <gateway_url translate="label">
                                    <label>Gateway URL</label>
                                    <frontend_type>text</frontend_type>
                                   
                                    <sort_order>10</sort_order>
                                    <show_in_default>1</show_in_default>
                                    <show_in_website>1</show_in_website>
                                    <show_in_store>0</show_in_store>
                                </gateway_url>

                            </fields>
                </stripe>

            </groups>
        </payment>
    </sections>
</config>

Ok this one is a little more dense but it's easy to understand if you go step by step, here we are defining a new configuration group on the payment section of the configuration settings of magento.

Note that we are already defining it's visibility on each store scope, as well as the order it will appear on the configuration tab. We are also defining some fields,  the first "active" that we will use to let the user deactivate our module from the admin panel, note that we define a frontend_type (which means which widget we will display to the user) and a source model, which will tell magento how to populate the widget, in this case a convenient yes or no option is provided; the other field is more straightforward and we will use to store the API url of our payment gateway.

This should show our module on the appropiate configuration tab in magento admin.

Summary

We learned how to activate a Magento  module, we also learned how to insert a form in magento configuration interface that will be useful to store the needed configuration values for our module ( such as status and gateway url).

On the coming part of this tutorial we will see how to populate these forms with default values ( to make our clients work easier, and how to actually process the cart information and connect with our payment API

Post information
Posted by 2014-06-11 08:35:52 +0000 UTC

Leave a Comment: