How to make a custom Magento payment extension for an external gateway
Magento is a great open source e-commerce platform and has emerged as the market leader in the recent past. However, it still has a steep learning curve and although the community is growing exponentially, I was not able to find a simple tutorial on how to make a Magento payment extension which links up to an external payment gateway.
In this tutorial, I’m going to attempt to show you how to make a bare-minimum extension, which you’ll need to complete because different payment gateways have different APIs, hooks and functionality. This is by no means a definitive guide, but my attempt to try and get you started. Please note this tutorial assumes that you already know how to build a simple custom extension and have average knowledge of Magento, MVC architecture and of course, PHP. This tutorial is for Magento versions 1.4 and above.
I have created a bare-minimum version of the extension which you need to download from the steps that follow. I hope the code will be self-explanatory. For information from Magento’s wiki click here.
Step #1: Click here to download the ZIP archive for this step. Extract it to app\code\local
Step #2: Click here to download the ZIP archive for this step. Extract it to app\design\frontend\base\default\template
Step #3: Click here to download the ZIP archive for this step. Extract it to app\etc\modules
Once you have done this, you need to follow the following steps to get it working:
- Open app\code\local\Myname\Mygateway\controllers\PaymentController.php and read through the inline comments carefully. The URL that the payment gateway needs to redirect to on your web site after processing the customer’s payment (based on the naming we’ve used in this example) should be: http://www.yourwebsite.com/mygateway/payment/response . This URL will trigger the responseAction() function in the PaymentController, where we will need to validate the response sent by the gateway (to make sure it’s really from there), and if validated, process the order. The validation code is generally provided by the payment gateway.
- Open app\design\frontend\base\default\template\mygateway\redirect.phtml . This is where we post our values to our external gateway. We can retrieve any order information and pass it on as hidden form fields, which is submitted via JavaScript.
- Log into your Magento admin, clear your cache, and make sure the extension is enabled and working fine by navigating to System -> Configuration -> Sales -> Payment Methods and checking if you can see it there.
It is extremely important to go through all the files to get a perfect understanding of the extension. I figured the best way to learn this is to look at a working example. This took me a long time to figure out and I hope this saves a lot of effort for someone. Happy coding!







Vladimir 1:56 pm on October 14, 2011 Permalink |
Hi, very good article, it’s exactly what I’m looking for, thank you! Any idea why the link won’t show up in System -> Configuration -> Sales -> Payment Methods ?
junaidbhura 3:43 pm on October 14, 2011 Permalink |
Thank you
. If you’ve used the extension as is, have you tried clearing your cache, logging out and logging into admin?
Hina 1:35 am on January 17, 2012 Permalink
Hi, I would like to have a simple extension created for my country gateway (yes, we have only one). Would you do it and how much would you charge? Thank you.
junaidbhura 9:14 am on January 17, 2012 Permalink
Hey Hina!
Could you please send me all your details to info@junaidbhura.com ?
junaidbhura 4:19 pm on October 14, 2011 Permalink |
Just to make sure I followed the above steps on a fresh copy of Magento 1.6 , and I was able to see it in the extension’s settings under under System -> Configuration -> Sales -> Payment Methods . Enabled is set to ‘No’ by default.
Vladimir 6:32 pm on October 14, 2011 Permalink
I also use a clean magento 1.6, I’ve tried clearing cache and logging out, the link still doesn’t show up =(. I can see the module in System->Configuration->Advanced->Advanced and it’s enabled, any thoughts?
junaidbhura 6:45 pm on October 14, 2011 Permalink
What “link” are you talking about? Are you talking about the extension that we just created? Or are you trying to reach the extension through a link?
If you’ve followed the steps exactly as it is, you should be able to see a “My Gateway” payment method under System -> Configuration -> Sales -> Payment Methods
Arturo 12:44 am on December 10, 2011 Permalink |
Vladimir… the panel appears over PayPal
Arturo 8:46 pm on December 14, 2011 Permalink |
Also in system.xml, line 7 appears ccavenue. Replace with mygateway
Anirudh Pareek 4:03 pm on December 20, 2011 Permalink |
that’s amazing. everything works fine.