Updates from October, 2011 Toggle Comment Threads | Keyboard Shortcuts

  • junaidbhura 9:23 pm on October 11, 2011 Permalink | Reply  

    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:

    1. 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.
    2. 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.
    3. 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 | Reply

      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 | Reply

        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 | Reply

        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 | Reply

      Vladimir… the panel appears over PayPal

    • Arturo 8:46 pm on December 14, 2011 Permalink | Reply

      Also in system.xml, line 7 appears ccavenue. Replace with mygateway

    • Anirudh Pareek 4:03 pm on December 20, 2011 Permalink | Reply

      that’s amazing. everything works fine.

    • bab 2:55 pm on February 16, 2012 Permalink | Reply

      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 | Reply

        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 | Reply

      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 | Reply

        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 | Reply

      Thanks a lot Junaidbhura,

      Your tutorial helps me to finish my custom payment module.

    • joe 10:58 am on March 20, 2012 Permalink | Reply

      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 | Reply

        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 | Reply

      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 | Reply

      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!

    • Omar 10:28 am on May 15, 2012 Permalink | Reply

      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 | Reply

        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 | Reply

      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 | Reply

        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 | Reply

      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.

  • junaidbhura 2:28 pm on December 31, 2009 Permalink | Reply
    Tags: , PHP,   

    What web technology to use on my site? 

    Biggest Piece of the Cake

    Biggest Piece of the Cake

    I think this should have been one of my first posts. If you’re making a web site for your business to showcase your company, etc. you will use XHTML-CSS and JavaScript for best SEO results.

    But what if you have a bigger plan? What if you want to build a larger web application? What server-side language will you use?

    The only person who can answer that is you. But you must get guidance from an unbiased source. Web developers tend to become fanatics of the language they are used to working with. It turns into a kind of patriotism. People will recommend a lot of web languages and frameworks and pull clients into what they are comfortable working with, as opposed to what the need really is.

    You know you’ve picked the right technology to use on your web site if:

    • It works without any hitches
    • There is tons of help available on the Internet (documentation, forums, communities, open source)
    • There are tons of companies / individuals who can take over half-way through the project
    • It is the most popular

    The last point is stressed because the more popular a technology is, the lesser the chance of you getting into trouble. We don’t want something that, only the person who developed your application and a handful of others know about, do we? We want something that everyone knows about, so we have more options to choose from.

    In my opinion, the technology that falls under this category is PHP. This has nothing to do with the fact that I am a PHP developer. I migrated to PHP from ASP.NET because of all of the above mentioned. There are many skeptics and articles all over the Internet that might disagree, but I don’t want to end up red-faced in front of my clients who want more flexibility in developers, and scalability. The fact everyone will agree on, whatever the reasons might be for it, is that the number of PHP developers in the world outnumber any server side language by a HUGE number.

    If you are looking to use a web technology in India, you can bet on PHP because it is about to become the largest PHP development community in the world. This means unlimited support and flexibility.

    There are newer languages that boast better features, but don’t come close to the market share of PHP.

    The closest example I can think of is: Would you pick a Windows PC with all its vulnerabilities and viruses for your office or a Linux-based computer? I’ve used Ubuntu Linux. I’ve never been a bigger Windows fan. I admit Ubuntu is far superior to Windows, but I felt isolated and let-down because Windows boasts a larger market share and all major software run on it perfectly.

    In short, it all comes down to:  support and market share. If, in the future, some other technology can boast such a market share, I will migrate to it and recommend it.

     
    • Abhijit 12:04 pm on January 5, 2010 Permalink | Reply

      Hello Junaid, this artice with help lot of people to decide on the choice of language and framework to be used for their website/portals. Quite insightful piece, I would say.

    • Faheem 4:11 am on February 22, 2010 Permalink | Reply

      Very insightful, Junaid. Thank you.

  • junaidbhura 10:55 am on September 4, 2009 Permalink | Reply  

    Easy Clean URLs with Apache’s mod_rewrite 

    First of all I would like to get the following out of my system:
    Tutorials are supposed to be EASY! How on earth do you expect someone who is just learning to understand regular expressions, you jerk!
    That felt good. Now lets move on, shall we?

    You’ve seen it being used everywhere. What is a “Clean URL” and why is it so important today?

    1. It’s better for Search Engine Optimization (SEO) because it fools the search engine into thinking that the page is actually a folder or a file (depending on how you’ve written it), rather than a long URL with a thousand variables which even a regular person would think twice about crawling.
    2. It demonstrates knowledge on part of the developer. Since this is slowly becoming a standard of sorts, it’s a must-know for developers.
    3. It looks nice and clean :)

    Okay, now what do you need to make this work?

    1. An Apache server with mod_rewrite function on. The following technique works ONLY on an Apache server with mod_rewite enabled. That’s right. If you use a Windows server:
      1. You’d better be using some Microsoft Technology like ASP, ASP.NET, etc. or you’re wasting your money!
      2. Here is an article to get clean URLs on the Windows (IIS) server.
    2. The ‘.htaccess’ file

    That’s it!

    Now, lets start. Assuming you have a web site called http://www.mywebsite.com and you have a page URL that looks like: http://www.mywebsite.com/index.php?page=green

    UGLY!

    Here is how you achieve clean URLs:

    1. Open the .htaccess file on your root folder. If you don’t already have one, make one in Notepad or whatever, but make sure you name it ‘.htaccess’ (so that’s no name with the file extension ‘htaccess’) and upload it to your root folder (this is usually www or public_html)

    2. If your .htaccess file already existed and you have some code in it already, do not overwrite it.

    3. Take a backup of the .htaccess file (in case something goes wrong) . Paste the following code:

    RewriteEngine on
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^([a-z\-]+)$ index.php?page=$1 [L]
    </IfModule>

    Note: If the line RewriteEngine on already exists in your .htaccess file, don’t paste that again.

    Voila! You now have a clean URL. Now if you navigate to http://www.mywebsite.com/green , you get your page!

    How does this work? The text following “RewriteRule”  is called a regular expression. It tells Apache to match a condition and if a match is found, redirect (rewrite) internally to a page.

    It’s really simple:

    ^([a-z\-]+)$ : This part is the matching part. This means that lowercase letters a through z and the hyphen (-) are accepted. In our case, “green” falls under this category, so its counted as a “match”

    index.php?page=$1 [L] : This is the part that is executed if a match is found. It tells Apache that whatever you found before the “$” is now $1. If you have two conditions, the first one would be $1 and the second $2 and so on. In our case we just matched one, so that’s stored in $1. So now $1 = green. And the page is directed to index.php?page=green .

    Sweet! But what’s that [L]? It tells Apache that this is the last statement it needs to execute if a match is found and to stop right there. Just to save on time.

    So basically, this entire thing is made up of:

    1. A condition which checks for a set. (Cause)
    2. A result statement which does something when a match is found. (Effect)

    For more in-depth reading on this subject, click here. For another simple tutorial, click here.

    Problems:

    “My page loads with mod_rewrite but my style sheets and/or images don’t work!”

    Obviously! Now that your browser believes that http://www.mywebsite.com/green is an actual folder, it tries to link it up from there.

    Quite simply, if your code on your page for your style sheet is: <link href=”assets/css/style.css” /> it looks for it under http://www.mywebsite.com/green/assets/css/style.css , which doesn’t exist.

    Simple solution:

    Right after your <html> tag, insert this: <base href = “http://www.mywebsite.com/&#8221; />

    Now you’re telling the browser, “Hey, from now on all links to resources on my page are to start from my base URL, okay?”

    So now the browser  loads http://www.mywebsite.com/(the base URL)assets/css/style.css

    Problem solved.

    “This just doesn’t work!”

    Make sure you are running an Apache server with mod_rewrite on. If you’re not sure, contact your web hosting company.

     
    • Andrew D 6:07 pm on October 11, 2010 Permalink | Reply

      What if I am not using php? I just want to rewrite http://mywebsite.com/vegas to http://mywebsite.com/vegas.html …. What is the code for that?

    • Andrew D 6:30 pm on October 11, 2010 Permalink | Reply

      Hey…I figured it out about the rewrite so thanks! I wanted to ask one more thing. Do you know how to code so I can stealth the first URL. What I mean is , I want the visitor to type in a URL and for that URL to stills show in the address bar (just for appearances) even after we are redirected to the destination URL?

    • junaidbhura 9:32 am on October 12, 2010 Permalink | Reply

      You’ll have to use IFRAMEs for that, which is not recommended because that would be bad for SEO.
      To achieve all these ‘cool’ things you’ll need to use a server-side language like PHP which can decide what content to display based on the URL and any other conditions you might have.

    • Renato 8:19 pm on November 28, 2010 Permalink | Reply

      Mmh… What if I am in a subfolder? For istance:
      http://www.mysite.com/folder/green -> http://www.mysite.com/folder/index.php?page=green
      I tried with RewriteBase, without any success…

      • junaidbhura 9:35 am on November 29, 2010 Permalink | Reply

        You’ll need to put .htaccess in that subfolder. Can I see your htaccess code?

    • Renato 4:09 pm on November 29, 2010 Permalink | Reply

      I tried this: (/folder/.htaccess)
      RewriteEngine On
      RewriteRule ^([a-z\-]+)$ index.php?page=$1 [L]

      And this:
      RewriteEngine On
      RewriteRule ^([^/\.]+)$ index.php?page=$1 [L]

    • junaidbhura 4:17 pm on November 29, 2010 Permalink | Reply

      Okay.. assuming that your address is:
      http://localhost/folder1/folder2/index.php?page=green
      and your .htaccess file is in folder2, the first one should have worked: http://localhost/folder1/folder2/green

      If it doesn’t, check if mod_rewrite is enabled.

    • Renato 11:24 pm on November 29, 2010 Permalink | Reply

      Tried exatly the first situation, without any success… I’m pretty sure mod_rewrite is enabled, there is a working wordpress on the same server with friendly urls!

    • Renato 12:08 am on November 30, 2010 Permalink | Reply

      Sorry for the mess, the problem was a “AllowOverride None” in the default site’s configuration file.
      To avoid the stylesheet and images problem, i used:
      RewriteEngine on
      RewriteRule ^(.*).html$ index.php?&page=$1 [L]
      I think that is good for SEO too, right?

    • junaidbhura 10:10 am on November 30, 2010 Permalink | Reply

      Ah :) . Yeah that’s good, it ‘fools’ the search engines into thinking they’re HTML pages.

    • sumit 3:42 pm on March 15, 2011 Permalink | Reply

      no man its not working any other idea to let me out of this

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel
Follow

Get every new post delivered to your Inbox.