Posted: 5th August 2014

Author: Ric

Tagged: Tutorials

Setting up a Paypal account to use with an IPN enabled shopping cart

Paypal is one of the nicest platforms to take online payments through. A developer can build Paypal integration in to just about any website relatively easily and most web users already know of and trust the service. You also don't usually need to worry about securing a website with an SSL certificate as Paypal payments are taken off-site.

Most Paypal integrations make use of Instant Payment Notifications, or IPN. Essentially, when your Paypal account receives a payment, it notifies a script on your website so that the website can acknowledge the payment, perhaps to reduce stocks or to flag an order as paid and ready for shipping.

Actual integration is dependent on specific project requirements, but, regardless of how your website works, if you want to use IPN you need to enable it in your Paypal account. Here's how:

  1. Log in to your Paypal account and navigate to Profile & Settings via the icon in the upper right.

  2. From the left, navigate to Selling Preferences.

  3. Then from the list on the right, IPN Settings.

  4. You want to enable IPN from the options on this page, but to do so you also need to provide an IPN notification url. This is the url of a script that receives payment notifications, but since you likely use your Paypal account for all sorts of things, you probably don't always want this to be the script used by the website you're enabling for. Instead, type in the url of a 'dummy script' and hit save.

  5. You now need a dummy script at this address. Here's one that we built which you can give to your developer to put online. It doesn't do anything other than accept the notification to keep Paypal happy, but it could be extended to keep a log of all your incoming payments, for example.
<?php
    $req = 'cmd=_notify-validate';

    foreach($_REQUEST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= '&'.$key.'='.$value;
    }

    $header = 'POST /cgi-bin/webscr HTTP/1.1'."\r\n"
             .'Content-Type: application/x-www-form-urlencoded'."\r\n"
             .'Host: www.paypal.com:443'."\r\n"
             .'Content-Length: '.strlen($req)."\r\n"
             .'Connection: close'."\r\n\r\n";

    $fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);

    if($fp) {
        fputs($fp, $header.$req);

        while(!feof($fp)) {
            $res = fgets($fp, 1024);
        }

        fclose ($fp);
    }
?>

You're now ready to use your account with an IPN enabled shopping cart, which should override this dummy script with it's own. Your developer just needs to know your merchant ID which is shown at the bottom of Profile & Settings.

Blog posts written by former QWeb employees are not necessarily an accurate indication of the current opinions of QWeb Ltd and the information provided in tutorials might be biased or subjective, or might become out of date.

Discuss this post

Leave a comment

Your email address is used to notify you of new comments to this thread, and also to pull your Gravatar image. Your name, email address, and message are stored as encrypted text and you won't be added to any mailing list and your details won't be shared with any third party.