How to set PayPal IPN for Multiple and single store, and get rid of IPN warning mails

How to set PayPal IPN for Multiple and single store, and get rid of IPN warning mails

Magento has always been a favorite platform for Ecommerce store owners because of its rich features and easy accessibility. Similarly, PayPal gateway is famous for its worldwide usage and high-end security. One chooses his/her store to be built with best technology and hence opt for Magento with integration of PayPal gateway. But, side-by-side they start facing the most common issue related to IPN ”“ yes Instant payment notification by PayPal. Our developers have bought an end to this “complete irritating notifications” with a simple solution.

Some of the most common issues:

“You need to enter the Notification URL for IPN and if the URL entered is correct, the order placement is successful.” This is the most basic thing that we learn while integrating. But the document does not mention the case of Single vs. Multiple store. What if I have multiple store?? It remains unanswered.
Even if you have not entered the Notification URL, it’s the beauty of Magento that the PayPal gateway gets integrated and the ordering becomes a successful process as Magento takes default URL as its IPN notification.

Problems faced by single store owners:

Single store users face the most basic issue. They sometimes enter the wrong URL or instead of URL they enter web address or forget the “/” at the end. This results in wrong IPN. So, whenever the order is placed, store owners start receiving failed PayPal IPN error mail. This happens every time when a new order is being placed.

The message will be as shown below:

IPN-message

This constant messages by PayPal leads to a failure from store owner’s side and thus they end up with loss.

Solution for Single store users:

There are 2 ways to solve the IPN issue.

Method 1:

Single store users can make use of default Magento functionality and set the IPN.
Steps:

  1. Click Profile on the My Account tab
  2. Click Instant Payment Notification Preferences in the Selling Preferences column.
  3. Click Choose IPN Settings to specify your listener’s URL and activate the listener.
  4. The following screen appears:

    PayPal-configurations

  5. Select “Do not receive IPN messages” and click save.

Method: 2

Repeat the steps as described in Method 1, but step: 4 differs. Instead of disabling the IPN, enter the correct URL (P.S: Correct URL should end with “/”) of the store. You will start receiving messages on successful ordering.
The format of the URL should be: /paypal/ipn/

Problems faced by multi-store owners:

In case of multiple store, when you enter the IPN URL, PayPal sends notification to the default store leaving store owners with half information. Store owner gets mail by PayPal of wrong IPN and the issue remains unsolved.
Other than that, when there are multi domains, store owners face the dilemma as to which URL should be submitted at the IPN notification gateway. Orders excluding from the submitted URL will mark as unsuccessful process and store owners will get the error notification mail as described in the single domain case.

Solution for multi store users:

