Debugging PayPal IPN in Magento

Posted in Magento tips & tricks, magento payment tips on January 9, 2011 0 comment(s)  

Probably one of the most used payment options (gateways) within Magento, and most likely every other cart system is the PayPal. This payment gateway is built into the default Magento installation

All you need to do is to punch in few configuration options under “System > Configuration > Payment methods“ and you are ready to go.

Great thing about the PayPal – Magento relation is the IPN feature of the PayPal itself. By definition IPN stands for Instant Payment Notification. Its a feature of PayPal that allows you to integrate your PayPal payments with your website’s back-end operations, so you get immediate notification and authentication of the PayPal payments you receive.

Luckily IPN itself is supoorted in Magento by default.

All this is great until you hit the wall with getting your orders not to update their status as they should. For instance, PayPal can send your Magento a certain IPN message and your Magento might not respond to it accordingly. This is the scenario where you need to get your hands dirty and start debugging.

A good starting point might be the Mage_Paypal_IpnController located under the app/code/core/Mage/Paypal/controllers/IpnController.php file. Within it there is only one action called indexAction(). There are merely a few lines of code inside this action which makes it pretty easy to understand. As you can see, it merely checks if the POST data exists and if it does then it passes it along to the Mage_Paypal_Model_Ipn model and its processIpnRequest() method located under the app/code/core/Mage/Paypal/Model/Ipn.php file, around line 98.

So how do you see your full IPN message withouth going into the PayPal admin interface each time? You can simply replace the indexAction() of Mage_Paypal_IpnController from:

public function indexAction()
{

if (!$this->getRequest()->isPost()) {
return;
}

try {
$data = $this->getRequest()->getPost();
Mage::getModel('paypal/ipn')->processIpnRequest($data, new Varien_Http_Adapter_Curl());
} catch (Exception $e) {
Mage::logException($e);
}
}

into something like:

public function indexAction()
{
if (!$this->getRequest()->isPost()) {
Mage::log("PayPal IPN no POST data, at ".time().".", null, "paypal_ipn_blank.log");
return;
}

try {
$data = $this->getRequest()->getPost();
Mage::log("PayPal IPN POST data: ".print_r($data, true).".", null, "paypal_ipn.log");
Mage::getModel('paypal/ipn')->processIpnRequest($data, new Varien_Http_Adapter_Curl());
} catch (Exception $e) {
Mage::logException($e);
Mage::log("PayPal IPN error: ".$e->getMessage().".", null, "paypal_ipn_error.log");
}
}

This will get your IPN related stuff nicely separated into special log files making them easier to debug. From here you can proceed to the processIpnRequest() method of the Mage_Paypal_Model_Ipn model doing similar until you reach the point where you might spot your bug in the Magento or your custom code that possibly broke the IPN functionality on Magento side.

Surely some of you will apply different, possibly better method, this is just one way of handling it.

Hope some of you find it useful.

p.s. This article was written with Magento 1.4.2.0 version in mind. Other versions of Magento might have things (models/controllers) in different places.

- From inchoo.net -


This post was posted in Magento tips & tricks, magento payment tips and was tagged with Magento tips, Magento tips & tricks

Leave a comments
Your email address will not published. Required fields are marked *

What Our Clients Say?

  • Oscar Soto MANAGER
    ArrowHiTech has always impressed me! They are professionals and are quick to response to my needs. I have used
    them in the past and will continue to use them in the future. Thanks Martin and all the Staff at ArrowHiTech!!!
  • The guys of AHT solutions did a perfect job and are real profesionals. Definitely recommend them and will use them again!
  • Jonatas Leonel MANAGER
    Martin has a great team of Magento experts, I really liked the work he did for me. I asked him to customize my template following a couple of jpg files that I got from a designer and he worked on that template without the psd's - that was awesome, the result I got was really good.
  • I really enjoy working with ahtsolutions. Quick response to our questions, delivery the project in time and excellent skills. I hope to work with them soon
Request us now! Yahoo:gravitational_boy Email:info@mage-shop.com Skype:hyun_yoo_it +84437955813