function VerifyIPN($ipn){ $req = 'cmd=_notify-validate'; foreach ($ipn as $key => $value) { if (!eregi("^[_0-9a-z-]{1,30}$",$key) || !strcasecmp ($key, 'cmd')) { unset($key); unset($val); } if ($key != '') { $value = urlencode(stripslashes($value)); $req .= "&$key=$value"; } } // post back to PayPal system to validate $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; $fp = fsockopen ((isset($ipn['test_ipn']) ? 'www.sandbox.paypal.com' : 'www.paypal.com'), 80, $errno, $errstr, 30); if (!$fp) { header('Status: 404 Not Found');// HTTP ERROR exit; } else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); } fclose($fp); $res=trim($res); if (strcmp ($res, "VERIFIED") == 0) { /** * VALID IPN */ return 1; }else{ /** * FAKE/BAD IPN */ return 0; } } return 0; } /** * Returns status of IPN, * 4 - Complete * 3 - Pending * 2 - Denied (Received denied) * 1 - Failed * 0 - Invalid IPN (Fail?) * * @param Array $ipn * @return integer */ function IPNStatus($ipn){ if(VerifyIPN($ipn)) { if ($ipn['payment_status'] === 'Completed') return 4; else if ($ipn['payment_status'] === 'Pending') return 3; else if ($ipn['payment_status'] === 'Denied') return 2; else if ($ipn['payment_status'] === 'Failed') return 1; else return 0; }else{ return 0;// Fake IPN } ; } function ProcessIPN($ipn){ $ppstatus=IPNStatus($ipn); if($ppstatus == 1 || $ppstatus == 2) { //denied/failed } else if($ppstatus == 4) { //successful payment }elseif($ppstatus == 3){ $pr=$_POST['pending_reason']; if($pr == 'unilateral'){ $pr = 'unilateral - The owner has not verified that they own the paypal email address. You must wait until the email is claimed (and hope it is not someone elses)