Steps to set up IPN:

  1. Create a folder name “Paypalipn” in the root
  2. Create a sub folder name “ipn” in the Paypalipn folder
  3. Create the php file named “paypal_ipn.php” with the following code under ipn folder
  4. <?php
    // CONFIG: Enable debug mode. This means we’ll log requests into ‘ipn.log’ in the same directory.
    // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
    // Set this to 0 once you go live or don’t require logging.
    define(“DEBUG”, 1);
    // Set to 0 once you’re ready to go live
    define(“USE_SANDBOX”, 1);
    define(“LOG_FILE”, “./ipn.log”);
    // Read POST data
    // reading posted data directly from $_POST causes serialization
    // issues with array data in POST. Reading raw POST data from input stream instead.
    $raw_post_data = file_get_contents(‘php://input’);
    $raw_post_array = explode(‘&’, $raw_post_data);
    $myPost = array();
    foreach ($raw_post_array as $keyval) {
    $keyval = explode (‘=’, $keyval);
    if (count($keyval) == 2)
    $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
    // read the post from PayPal system and add ‘cmd’
    $req = ‘cmd=_notify-validate’;
    if(function_exists(‘get_magic_quotes_gpc’)) {
    $get_magic_quotes_exists = true;
    }
    foreach ($myPost as $key => $value) {
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
    $value = urlencode(stripslashes($value));
    } else {
    $value = urlencode($value);
    }
    $req .= “&$key=$value”;
    }
    // Post IPN data back to PayPal to validate the IPN data is genuine
    // Without this step anyone can fake IPN data
    if(USE_SANDBOX == true) {
    $paypal_url = “https://www.sandbox.paypal.com/cgi-bin/webscr”;
    } else {
    $paypal_url = “https://www.paypal.com/cgi-bin/webscr”;
    }
    $ch = curl_init($paypal_url);
    if ($ch == FALSE) {
    return FALSE;
    }
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    if(DEBUG == true) {
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
    }
    // CONFIG: Optional proxy configuration
    //curl_setopt($ch, CURLOPT_PROXY, $proxy);
    //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    // Set TCP timeout to 30 seconds
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Connection: Close’));
    // CONFIG: Please download ‘cacert.pem’ from “http://curl.haxx.se/docs/caextract.html” and set the directory path
    // of the certificate as shown below. Ensure the file is readable by the webserver.
    // This is mandatory for some environments.
    //$cert = __DIR__ . “./cacert.pem”;
    //curl_setopt($ch, CURLOPT_CAINFO, $cert);
    $res = curl_exec($ch);
    if (curl_errno($ch) != 0) // cURL error
    {
    if(DEBUG == true) {
    error_log(date(‘[Y-m-d H:i e] ‘). “Can’t connect to PayPal to validate IPN message: ” . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
    }
    curl_close($ch);
    exit;
    } else {
    // Log the entire HTTP response if debug is switched on.
    if(DEBUG == true) {
    error_log(date(‘[Y-m-d H:i e] ‘). “HTTP request of validation request:”. curl_getinfo($ch, CURLINFO_HEADER_OUT) .” for IPN payload: $req” . PHP_EOL, 3, LOG_FILE);
    error_log(date(‘[Y-m-d H:i e] ‘). “HTTP response of validation request: $res” . PHP_EOL, 3, LOG_FILE);
    }
    curl_close($ch);
    }
    // Inspect IPN validation result and act accordingly
    // Split response headers and payload, a better way for strcmp
    $tokens = explode(“\r\n\r\n”, trim($res));
    $res = trim(end($tokens));
    if (strcmp ($res, “VERIFIED”) == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment and mark item as paid.
    // assign posted variables to local variables
    //$item_name = $_POST[‘item_name’];
    //$item_number = $_POST[‘item_number’];
    //$payment_status = $_POST[‘payment_status’];
    //$payment_amount = $_POST[‘mc_gross’];
    //$payment_currency = $_POST[‘mc_currency’];
    //$txn_id = $_POST[‘txn_id’];
    //$receiver_email = $_POST[‘receiver_email’];
    //$payer_email = $_POST[‘payer_email’];
    if(DEBUG == true) {
    error_log(date(‘[Y-m-d H:i e] ‘). “Verified IPN: $req “. PHP_EOL, 3, LOG_FILE);
    }
    } else if (strcmp ($res, “INVALID”) == 0) {
    // log for manual investigation
    // Add business logic here which deals with invalid IPN messages
    if(DEBUG == true) {
    error_log(date(‘[Y-m-d H:i e] ‘). “Invalid IPN: $req” . PHP_EOL, 3, LOG_FILE);
    }
    }
    ?>

    OR

    Download the zip folder from here and follow the steps:

    • Extract the folder paypal_ipn
    • Copy the entire folder in the root directory

    Thus you will have a newly created URL as <domain>/paypalipn/ipn/paypal_ipn.php

  5. Enter the newly created URL in the notification IPN section which you get by log in to My Account -> Profile -> Instant payment notification.

Thus, the IPN problem is solved once and for all. Now, it doesn’t matter whether you hold a single store or a multiple store, you can easily integrate the store with PayPal gateway including IPN notification and make the most of it.
Kindly, feel free to ask your doubt or give us a feedback in the comment section below. We will help you out in the best possible way.

Effectively setup PayPal IPN on single and multi store Magento. Stop receiving paypal IPN warning emails Click To Tweet
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
17 Comments
  1. Hi,

    Cant seem to get this to work correctly? I have entered the new URL in Paypal but Magento uses notify_url which seems to override what I have entered in the IPN URL in Paypal “/paypalipn/ipn/paypal_ipn.php”

    Therefore back to the same problem before.

    Thanks

    Sam

    1. Hi Sam,
      You have to include a domain name in “/paypalipn/ipn/paypal_ipn.php”.
      Make sure you have downloaded and copied the paypal_ipn folder in root. lastly you have to set 0 in “define(“USE_SANDBOX”, 1);” if it works proper in test mode. I hope making all these changes help you overcome the issues still if the problem persists, you can check for the error in ipn.log file or Contact Us to get help.

      1. Hi,

        though the last comment is already one year old, I have the same problem. I set up the IPN in the PayPal account, linking to https://www.mydomain.com/paypalipn/ipn/paypal_ipn.php

        Using the IPN Simulator, looks like it’s working fine. But Magento uses a notify_url in the form before the user arrives to PayPal site, and this parameter override the URL set in the PayPal account.

        Maybe the solution is to delete the notify_url parameter from the Magento form?

  2. Hi there,

    I am looking for the correct settings if you run 2 magento installations (both multi store) on one paypal account.
    It really seems impossible to enter a correct url in paypal at My Account -> Profile -> Instant payment notification, why not just leave this section in paypal blank?
    Any advice is much aprecataed, thanks Tijmen

    1. Hi Tijmen,
      The only reason why you cannot keep the URL section blank is, when the order is placed from any of the stores from multistore, a proper URL need to send for the return notifications. If you haven’t set any, it might return to the incorrect store from which actually order is not placed. So It will reject IPN request.

  3. Hi,
    We start using paypal ipn for our magento requirring profiles paypal payments, magento start creating orders after each paypal automatic payment, so ipn works, but we start receive paypal ipn failure emails with 503 return error.
    I’ve uploaded your folders to root directory and change paypal ipn url to it’s php file. Now we dont receiver ipn failure messages, but magento stop creating orders after automatic paypal payments.
    In paypal ipn history we see, that now ipn return message is 200.
    Please help.
    We set sandbox mode to 0. Should we download file caextract.html in our case?

    Thank you

    1. Hello SH77,
      To understand your problem specification in a better way, we will require some of your store details. First of all make sure you have used appropriate method for related multi or single store. Let us know about your store setup, root URL and how you have entered URL in Paypal in order to provide you guidance in a proper direction.

          1. We will have multistore soon. Regular paypal ipn url – that what we using now and receive ipn failure emails…. So we need multistore solution

          2. The method you have implemented will work properly only once you setup multistore. For your current store setup, either use method for single store to get IPN notifications or revert all the settings back.

  4. HI

    We have multi-website and multi-stores in single magento installation with sub-domains. I followed your blog but still i am getting ipn error. I checked payment_paypal_express.log file and notifyurl is according to your blog but in DoExpressCheckoutPayment in log file notify url is old as magento default /paypal/ipn.

    mostly orders getting ipn error after successfully completion and moving to suspected fraud state . can you please help on this.

    error : IPN “Completed”. Order is suspended as its capture amount $14.05 is suspected to be fraudulent. Transaction ID: “70R49336TG306202W”.

    1. Hello
      If you followed the above steps correctly then your problem might be solved. If you still facing difficulty you can Contact Us with your details in order to get this issue solved. We will be happy to help you.

  5. Hi,

    I have received PayPal failed IPN email after this setup. I have follow all step correctly. Email is (Please check your server that handles PayPal Instant Payment Notifications (IPN). Instant Payment Notifications sent to the following URL(s) are failing: paypalipn/ipn/paypal_ipn.php)

    Thanks

  6. Hi!

    Is this solution compatible with Magento 1.9.2.1?
    We have one Magento installation with multistore (with different domains for each language – co.uk, .fr, .de) but after trying your solution we still get the “Unable to place the order.” message and the IPN Warnng emails from Paypal. The only domain that seems to work is the default one – “.com”

    Thanks,
    Ricardo

Leave a reply

Your email address will not be published. Required fields are marked *