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.
Cheers.
17 comments
Hello, i am creating the mobile, app. i have added the paypal module in app , and i am using the magneto. i am getting paid by paypal and returing the paypal transaction id. but i dont know how to set that id with magneto order (order either created by coding or Order creatind by magneto soap api v1) can you help me? so payal update the order directly when payment is done.
Here is i wrote details on the post topic for PayPal Magento order and IPN setup
http://www.yogisfunda.com/magento-paypal-order-status-pending-payment-setting-paypal-ipn-magento/
Hi Dear,
I need your help for how can I customize the PayPal payment notification email which is received by merchant. I want to display my website URL in that email.
Did anyone managed to figure out how to fix IPN issue in latest Magento release 1.7.0.2? All of our orders are stuck in Pending Payment, and information Magento give s to setup IPN does not work!
How to fix this please help me …
exception ‘Exception’ with message ‘PayPal IPN postback failure. See paypal_unknown_ipn.log for details.’ in \app\code\core\Mage\Paypal\Model\Ipn.php:156
Stack trace:
#0 \app\code\core\Mage\Paypal\Model\Ipn.php(114): Mage_Paypal_Model_Ipn->_postBack(Object(Varien_Http_Adapter_Curl))
#1 \app\code\core\Mage\Paypal\controllers\IpnController.php(43): Mage_Paypal_Model_Ipn->processIpnRequest(Array, Object(Varien_Http_Adapter_Curl))
#2 \app\code\core\Mage\Core\Controller\Varien\Action.php(419): Mage_Paypal_IpnController->indexAction()
#3 \app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch(‘index’)
#4 \app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#5 \app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#6 \app\Mage.php(683): Mage_Core_Model_App->run(Array)
#7 \index.php(87): Mage::run(”, ‘store’)
#8 {main}
Dear Friend,
I am using magneto 1.7.0.2 and Themeforest shopper theme v 1.1 which is available to download, I am updating my site
to this theme right now I am working on my localhost xampp on windows xp.
The problem i am facing is this that i tried to set up PayPal payment option on the admin panel as paypal standard but
paypal option is not visible on the payment option in the theme where we do payment.
I have integrated cc avenue and it is working fine as i have to made changes after downloading the kit form the cc
avenus as per the pdf document provided by them.
There are other option which are visible in the front end when i select yes option on the admin panel like :- saved
cc, cash on payment etc but only pay pal is not visible in the front.
Just to add in i have used all different currency like Indian Rupee, USD, Pound etc but still paypal option is not
visible, I want to use pay pal for all the currency even for Indian Rupee is it possible.
I am attaching an attachment showing options but no Paypal and I am using USD as a currency option.
Need help as i am trying to change to this theme of shopper by this month of may.
Photo Attached : – http://www.flickr.com/photos/84479482@N06/
Regards,
Abhishek
Well, we seem to be having issues with the PayPal IPN being stuck on ‘Retrying’ when our customers make purchases through Ebay. PayPal is sending the notification but our listener is not recognizing the order ID since it was not made on the site so it does not respond back. Wondering if anyone can lead me in the right direction to add something to the listener to respond back to PayPal saying ‘OK’. After four days of ‘retrying’, paypal shuts the IPN off.
Thanks.
Thanks for the tip, we had a couple orders that didn’t update in Magento but were successful. They were causing the exception “PayPal IPN postback failure.” so after tracing the stack, they were being caused in the Ipn Model in the _postBack function here: if ($response != ‘VERIFIED’) {
So I also suggest adding in this log: just before that line: Mage::log(“PayPal IPN Post back debug: “.print_r($this->_debugData, true), null, “paypal_ipn_postback.log”);
I have magento 1.7.0.2 and I don’t see paypal ipn option. Was taken out ?
As allways, thank you for quality articles.
Andy – You pretty much just saved my life! I was going insane over this! And found out that the registered e-mail was with a capital first letter.. jesus… How stupid is this.
anyways, thanks!
I have found this error in exception log when i install ( copy ) it..It makes the whole site down. Please help..!!
Mage_Core_Exception’ with message ‘Mage registry key “_singleton/Empty_Cart_Model_Observer” already exists’ in /var/www/vhosts/XXXXXX.com/httpdocs/app/Mage.php:550
Thanks Branko – again.
This helped me find my error in code, since there is no other way of debugging IPN, and it helps to be able to see what is happening. I agree with Tomislav – a good example of Mage::log usage!
What a completely useless article.
Since no default paypal gateway updates the order status to ‘complete’ upon a successful IPN verification, maybe you could have shown us something more specific instead of super basic debugging techniques.
Hey everyone!
Well after few ours into this im going crazy this is whats happening, i have imported all my products to magento , all seamed to be fine in the front and back office, until some paypal orders starting to arrive. For some reason when checking out, some products don’t appear in the description list of the paypal website checkout like no items. Not even the shipping is there… but this is not happening on all products, so i went to de specific product they bought check everything and did not see anything out of the ordinary, clicked saved then i saw category tree checked list disappear, i re-checked the categories hit save and then try to do a purchase with this product and bam! Details where passed to paypal website checkout….. i know is super weird can someone please help me? i have 10,000 products.
paypal log shows this difference
after saving the products this appears on the variables
[item_number_1] => 461994
[item_name_1] => PINK SUGAR
[quantity_1] => 2
[amount_1] => 11.81
[item_number_2] => Vous avez le choix entre: – La Poste – Colis Standard, Livraison entre 7 et 12 jours
[item_name_2] => Livraison
[quantity_2] => 1
[amount_2] => 15.00
Thank you Branko. It is helping me at he moment
One tip that a few of our customers have fallen foul of:
your paypal email address as registered with paypal and your paypal email address as entered into the Magento backend are case sensitive.
So you if you register with paypal as cooLmE@something.com
you must enter the exact same info into the magento backend.
Drove me slightly mad the 1st time I came across this.
Just joking. Seems like a good Mage::log usage 🙂