Paypal IPN Listener class.

I’ve only used Paypal IPN a couple times. Both time were in systems where the IPN was already set up. It didn’t take much work for me to actually do anything from there. I am working on a new WordPress module. Part of which includes functionality where I needed to do some paypal processing. While searching through the internet I found a lot of sample code, but nothing that was a simple object. After some headaches caused by a typo, I have a php5 class for Paypal IPN validation.


class PaypalIPNListener
    protected $payment_status = false;
    private $sandboxUrl = "";
    private $productionUrl = "";
    private $serviceUrl = null;
    private $serviceData = "";
    public function __construct($postData = array(), $sandbox=false)
		// Set the initial serviceUrl
		$serviceUrl = $productionUrl;

		// If sandbox change the serviceUrl
		if($sandbox == true) $serviceUrl = $sandboxUrl;

        // Save the post data for later use.
        $this->postData = $postData;

        // If we are debugging switch the URL
        $serviceUrl =  ($sandbox) ? $sandboxUrl : $productionUrl;
        // Add the validate notify command to the post data.
        $postData['cmd'] = '_notify-validate';
        // Create a querystring from the data.
        foreach($postData as $key => $val)
                if($serviceData != "") $responseData .="&";
                $serviceData .= $key . "=" . urlencode($val);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->serviceUrl);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $output = curl_exec($ch);
        // If the payment is verified then set the status as verified.
        if($output == "VERIFIED")
            $this->status = PAYPAL_IPN_LISTENER_VERIFIED_STATUS;
            $this->payment_status = $postData['payment_status'];

    public function getPaymentStatus()
    	return $this->payment_status;

    public function __toString()
        return $this->status;

The code makes creating an IPN script simple for anyone.

// Create the IPN Listener object. (passing true as the 2nd parameter
// sets you up with the sandbox for development.
$ipn = new PaypalIPNListener($_POST, true);

Once you create the object there is nothing left for you to do for the validation process to begin. It has already begun, and ready for you to use. How do you use it? Like this.

// IPN Listener Object
$ipn = new PaypalIPNListener($_POST, true);

    // Payment is verified.   We use the magic method __toString to return the validation status.
    // Then it's as simple as checking the $_POST data for the ['payment_status'] to make sure they
   // weren't denied or that something didn't go wrong on paypals side.
    // something went wrong with the validation process.  You should handle it here.
    // at least log it so that you have a record of the failed validation.

To go into production mode either pass “false” as the second parameter or remove it entirely only passing the post data. Once you do that you are GOOD TO GO!

2 Responses to “Paypal IPN Listener class.”

  1. Thanks! That’s really coming in handy!

    But it seems that cURL is not installed with WordPress. What do you recommend?

  2. Hey Flo, cURL isn’t something that gets installed with WordPress. cURL is a php extension and it is usually installed, but some ISPs do not install it. Unfortunately my PHP class won’t work without it. There are other code snippets on the web that use fopen, but again some ISPs turn off remote fopen requests. I’d contact your web host and see if they can turn cURL on for you, if not I’d find another host. MediaTemple is a great host, I have been using them for a year, and I love it.

Leave a Reply

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

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 46,752 bad guys.