Paypal IPN listener with Zend Framework

Below is a fully working code for creating a Paypal Instant Payment Notification listerner using Zend Framework:

/**
* Handle paypal response and return true if ok
*
* @return bool
*/
function isPaypalIPNMsgValid()
{
$client = new Zend_Http_Client(PAYPAL_IPN_URL, array(
 'maxredirects' => 0,
 'timeout'      => 250,
 'rfc3986_strict' => false)); //not rfc3986_strict necessary to be replicate with paypal input message

 global $data;
 $data['cmd'] = '_notify-validate';
 foreach ($_POST as $key => $value) { // 'cmd' arg must be first => we add other POST entries after it
  $data[$key] = $value;
 }

 $client->setParameterPost($data);
 $response = $client->request('POST'); // query paypal for transaction informations

 if ($response->isSuccessful() && $response->getBody() == 'VERIFIED') {
  return true; // OR to handle only some IPN message types: return isset($data['txn_type']) && in_array($data['txn_type'], array('express_checkout', 'web_accept'));
 }
 return false;
}

if (!isPaypalIPNMsgValid()) {
 die();
}

sources

One Comment Trackback URL | Comments RSS

  1. Thanks Says:

    Thanks for a very understandable code; nice work :)

Post a Comment

Your email is never published nor shared. You're allow to say what you want...

Powered by sweet Captcha