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
junaidbhura 3:55 pm on February 16, 2012 Permalink |
Thanks, Arturo. Changed this to ‘mygateway’
Anirudh Pareek 4:03 pm on December 20, 2011 Permalink |
that’s amazing. everything works fine.
bab 2:55 pm on February 16, 2012 Permalink |
how do i set the response url so that the payment gateway response triggers the magento code ?
Is there some specific setter method in magento that I have to put this URL in .
Able to send the request thru the getCheckoutRedirectUrl in mage_payment_model_method_abstract . However not able to set the response part.
junaidbhura 3:02 pm on February 16, 2012 Permalink |
The response URL would be set at the Payment Gateway. They must have given you a control panel or something right? There should be an option there to enter your response URL. In our example that URL would be http://www.yourwebsite.com/mygateway/payment/response . This would execute the code mentioned in responseAction() in the PaymentController.php
bab 3:36 pm on February 16, 2012 Permalink
yes response is coming from the gateway .. however how do i “configure” magento to start processing once a response comes back to me … Is this configured in the payment controller php ?
eg – http://www.mywebsite.com/dummy/request.php->sends the request .. to the gateway
http://www.mywebsite.com/dummy/response.php->executed by the gateway as a response .
how do i “tell” magento to pick up processing once response.php has been triggered/executed. In my case it seems to just end there .. since i have not set it anywhere.
Also somehow facing the same issue what vladmir seems to be facing .. module shows up in config->advanced->advanced but does not show up in config->sales->payment methods
junaidbhura 3:50 pm on February 16, 2012 Permalink
Okay you really need to read up on building a Magento extension and MVC architecture before getting to this level. There is no response.php . “response” is a function (responseAction located in file PaymentController.php) which is executed when the payment gateway redirects to the response URL you’ve mentioned. The payment gateway usually sends a POST request to the URL you’ve mentioned. So in the responseAction function you get the response sent by the payment gateway using the $_POST variable. If you still don’t understand how this works, I suggest starting off much smaller and understanding the basics of the MVC architecture and building simple front-end Magento extensions.
bab 5:17 pm on February 20, 2012 Permalink |
thx a lot .. ur comments helped .. Have another q .. is it possible to go to the order review (or stay at the payment information ) in the checkout/onepage depending on whether the payment was successful or not ?
junaidbhura 8:42 am on February 21, 2012 Permalink |
Since this extension is to link up with an external payment gateway, the procedure in Magento is that you are sent via this extension to the payment gateway, and then the payment gateway sends you back to your return URL. You can do whatever you want in your return page depending on whether the payment was successful or not. The checkout page would have nothing to do with it once you are sent out of it.
julien 7:18 pm on March 8, 2012 Permalink |
Thanks a lot Junaidbhura,
Your tutorial helps me to finish my custom payment module.
junaidbhura 9:02 am on March 9, 2012 Permalink |
You’re welcome, Julien!
joe 10:58 am on March 20, 2012 Permalink |
Hey Junaidbhura,
very nice post, on Magento ver. 1.6.2.0 with developer mode enabled , I got stuck with message: “Notice: Trying to get property of non-object in app\code\core\Mage\Adminhtml\Block\System\Config\Form.php on line 526″, after I select Shipping Methods in Admin. How can I trace down such kind of warnings? Thanks
junaidbhura 11:08 am on March 20, 2012 Permalink |
Thanks Joe! Unfortunately I’m not going to be able to help you out here because that could be caused by anything!
joe 5:29 pm on March 20, 2012 Permalink |
Oh, sorry, i meant: after I select Payment Methods in Admin… With developer mode disabled, it’s ok. Same with original files.
kmi 10:52 am on April 1, 2012 Permalink |
You made my day!!! After a few days of battling to get the integration to my payment gateway via form post working, you put it on a silver platter. Merci beaucoup!
junaidbhura 8:48 am on April 2, 2012 Permalink |
Glad it helped
Omar 10:28 am on May 15, 2012 Permalink |
Hello, can you send me the link of files again becuase the above link is not working thanks
junaidbhura 10:30 am on May 15, 2012 Permalink |
Omar, just double-checked the links. They seem fine. Can you try again?
Omar 10:33 am on May 15, 2012 Permalink
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
Nope still not working. Talking about these links
xomero 6:17 am on May 25, 2012 Permalink |
great post and code, thanks its helping a lot, my gateway payment generates a “token” in the hidden fields and the recomendations from the bank is to validate this tocken once they return the success.
So i have to store this token somwhere in the DB then once the bank send the client back read the transaction id and the tocken to validate the operation, any idea how can I crate the databae, what fields to include, how to acccess and compare from the controller?
thanks.
junaidbhura 9:07 am on May 25, 2012 Permalink |
Thanks Xomero,
I’m guessing your payment gateway sends a POST request to your controller. So just do a var_dump() in the responseAction() and do a test transaction. That way you’ll get to see what variables your payment gateway sends you. Based on that you can create a custom table in your database and run a Magento database query: http://www.magentocommerce.com/boards/viewthread/2235/ . I’m sorry I won’t be able to help you beyond the scope of this tutorial. All the best!
xomero 5:53 am on May 26, 2012 Permalink
that’s great, thanks for your time!
Casilas 2:30 pm on June 1, 2012 Permalink |
Thanks junaidbhura for such a great tut.
But, I have a problem, done everything, step by step, but “My Gateway” does not appear in Payment methods?
Yes, i’ve cleared cache and logout/login, still nothing. It’s 1.6 magento.
Casilas 2:33 pm on June 1, 2012 Permalink |
Done, nevermind