Return to Snippet

Revision: 11489
at February 6, 2009 20:23 by xtheonex


Initial Code
<?php
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
	$value = urlencode(stripslashes($value));
	$req .= "&$key=$value";
}
$header .= "POST /cgi-bin/webscr HTTP/1.0
";
$header .= "Host: www.paypal.com:80
";
// $header .= "Host: www.sandbox.paypal.com:80
";
$header .= "Content-Type: application/x-www-form-urlencoded
";
$header .= "Content-Length: " . strlen($req) . "

";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
// $fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
if (!$fp) {
	// Do some code when your server cannot communicate with PayPal!
	mail('[email protected]', 'IPN ERROR', "HTTP ERROR\n\n" . $res);
} else {
	fputs ($fp, $header . $req);
	while (!feof($fp)) {
		$res = fgets ($fp, 1024);
			if (strcmp ($res, "VERIFIED") == 0) {
				if ($_POST['payment_status'] == 'Completed') {
					// Do some code when PayPal returns payment completed status!
					foreach ($_POST as $key => $value){
						$emailtext .= $key . ": " . $value . "\n";
					}
					mail('[email protected]', "IPN VARIABLES", $emailtext . "\n\nREQUEST STRING:\n" . $req);
				} else {
					// Do some code when PayPal returns a different status!
					foreach ($_POST as $key => $value) {
						$emailtext .= $key . ": " . $value . "\n";
				}
				mail('[email protected]', "IPN VARIABLES", $emailtext . "\n\nREQUEST STRING:\n" . $req);
			}
		} elseif (strcmp ($res, "INVALID") == 0) {
			// Do some code when PayPal returns some error!
			foreach ($_POST as $key => $value){
				$emailtext .= $key . ": " . $value . "\n";
			}
			mail('[email protected]', "IPN VARIABLES", $emailtext . "\n\nREQUEST STRING:\n" . $req);
		}
	}
fclose ($fp);
}
?>

Initial URL

                                

Initial Description
This is a basic shell for PayPal IPN.
It is by no means a complete solution, but it should provide a good starting point for anyone that wants to work out IPN.
Or it can be used as is to do basic IPN.

Initial Title
PayPal IPN Shell

Initial Tags
php

Initial Language
PHP