Magento News

 

Debugging PayPal IPN in Magento

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 -

Possibly Related Posts:


 

Leave a Reply