PayPal IPN

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hey everyone,

So I know this is related pretty strictly to paypal... But I also know that you all most likely use it :)

So with that said... Has anyone successfully setup the IPN with paypal? I'm trying to figure out to get it up and working and can't seem to wrap my head around it... But then again, I've been at it for a few hours so I'm hoping in the morning it gets more clear!

Here is the test code that I am working with:

<?PHP

error_reporting(E_ALL ^ E_NOTICE);
$email = $_GET['ipn_email'];
$header = "";
$emailtext = "testing 123";
// Read the post from PayPal and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc'))
{
$get_magic_quotes_exits = true;
}
foreach ($_POST as $key => $value)
// Handle escape characters, which depends on setting of magic quotes
{
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1){
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
// Post back to PayPal 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 ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);//Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);// Live
// Process validation from PayPal
// TODO: This sample does not test the HTTP response code. All
// HTTP response codes must be handles or you should use an HTTP
// library, such as cUrl
if (!$fp) { // HTTP ERROR
} else {
// NO HTTP ERROR
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// TODO:
// Check the payment_status is Completed
// Check that txn_id has not been previously processed
// Check that receiver_email is your Primary PayPal email
// Check that payment_amount/payment_currency are correct
// Process payment
// If 'VERIFIED', send an email of IPN variables and values to the
// specified email address
foreach ($_POST as $key => $value){
$emailtext .= $key . " = " .$value ."\n\n";

}
mail("pruimj@xxxxxxxxx", "Live-INVALID IPN", $emailtext . "\n\n" . $req);

//removed for testing
//mail($email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req);
} else if (strcmp ($res, "INVALID") == 0) {
// If 'INVALID', send an email. TODO: Log for manual investigation.
foreach ($_POST as $key => $value){
$emailtext .= $key . " = " .$value ."\n\n";
}
mail("pruimj@xxxxxxxxx", "Live-INVALID IPN", $emailtext . "\n\n" . $req);

//Removed for testing
//mail($email, "Live-INVALID IPN", $emailtext . "\n\n" . $req);
}
}
}
fclose ($fp);
?>

Thanks everyone! Hoping to find a easy fix to it!



Jason Pruim
pruimj@xxxxxxxxx



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux