How to make a custom Magento payment extension for an external gateway

Magento

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!

213 Comments

Feel free to have your say! Leave a Comment

  1. Vladimir says:

    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 says: (Author)

      Thank you :) . If you’ve used the extension as is, have you tried clearing your cache, logging out and logging into admin?

    • junaidbhura says: (Author)

      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 says:

        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 says: (Author)

        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

  2. Arturo says:

    Vladimir… the panel appears over PayPal

  3. Arturo says:

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

  4. Anirudh Pareek says:

    that’s amazing. everything works fine.

  5. bab says:

    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 says: (Author)

      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 says:

        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 says: (Author)

          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.

  6. bab says:

    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 says: (Author)

      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.

  7. julien says:

    Thanks a lot Junaidbhura,

    Your tutorial helps me to finish my custom payment module.

  8. joe says:

    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 appcodecoreMageAdminhtmlBlockSystemConfigForm.php on line 526″, after I select Shipping Methods in Admin. How can I trace down such kind of warnings? Thanks

  9. joe says:

    Oh, sorry, i meant: after I select Payment Methods in Admin… With developer mode disabled, it’s ok. Same with original files.

  10. kmi says:

    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!

  11. Omar says:

    Hello, can you send me the link of files again becuase the above link is not working thanks

    • junaidbhura says: (Author)

      Omar, just double-checked the links. They seem fine. Can you try again?

      • Omar says:

        Step #1: Click here to download the ZIP archive for this step. Extract it to appcodelocal

        Step #2: Click here to download the ZIP archive for this step. Extract it to appdesignfrontendbasedefaulttemplate

        Step #3: Click here to download the ZIP archive for this step. Extract it to appetcmodules

        Nope still not working. Talking about these links

  12. xomero says:

    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 says: (Author)

      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!

  13. Casilas says:

    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.

  14. Fábio says:

    Hi!

    Very good article, it’s exactly what I’m looking for, thank you!
    I have a problem. When I make checkout, the fields of payment for the client (number of card, dates… ) do not appear :S

    Can you help me?

    • junaid says: (Author)

      Thanks, Fabio! This method is for an external payment gateway, so the fields don’t appear because the credit card information is entered at the payment gateway. Like when you checkout with PayPal you enter all the information on PayPal, right?

  15. Fábio says:

    Now I understand. Thanks!

    To get the amount will be getBaseGrandTotal(); ?> ?
    and how to get the product information?

  16. Fábio says:

    Thanks Junaid!

    Your tutorial was very helpful for me :)

  17. Mike says:

    Hi, i followed all your steps but after the payment is made by the external gateway, it redirects to my website main page but does not give me any feedback if my payment was successful.
    Is there any way to redirect to a successful payment page?

  18. Colin says:

    I am trying out link to an external gateway,I have go as far as contention but now I am slight confused or missing understanding, where to locate the post back reply ,

    Great Post really helpful & thank yo for time.

  19. vasanth says:

    how can save the order in the custom payment.
    Is this correct?Order is not stored and i used the code in my response function.

    $order = Mage::getModel(‘sales/order’);
    $order_id = Mage::getSingleton(‘checkout/session’)->getLastRealOrderId();
    $order->loadByIncrementId($order_id);
    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, ‘Gateway has authorized the

    payment.’);
    $order->sendNewOrderEmail();
    $order->setEmailSent(true);

    :( please give me solun

    Thnx
    vasa

    • junaid says: (Author)

      Hey Vasanth,
      The Order ID will be passed by the payment gateway, usually through a POST call. Of course, you need to pass the Order ID to the payment gateway first for it to pass it back to you. Once you get the Order ID from the payment gateway, you can get the order by using:
      $order->loadByIncrementId($order_id_sent_from_pg);

  20. Darshan Jain says:

    I already installed the extension.And also refresh the cache & session.Although module is enable in admin panel.But one major problem is not display under system->configuration->sales->Payment_Methods.My magento version is v1.6.2.

    Please give me proper solution for this.

    • junaid says: (Author)

      Hi Darshan,
      I’m afraid I can’t help you out here, because there could be a conflict with this extension with another or a multitude of reasons why this is happening.

  21. bamidele says:

    i made the payment gateway, it is working fine on my localhost but when i transferd it to my main site , i couldnt see it on payment methods

    • junaid says: (Author)

      Bamidele,
      Try clearing your cache (both browser and Magento’s) , logging out and logging in. And of course make sure you have uploaded all the files.

  22. dyla says:

    Hi. Nice tutorial and thank you for that.

    I got a problem after tried to test this on my server. after clicked on shipping method, there is another page appeared on the same page (side-by-side) something like there is an error processing … and after 1-2 seconds it redirected back to checkout page. could you advise me on this problem?

    thank you.

  23. bamidele says:

    how do i display custom gateway errors sent by my gateway… i dont know the method to use in the responseAction

  24. kavee says:

    Thank you for the excellent post.
    I followed exactly, and got the payment method showing on the admin page. However it is not showing on checkout. Then I used the unmodified code as you have published, but with same issue. I am using magento 1.7

    • junaid says: (Author)

      Thanks Kavee,
      Thats probably because you need to clear the Magento cache under System -> Cache Management. Clear all the caches there to be safe.

  25. kavee says:

    I did that so many times, no luck…

  26. kavee says:

    The only difference is, I am on version 1.7. Can this be a problem?

    • junaid says: (Author)

      Kavee,
      No, I’ve tested this on 1.7 on a fresh install. Not sure what the problem could be. You could probably try this carefully on a fresh install too and work backwords from there to find out what’s different. Make sure the extension is enabled in the admin of course. I’m afraid thats about how much I can offer support :) . All the best!

  27. kavee says:

    Yes, it works on a fresh installation. Have to find out why not in my existing setup. May be something to do with the custom theme.

    Thanks

  28. kavee says:

    It appeared to be a file permission issue. I ran magento-cleanup, and now it is working beautifully!!
    Junaid, one more question, what is the return URL to be given to the gateway (for processing gateway return status)? Appreciate your help….

  29. BGz says:

    hello junaid, and thanks for your effort.
    one thing i still do not understand is how you came up with response url? where and how you define that url? where it comes from?

    thanks again

    • junaid says: (Author)

      Hi BGz,
      To understand this you need to understand MVC architecture and how Magento’s extensions work. The response URL is the name of the function “responseAction” in your PaymentController.php . If you were to name that function “bgzAction” , then your response URL will be http://www.yoursite.com/payment/bgz . If you have any further questions on the subject I suggest reading up on MVC.

  30. urspsk says:

    Hai Junaid,

    Great Work, this work fine.

    Thanks a lot for sharing your code.

    WarmRegards,
    UrsPSk

  31. Alfredo says:

    Hello,

    I install the module fine.

    But in the payment option “frontend” don’t show?

    What need make for show?

    Thanks for the code
    Greetings from Costa Rica

    • junaid says: (Author)

      Alfredo,
      Greetings from Bangalore! If it doesn’t show up on the front end then there’s probably a caching issue or a conflict with some other piece of code. It works fine on a fresh install, so there’s some existing code on your site that conflicts with this extension.

  32. Alfredo says:

    Hello, I fix the issue,

    Disable Compilation, but need send customer name

    Thanks

  33. Alfredo says:

    Hello,

    I use http://site.com/mypay/payment/response.

    And return error:
    Fatal error: Call to a member function getMethodInstance() on a non-object in magento/app/code/core/Mage/Payment/Helper/Data.php on line 119

    Thanks

  34. Thank you very much says:

    Hai Junaid,

    Great Work, this work fine.

    Thanks a lot for sharing your code.

  35. Ahsan says:

    Hello Junaid,

    Thanks a lot for sharing this great tutorial.

    I am facing an issue when order is placed it is not showing in the Order Grid at Sales->Order listing.

    Also is it possible to send request to gateway before placing an order?

    Thanks alot

    • junaid says: (Author)

      Ahsan
      Thanks! If the order isn’t showing up, it probably isn’t placed properly. This method does send a request to the payment gateway and gets a response. You have to write the code to update the order when the response comes back from the payment gateway.

  36. Ahsan says:

    I want to validate response from gateway before placing an order e.g when user place order by new payment gateway:

    1. I have to call payment gateway API
    2. Get response from API
    3. If response is ok from gateway than create new order and redirect user to payment gateway website otherwise show user a message to “there is some internal problem please select any other payment option”.

    Looking forward for your help urgently. I have to wrap-up this asap

    Thanks

    • junaid says: (Author)

      Ahsan,
      I understand you are working around a deadline, but I’m afraid that is beyond the scope of this tutorial. This tutorial is meant to take you only this far, and assumes you have the expertise to do the rest.

  37. Ahsan says:

    I can understand it anyway thanks for sharing this tutorial that really helped me. If you share any other related tutorial links I will be thankful

  38. Leroux says:

    Hi, I have installed the module and everything seems to work great up to the point where the response from the gateway. I get the following error:
    Fatal error: Call to a member function getMethodInstance() on a non-object in /Library/WebServer/Documents/www/DEEP/invivo/app/code/core/Mage/Payment/Helper/Data.php on line 119

    I am new to working with Magento so maybe I am missing something? I am also testing this on a clean install.

  39. sazedul says:

    Hi Thanks for your nice post its helped me to deal with an external gateway.now i just need some little info regarding redirect if Success. I putted the following link “http://mysite.com/index.php/mygateway/payment/response” in the redirect post page if success.
    its able to redirect Successfully after Success but i am getting this message
    “The page you requested was not found, and we have a fine guess why” in the site.
    which means .It will be helpful if you can help me to search the problem.
    do i have to add something after “mygateway/payment/response” or
    i have to alter something like “response” to “responseAction” . please brother, help me . I will be grateful to you.

    Thanks

    • junaid says: (Author)

      Hi Sazedul,
      This is most likely a caching issue. Make sure you go to System -> Cache Management and empty all caches. Also, for a development environment its best to turn off all caches to avoid problems like this one.

      FYI, you don’t need to change the response to responseAction in the URL. The word “response” in the URL automatically fires the responseAction function in PaymentController.php . So make sure that you check the response that you get from the payment gateway in the responseAction function and update your order accordingly and maybe even redirect to a success or failure page accordingly.

  40. sazedul says:

    Hi,
    Jjunaid Brother.Thanks at last i did it.

  41. Rafique says:

    Thankyou for a brilliant tutorial and for sharing your knowledge. I have just one question!

    I know it is beyond the scope of this tutorial but there isn’t many people around that I can ask – so here goes:

    How do I generate and post a token along with the orderId?

    • junaid says: (Author)

      Thanks, Rafique!
      Generating the POST token will need to be custom code. That depends on what you want your token to look like, since there’s no “one way” of generating a POST token. You can ask your payment gateway what that needs to look like. Regarding actually POSTing it, you can just make another hidden field in the redirect.phtml file which sends the POST request to the payment gateway.

  42. Rafique says:

    Thanks for the reply, I have your module working in that it is processing the order and redirecting to the gateway via the form. I just need to overcome a “Security precondition failed” error at the gateway.

    I’m using this for Chase Paymentech BTW!

    Kind regards…

    • junaid says: (Author)

      I hate to say it, but its beyond the scope of this tutorial!

      I think you should read their documentation in detail, and if they gave you some sample code, make sure you replicate that exactly inside Magento so that you don’t miss out any security preconditions and any other specifications of theirs. Good luck!

  43. Rafique says:

    Thanks man..

  44. Eric says:

    Hey Juan,
    Good job on this saved me hours.. no days trying to create my own module payment gateway from scratch. I have run into a problem and your expertise would be greatly appreciated. As you have said many times over the return url is:

    http://www.yourwebsite.com/mygateway/payment/response

    so I changed it to suite my site and now it should be

    http://bookshopprodweb.jcu.edu.au/magento/mygateway/payment/response

    that looks right until I try it …then it isnt found by the server the function is still responseAction, I extracted everything to their right folders everything works well upto that part.

    could I just make a new file called response.php and the just call that function?

    making my return url

    http://bookshopprodweb.jcu.edu.au/magento/response.php

    • junaid says: (Author)

      Hi Eric,
      Thanks! My name’s Junaid, btw :D

      It will be better to follow Magento’s MVC structure rather than creating a response.php file . Make sure you’ve made your extension correctly by testing it like:

      1. Create a function called ‘testAction’ in the PaymentController.php and echo ‘Hello World’
      2. Navigate to http://bookshopprodweb.jcu.edu.au/magento/mygateway/payment/test and see if you see ‘Hello World’ on your screen. If you don’t then your extension is not set up well, or your site is cached or it could be practically anything!

  45. Eric says:

    Hi Junaid one last problem it only works for logged in users but a guest gets redirected back to the cart :( Any ideas why that could be the case?

  46. Jose says:

    Hi,
    Buen tutorial, gracias por su ayuda.
    Ecuador. Latin America

  47. Dasun says:

    Thanks a lot for the code and everything. Just awesome.

    One last thing is there a way to insert the data which are returned from payment gateway to database? Simply wanna store the data in the database which are returned from the database.

  48. Jimmy says:

    Great example. Thanks.
    But I’ve got same error as `joe` at `March 20, 2012 at 10:58 am` in this comments…
    when I lead to Payment Methods in admin panel, I see
    Notice: Trying to get property of non-object in /var/www/data/myproject/htroot/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php on line 568

    Magento v.1.7.0.2

    I nothing changed in files. Guess something wrong in XML.
    /Myname/Mygateway/etc/system.xml
    if I comment out fields section, then it works fine.

  49. Jimmy says:

    I found the error in XML

    Section “sort_order” wrote NOT correct.
    Must be, so:

    Sort Order
    text
    100
    1
    1
    0

    but wrote, so:

    Sort Order
    text

    100
    1
    1
    0

  50. Janluka says:

    Hi Junaid, THX for this great extension. The ext works great, i have only a little problem. When all is done in response action (i’m sure that all is done because i receive an email and the order is processing) i cannot redirect to success or failure page. Every url that i put in Mage_Core_Controller_Varien_Action::_redirect(‘checkout/onepage/success’, array(‘_secure’=>true)); is wrong and send me to the home page. Have you ever encountered this kind of problem. Hope you can help me…. The work is pratically done and everything is fine except redirect to success or failure. Sorry for my poor english.

    Regards

    Gianluca

    • junaid says: (Author)

      Hi Gianluca
      I’m glad this helped you out. Hmm that is a weird problem. Maybe you can try a different redirect function like this:
      Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl(‘checkout/onepage/success’));

      • Janluka says:

        Just tried …. Still not working. I try to put a log before and after. Both are written to the log. No error in apache… I’m going mad. Everything is working except the redirect. I also try $this->_redirect(‘checkout/onepage/success’, array(‘_secure’=>true));… Same result, no error. If you have any idea that could help me please tell me. Thx a lot for your ext and for your help!

        • junaid says: (Author)

          If you get redirected to the home page when you try redirecting to the checkout/onepage/success it means that the order you are trying to access no longer exists in the session. Are you sure you are working with an active order?

          Basically, the onepage/success and onepage/failure pages will only work if a user created an order, got redirected to the payment gateway, paid and got redirected back to our site and if the order is still available in $_SESSION (without closing the browser window). Otherwise, you get redirected to the home page.

          • Janluka says:

            Yes, I’m sure. After payment if i type manually the url to checkout/onepage/success it works. I also try to put a header location and exit from function…. Still not working….. I don’t know how could i resolve it.

          • junaid says: (Author)

            In the responseAction function, the first check is to see if there was a POST, and if there’s no POST to redirect to the home page. Does your payment gateway send you a POST request back to responseAction? If this is not the problem, then I’m not sure what it could be :)

  51. Pankaj says:

    The exactly is thing for which i was looking for thanks a lot

  52. Pankaj says:

    Junaid i am newbie to magento can you confirm when magento insert order in sales_flat_order either before payment or after payment and what is #00000001 order id and how it is calculates
    Thanks

  53. Pankaj says:

    Hello junaid ,

    Thanks a lot for your response.

  54. Francisco says:

    Hi Junaid. I am very grateful for your post and code. It has saved me hours of work, but i have two questions that were posted before but not answered:

    1. How can i save some of the information returned to my database and relate it to the order? (It may be out of the scope, i know, but if you could just point me in the right direction, i would appreciate it)
    2. This Fatal Error shows up instead of a response, i traced it to a class called Mage_Payment_Block_Info : Fatal error: Call to a member function getMethodInstance() on a non-object in magento/app/code/core/Mage/Payment/Helper/Data.php on line 119.
    3. Following Magento’s model, how would i implement a SOAP payment gateway. Information is sent, and returns with values, in the same redirect page, so i have to redirect the page myself towards success or failure.

    • junaid says: (Author)

      Hi Francisco
      1. This is way beyond the scope of this tutorial, and I’m afraid you need to do this on your own
      2. I’m not sure what this error is, it could be a conflict with something
      3. If you use SOAP, then the SOAP actions would be in the redirectAction, and yes you need to redirect so success and error based on the result. There would be no responseAction

    • Marco says:

      Hello francisco …. I have the same error as you, you found a solution?

      Tnks

  55. Marco says:

    we are using your extension and everything works fine. It connects to the bank successfully. When payment is rejected failure response is activated, but when the payment is accepted, it returns an error. Log Magento active and I get this error.
    2013-04-16T05:01:59+00:00 ERR (3): Recoverable Error: Argument 1 passed to Mage_Payment_Helper_Data::getInfoBlock() must be an instance of Mage_Payment_Model_Info, boolean given, called in /home/vetter/public_html/app/code/core/Mage/Sales/Model/Order.php on line 1269 and defined in /home/vetter/public_html/app/code/core/Mage/Payment/Helper/Data.php on line 117
    Could you give me a light to fix this?
    Thanks for your time and patience

  56. ranga says:

    when i submit the order button cart get empty without validating the order

  57. Stefano says:

    Thanks Junaid, you made my day! :D
    The script works perfectly!
    But now a have a little problem… With success payments, all is perfect, but when payment fails, it seems “cancelAction()” function hasn’t been triggered. So, wrong orders aren’t change to “canceled” status. Oddly, the redirect to failure page works!
    Have you some advice?
    i suspect there’s a problem with my gateway, because it refresh twice the redirect link URL (that the payment gateway needs to redirect to my own site). Maybe does it lost something with session id?

    thanks again

  58. fadi says:

    Thank you so much for this great tutorial, i got it working until i get to the return URL.
    My payment gateway returns the values in the URL so its not a POST but a GET.
    example: /mygateway/payment/response/&orderInfo=100000033&

    how can get the values?? normal GET and REQUEST is not working.

    Thank again

    • junaid says: (Author)

      Hi Fadi,
      I’m not sure if it was a typo, but it doesn’t look like the URL is configured properly. You mentioned:
      /mygateway/payment/response/&orderInfo=100000033&
      Whereas a proper URL would be:
      /mygateway/payment/response/?orderInfo=100000033&
      (Notice the question mark) . Let me know if this works.

  59. Joe says:

    Hello,

    I noticed by default Magento will send the transaction ID not the order ID to the processor. Is there any way to send the order ID instead?

    Also, how would I add the customer’s IP address to the fields transmitted?

    • junaid says: (Author)

      Hi Joe,
      Since this is a custom payment gateway that we are building, Magento doesn’t “send” anything. It is us through our code who can determine what to send and what not to send. To answer your question: Yes we can send the order ID and the customer’s IP. But I’m afraid that is beyond the scope of this tutorial. You can take a deeper look at the code and I’m sure you will find your answer.

  60. wily says:

    Hi Junaid, thanks for your tutorial. It’s a great help.

    I notice that the order is immediately saved into database when the ‘Place Order’ button on checkout page is hit. I knew this from the admin page, there were pending order comes in before the gateway redirect to magento shop.
    Is it an issue or something? I thought the order should be saved after http://www.yourwebsite.com/mygateway/payment/response get valid response from the gateway.
    note: I’m working on localhost actually, with simple php script as server. Or is this the issue..?

    • junaid says: (Author)

      Hi Wily,
      Don’t worry about it, this is default Magento behavior. The order is first created with the status “Pending” and then depending on whether the payment was successful or not, the status of the order is updated.

  61. raju says:

    nice …

  62. raju says:

    how do we insert transaction id after bank approve payment ? need help

    • junaid says: (Author)

      Hi Raju
      Unfortunately this is beyond the scope of this tutorial. If you read the code carefully, I’m sure you will find what you are looking for

  63. Sajith says:

    nice tutorial,
    i need to know how to handle the transaction complete stage
    eg: after the payment complete it returns the transaction complete and i need to update my stock quantity according to that status, so how can i do such a process from this module, i’m new to magento please give me some help THx

    • junaid says: (Author)

      Hi Sajith
      If you look at the code, this is done automatically when you update the order. Please read how Magento handles orders when it is updated for more information.

  64. Sajith says:

    yes i found it Junaid thank you very much for your kind quick response,
    can u please tell is there any way to get the back end details that means think i add

    Gateway URL
    text
    58
    1
    1
    0

    i need to take this value to redirect.phtml action

    is there any way to get that value Junaid?

  65. junaid says: (Author)

    Sajith, I’m afraid this is beyond the scope of this tutorial. Good luck!

  66. Sajith says:

    its ok

  67. Sajith says:

    ohh this is related to your tutorial junaid, i think u didn’t get my question.
    eg: after your module activate , back end panel we can add gateway url
    so if i want to add it in form action without hard cording , means get the value that inserted by back end form name=”mygatewayform” method=”post” action=”http://www.gateway.com/the_url_they_gave_me”

    how to do??

    • junaid says: (Author)

      Sajith,
      This tutorial is about linking to an external payment gateway and getting a response back. This tutorial is not about getting an extension’s settings into the code as you are suggesting.

  68. manish says:

    Hii Junaid
    Excellent tutorial but i am facing a problem.When my gateway redirect me to my website it show me an error page ” Fatal error: Call to a member function getMethodInstance() on a non-object in /home/content/78/10881478/html/app/code/core/Mage/Payment/Helper/Data.php on line 119″. Please tell me how can i remove this error.

  69. Shahed says:

    I had exactly similar problem, i have resolved it by the following way.
    Here is the findings:
    First of all make it sure that you have got order id / transaction id on your payment gateway response. Usually payment gateway refer order id as transaction id, so see the documentation of payment gateway about the response variables naming.
    If you don’t find the order id / transaction id variable on the documents, you can also track it by using :

    print_r($_REQUEST);
    die();
    if($validated) {
    ……

    Lets say you have got the transaction id by the name tran_id so ,you can code like : $orderId=$this->getRequest()->getPost(‘tran_id’);

    I have solved it by this way, hope your problem also resolved.

    Let me know your update.

    Thanks
    Shahed

  70. manish says:

    hii Shahed
    I worked as you said in above post but still getting same error. if i comment $order->sendNewOrderEmail(); then it work fine for me. Please help me to resolve this problem. I am using federal bank payment gateway.

    Thanks
    Manish

  71. Vineet says:

    hi
    i m using your code on my website but when i m integrating with ccavenue and after the process of the order to ccavenue if i press the cancel button on the ccavenue page the redirect to my index.php page and the order is proccessed with out payment it is not redirecting to the failure page. I have done all the major steps as directed. Please guide on this

  72. Biodun says:

    hi junaid can i integrate it with magento 1.7.0.2 ?
    and want to ask maybe it can be develop into extension in magentocommerce

    • junaid says: (Author)

      Hi Biodun,
      This is a standard extension which should work on all versions of Magento. Any extension can be packaged and deployed on Magento Connect.

  73. manish says:

    I found the solution.

    Replace the below lines(34-35):
    $order = Mage::getModel(‘sales/order’);
    $order->loadByIncrementId($orderId);

    by below code:
    $order = Mage::getModel(‘sales/order’)->loadByIncrementId($orderId);
    if($order['increment_id'] == “”)
    {
    echo “Order ID $orderId not found, please verify this Order Number is correct.”; die();
    }
    condition to check increment_id is used in case order id returned by payment gateway is not correct.

    In my case payment gateway was not returning order id. I Use following code
    $orderId = Mage::getModel(“sales/order”)->getCollection()->getLastItem()->getIncrementId();

    and it start working fine

  74. Biodun says:

    hi junaid, i posted some error i was encountering maybe i can get help but u didnt approve it

  75. Dan says:

    Hi

    This is an old post but it has been great for helping me integrate a payment solution.

    I am 99% there but am stuck on the notification back from my payment gateway.

    The payment gateway provide a notification to the server via an HTTP GET request, letting you know if a payment was accepted or declined.

    They provide a url for this example below

    http://www.websiteaddress.co.uk/mygateway/payment/response?Operator=&SessionID=&Note=&Tariff=&Status=&Mobile=

    But I am not sure how to use this to notify site if payment has been completed.

    Any help would be great.

  76. John says:

    Thank for you module, I am having a problem with with response, the if statement do not validate response from my gateway, how do I make it that if $validate = 00 show success and redirect else error

  77. Irina says:

    Thanks for the great extension. Clear and simple instructions, easy to modify and use with Magento 1.7

  78. Hi,
    Thank you for your great guide ,
    I sucsessfully created my own module , But I need to store transaction id and other information , How I can do this? I appreciate for your answer :)
    Thank you

  79. Jayson says:

    I cant seem to make the Url redirection can somebody post a working code?

  80. Vivek says:

    What a brilliant tutorial. I was searching for the same.

    Worked in a single hit.

    A ton of thanks. You saved my hours.

  81. Dejan Ignjatov says:

    Hello and thank you for this awesome post. It’s just the thing I’ve been looking for a couple of weeks by now.
    I have a weird issue. I did all the steps on a fresh Magento copy locally (1.7.0.2) and it appeared in the admin panel. I did the logic and everything, and I uploaded the files (double-checked, all files are uploaded) on a Magento 1.6.2.0 live store. I can’t see my module there. So, same files on local and live, 1.7.0.2 version local, 1.6.2.0 live, and it doesn’t work on the live version. Any ideas?

    • junaid says: (Author)

      Hi Dejan,
      Have you tried clearing all your caches on your live site under System -> Cache Management? You can also triple check if you have uploaded all the necessary files (perhaps you forgot one?)

      • Dejan Ignjatov says:

        Yes, the cache is disabled while I am doing this.

        And I checked again, all files are there… I uploaded them as full folders in three steps, just like the packs.

        And also it doesn’t show on frontend locally either. (v 1.7.0.2). I didn’t check this before because I can’t test it on my local server. “Enabled” parameter is set to “Yes”.

      • Dejan Ignjatov says:

        Please ignore my previous replies, I had a small error in the config.xml (one name opening the tag, another closing, but it seems 1.7 ignored this somehow and showed me the module). I am testing further so I will get back as soon as I have result. Thank you for your fast reply.

      • Dejan Ignjatov says:

        OK, I made it. Thank you so much, this was such a pain for a long time and now it is solved in a couple of hours. Brilliant!

    • Dejan Ignjatov says:

      Oh, and one more thing. When I open my store locally, it doesn’t appear in the frontend.

  82. karthhhi says:

    Thanks a lot dude… you are a real life saver… never seen a tutorial and extension as simple as this.

  83. Chandler says:

    Hi, thanks for your awesome tutorial, but I encountered a little problem in 1.7.0.2. Everything is fine except that I cannot change settings in admin. nothing happens when I clicked save. Any ideas?

  84. karan says:

    thanks a lot junaid.. its working fine…

  85. Oleg says:

    Hello. Thank you for bootstarter. I’m going to use successAction and cancelAction to handle payment status. So, user successfully redirects to bank’s payment page, but when he redirects to bla/bla/bla/success, his checkout/session is lost. Something happens between

    $session->setMyVar($session->getQuoteId());
    header(‘location’. $myurl);
    exit();

    in redirectAction and

    $session-> getMyVar();

    in successAction.
    I realy got stuck with it. Please help.

  86. Anonymous says:

    What the names of all the values ​​that I can to post on the redierct page ?????

    For example getBaseGrandTotal ();?>

  87. Jokull says:

    Thanks for the tutorial!! I’m having the same problems as Chandler. Everything is worked perfectly except that I can’t seem to be able to save the config under admin->system->configurations->payment methods. There is no error message and nothing in Chrome console. Please help if you have any idea what this could be?

    And yes… I tried all the basics, like clearing the cache and reindexing

  88. eighteens says:

    hi junaid,
    can you integrate this extension with my websites? if can how much di you charge?

  89. eighteens says:

    i have send the email to info@junaidbhura.com

  90. Chero says:

    Very Helpful tutorial simple and easy to get working. I’m having a problem after moving my site from local host to a live server. I have a button on the redirect page but it does not seem to display when the site is live. Any suggestions???

    • junaid says: (Author)

      Hi Chero,
      I’m glad you found this useful. If this works on your localhost, then make sure you have uploaded all files correctly and clear all caches. Other than this, the problem could be a range of things!

  91. Roop says:

    Very Good Article, Thanks …………………..

  92. Rahul says:

    Thanx for this, I need some new thing and got it here

  93. Pankaj says:

    Hi junaid,

    I have created a custom paument gateway but i am not able to debug in capture function and orders are successful created

    thanks

    __(‘Invalid amount for capture.’));
    $order = $payment->getOrder();
    echo “”;
    $file = fopen(“test.txt”,”a”);
    fwrite($file,’123′);
    fwrite($file,print_r($order,true));
    Mage::log(“Logging a message pankaj”);
    Mage::log(print_r($order,true));

    print_r($order,true);die;
    $this->_place($payment, $amount, self::REQUEST_TYPE_AUTH_CAPTURE);
    $payment->setSkipTransactionCreation(true);
    return $this;
    }
    }
    ?>

  94. sathiesh says:

    thank you for your wonderful post
    I am having issue with red hat OS. I am able to successfully configure with your above steps in ubuntu
    But
    Its not working in Red hat I am not able to see the link in system – > configuration – > advanced – > advanced
    Could you please help me

    • junaid says: (Author)

      Hi Sathiesh,
      The OS is not the problem. There must be a problem with your Magento installation. Have you cleared all your caches?

  95. Oluwafemi Korede says:

    Thanks for this module, this helped a lot, but I have a question, the payment gateway I am to connect to is requesting for the following

    Pending Return Url
    Cancel Return Url
    Success Return Url
    Failure Return Url
    I am confused on what to use…
    am I suppose to use http://mysite.com/mygateway/payment/response for everything..
    Please your response will be appreciated…
    Thanks.

    • junaid says: (Author)

      Hi there,
      Each URL points to a function in the controller. For example, payment/response points to PaymentController -> responseAction . If you want a cancel URL, you can make another function in PaymentController called cancelAction() . For success, successAction() , etc.

  96. Oluwafemi Korede says:

    Thanks a lot, this was really helpful for me

  97. Oluwafemi Korede says:

    One more question to ask sir,

    How do I get the type of product being bought and the email of the user buying the product at my checkout page, I mean the redirect.phtml

    Thanks..

  98. Kostas says:

    Excellent!!!!

    Thank you man. This must be the #1 tutorial for understanding the concept right away!

  99. German says:

    Hello, i’m creating my own payment method gateway, following your steps but the payment controller doesn’t seem to handle the response of the gateway properly.
    I could verify the gateway recives my POST correctly, but I don´t know why responseAction doesn’t handle gateway´s response. I woluld like to know how I must change or verify ? Thank you for your help..

  100. Baljeet Singh says:

    I am getting error. Please help me to resolve this.

    Fatal error: Call to a member function getMethodInstance() on a non-object in /opt/lampp/htdocs/magento1.6/app/code/core/Mage/Payment/Helper/Data.php on line 119

  101. Julien says:

    if i need to send error message from the paymentcontroller.php to the failer redirected page what function should i use??

    • junaid says: (Author)

      Add the error before redirecting to the failure page. Something like this:
      Mage::getSingleton( ‘customer/session’ )->addError( ‘There was an error…’ );

  102. Sajeewa says:

    thank you so much, it works fine :)

  103. Jack says:

    Hi. i have everything working except for one issue. when i checkout and the gateway posts back to my success url, the order is entered into sales > orders but doesnt create the invoice and apply payment to the order – it shows total due $the amount.

    in my redirect.phtml file i have tried the clientSuccessfulPurchaseUrl as both $site_url;?>mygateway/payment/response and $site_url;?>checkout/onepage/success

    in both cases, successful transactions are showing as successful to the customer but in admin > sales > orders the payment isnt being applied.

    the gateway has confirmed that they are receiving and processing the transaction and posting back to my success url.

    any help would be great. thanks.

    • junaid says: (Author)

      Hi Jack,
      The Sales -> Invoices are different from Sales -> Orders. You need to create the invoices manually from the orders, it won’t automatically get created after a payment, unless you write code for that. The payment wont be “applied” to the order, only the status would change to “processing”. Hope this helps.

      • Jack says:

        i see. so i have everything working as intended? in this case, i would need to cross reference my pending orders with my gateways transactions and manually create my invoices in magento to apply payment?

        in terms of writing the additional code to perform the automatic creation of invoices, is this something you could do as a project for me?

        • junaid says: (Author)

          Hi Jack,
          You don’t need to cross-reference your payments with the gateway. That’s where the order status comes in. Pending orders would have a status ‘pending’ , and orders which have been paid will have a status ‘processing’. This is set in the responseAction()

          Please send me an email regarding anything outside of this tutorial. My email address is in the contact page.

  104. Jitendra says:

    You Are Genius .

  105. Oscar says:

    Hi Juanid

    Thanx for this excellent contribution. In my case everything is working fine but order email is not been sent. Any idea?

    Best regards

  106. Parag Kamble says:

    Hi Juanid,

    This extension is really helpful and helped us a lot but I got stuck with the following:
    I have integrated HDFC payement gateway with mygateway but I am stuck with responseAction(), $this->getRequest()->isPost() is is returning false, response is coming through hdfc as post data but it doesn’t retrieve post data in responseAction().
    Can you please suggest me how do I get post response in responseAction(). It would be great if you add your inputs on this.
    Thanks in advance.

    Regards,
    Parag Pradip Kamble.

  107. Wale Dayo says:

    Great Post and very helpful,
    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

  108. Wale Dayo says:

    Great Post and very helpful,
    When i passed the http://www.yourwebsite.com/mygateway/payment/response as the response url to my gateway plaform, am getting this error
    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
    Thanks

  109. Luis says:

    save my life, god bless you and the Guadalupe Virgin pay your wonderful post really save my life, do you have any paypal acoount for sendind some of my apreciate XD

  110. Eve says:

    Hi Junaid, so glad to found these resources. I’m also stuck with the response now.I redirected my response,using the below link:
    http://localhost/magento/test/index.php/mygateway/payment/response/?validated=1;orderid=100000024

    but it will be redirected back in http://localhost/magento/test/index.php (home page).it will not display any success or error page.

    I added on response action from the payment controller the ff. lines:
    if(isset($_GET['validated']))
    {
    $validated=$_GET['validated'];
    }
    else
    {
    $validated=false;
    }
    if(isset($_GET['orderId']))
    {
    $orderId= $_GET['orderId'];
    }
    else
    {
    echo “no orderId”;

    }
    print_r($validated);

    expectedly, it should go inside if($validated) condition. but then its not working. Hope you can help me on these.

    Thanks,

    EvE

  111. Eve says:

    Already got different response now, after I change it into POST. but it’s Fatal error already line 119. Please enlighten me on these.

    Thanks,

    EvE

  112. Eve says:

    sent the wrong case for the variable.Now its having different behaviour. after payment its redirecting to the empty cart. Any idea on these?

    Thanks!

  113. Sebastian says:

    Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!! God bless you Friend!! I finished my Module!!

  114. harru jasson says:

    Hi
    Thanks for nice tutorial. It real work nice. but i have one problem occur , when i proceed to checkout no any display the payment method . please help me.

  115. John says:

    Hello,

    First of all, thank you for this great post and great magento module!

    I’m having a little problem with it. I have tested it on a fresh magento installation and work great, but when I try to addapt it on my website , when I place an order, instead of getting me to the payment gateway link from ‘redirect.phtm’, it get me to http://www.mywebsite.com/mygateway/payment/redirect‘ (mywebsite is just for example, it’s actually my website link)
    Could you help me with this problem?
    Also, I want to know how much it cost, if you want, to integrate this module to my website, I’m a beginner and I don’t have so much time to spend on it.

    Thank you!

  116. Said says:

    How to extent this module to perform “cancel order” and “refund” actions???

Leave a Comment

What do you think about this post?