How to create a payment gateway in Magento?
Each payment method can be done as separate module or few methods can be combined in same module if they could be used together or share functionality. It is another complex job for people who are trying to figure out how they can connect their own countries’ bank payment gateways to their Magento installations. Here is how to create a payment gateway in Magento.
Basic payment method
- First of all, you should install the latest version of Magento.You need to go there and download Module Creator plugin. You have to install it via Magento Connect in which you get the hashcode from Magento extensions page. That code has to be entered in the Magento Connect page on the admin panel. This extension will help to create all the required folders and files, even creates a simple admin tab and you are ready to go for the development. Once done you should get to the http://localhost/magento/moduleCreator link and fill the form. You will notice some files that have been created, once you have clicked on the create button.
- As our Module Creator has already created a system.xml file inside the module’s etc folder, we need to edit the files config.xml, system.xml and create a model class for the payment method. We need to decide on a unique code for the payment method, before starting with a payment method. Here we have chosen the code to be ‘pay’ and name of my module is Excellence Pay.
- The xml path is required for all payment method i.e. it needs to be inside the<payment> tag and here <pay> is our payment method’s code. The xml will create fields in the System->Configuration->Payment Methods
- Next we need to put in our module config.xml file default values for the fields created in system.xml. Here the code in config.xml is directly put inside the<config> tag. An important thing here is the <model> tab which has the classpath of the payment method model class.
- Next we will create the model class for our payment method at location Excellence_Pay_Model_Pay, as given in the config.xml<model> tag. This is all that is required to create a simple payment method.
Payment method with form fields
Now let’s see how to create a payment method with form fields. Here the title of the payment method is “Pay by Check” and it has two fields Check No and Check Date. The user enters these fields and these are saved to the database for order processing.
- First we will see how to add the form html to the payment method. That is, when we click on ‘Pay by Check’ it will show the two form fields. We will have to add a variable in order to do this in our payment model class Excellence_Pay_Model_Pay. This variable has the path of the block class which contains form. It just basically sets the template of the form to be ‘pay/form/pay.phtml’ .
- We need to make sure that the values go into the database and are visible in Admin -> Sales -> Orders, after we have created the form. So we need to save the form fields to database and also validate the fields. For this in our payment method model class Excellence_Pay_Model_Pay we will add two function assignData ($data) which is called when we click the ‘Continue’ button in the checkout Payment Step. The $data variable has our fields data in it, so in the assignData function, we get the information from $data variable Next is the validate () function, which is used to validate the form fields data and check if they meet the business requirement or not.
- Now, we need to create columns for these to store these two fields in the database. By default in magento payment method fields are stored in two tables
‘sales_flat_quote_payment’ and ‘sales_flat_order_payment’. If you see the existing structure of the tables, these have many column for purchase order no, store credit cart info etc which are default magento payment methods. We need to add 2 more columns to both these table for our fields i.e. check no and check date.
- The last thing remaining for the payment method is the information block. This info block is used in many places and it contains the information of the payment method that shows up in progress area, order email, my account etc.