baumi's blog

baumi's personal blog … Linux, OS X, Windows, Random things, …

Magento / PayPal IPN: what to do if paypal IPN occasionally isn’t working? – plan B

Occasionally, and with different kinds of webshop technologies it happens once in a while that the PayPal IPN (instant payment notification) doesn’t go through. Internet forums are filled with different reports about this phenomenon, too. – What happens? Basically a customer paid, payment went through, but the webshop doesn’t receive the IPN payment notification from PayPal. – That sucks!

So here is what i’ve come up with as our backup plan, or “plan B”:

a) observing the email box for paypal payment notifications. This part is one by a small bash script, first running fetchmail, then doing some grep/regexp to extract paypal emails and order numbers.
b) manually set the order to “paid” (if necessary) and enable the downloadable products the customer had paid for.

Code:

load($orderid, 'increment_id');
        $order_status = $order->getData('status');

        if ( $order_status != "complete" )
        {
                // ----- Invoicing -----------
                $invoice = $order->prepareInvoice()
                                 ->setTransactionId($order->getId())
                                 ->addComment("Auto-created invoice by PayPal-IPN-Checker")
                                 ->register()
                                 ->pay();
                $order->addRelatedObject($invoice);

                #$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, "Message");
                $order->sendNewOrderEmail();
                $order->setEmailSent(true);
                $order->save();
                // ----------------------------


                // ----------
                // Enable the purchased products for download
                // Source: http://magento.stackexchange.com/questions/17246/download-link-is-not-showing-for-the-programmatically-added-downloadable-product
                $orderItems = Mage::getModel('sales/order')->load($order->getId());
                foreach ($orderItems->getAllItems() as $itemId => $orderItem)
                {
                        $product = Mage::getModel('catalog/product')->load($orderItem->getProductId());
                        if($product->getTypeId()=='downloadable')
                        {
                                $product_links = Mage::getModel('downloadable/product_type')->getLinks( $product );
                                foreach ($product_links as $link)
                                {
                                    $downloadPurchase = Mage::getModel('downloadable/link_purchased')
                                        ->setOrderId($orderItem->getOrderId())
                                        ->setOrderIncrementId($orderItems->getIncrementId())
                                        ->setOrderItemId($orderItem->getId())
                                        ->setCreatedAt(date('Y-m-d H:i:s'))
                                        ->setUpdatedAt(date('Y-m-d H:i:s'))
                                        ->setCustomerId($orderItems->getCustomerId())
                                        ->setProductName($product->getName())
                                        ->setProductSku($product->getSku())
                                        ->setLinkSectionTitle('Click here to download');

                                    $downloadPurchase->save();

                                    $linkHash = strtr(base64_encode(microtime() . $downloadPurchase->getId() . $orderItem->getId() . $product->getId()), '+/=', '-_,');
                                    $downloadItem = Mage::getModel('downloadable/link_purchased_item')
                                        ->setProductId($product->getId())
                                        ->setNumberOfDownloadsBought(0)
                                        ->setNumberOfDownloadsUsed(0)
                                        ->setLinkTitle($link->getTitle())
                                        ->setIsShareable($link->getData('is_shareable'))
                                        ->setLinkFile($link->getData('link_file'))
                                        ->setLinkUrl($link->getData('link_url'))
                                        ->setLinkType(Mage_Downloadable_Helper_Download::LINK_TYPE_URL)
                                        ->setStatus('available')
                                        ->setCreatedAt(date('Y-m-d H:i:s'))
                                        ->setUpdatedAt(date('Y-m-d H:i:s'))
                                        ->setLinkHash($linkHash)
                                        ->setOrderItemId($orderItem->getId())
                                        ->setPurchasedId($downloadPurchase->getId());

                                    $downloadItem->save();
                                }
                        }
                }
                // --------

       }
?>

Comments are currently closed.