From: acos on
Does anyone have a recent _working_ example of posting back form
variables (sucessfully) to Paypal[ webpayments standard] using cfhttp
when they send an IPN message to your page for payment verification?

I downloaded their sample code but it is in PHP (sadly I can only
guess at what they are trying to accomplish) and the cfm file I got
appears to be from many years ago which doesn't appear to function
properly anymore.

Any help would be appreciated.

If I can someone get this code in Coldfusion lingo...
PHP code as follows:

<�

// set this to your e-mail address:
$email = "YOUR(a)email.com";


function doTheCurl ()
{
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$ch = curl_init();


curl_setopt($ch, CURLOPT_URL,
"https://www.sandbox.paypal.com/cgi-bin/webscr");



curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = curl_exec ($ch);
curl_close($ch);
return $fp;
}

function doTheHttp ()
{
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$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";

// check to see if this is sandbox or not.
if ($_POST["test_ipn"] == 1)
{
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
}
else
{
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
}

if (!$fp) {
return "ERROR";
}
else
{
fputs ($fp, $header . $req);
while (!feof($fp))
{
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0)
{
return "VERIFIED";
}
else if (strcmp ($res, "INVALID") == 0)
{
return "INVALID";
}
}
fclose ($fp);
}
return "ERROR";
}

function doEmail ($title,$email)
{
$alsomail = "$title\n\n----HTTP POST VARS----\n\n";
foreach ($_POST as $key => $value)
{
$alsomail .= "$key = $value \n\n";
}
foreach($_GET as $kee => $val)
{
$gets .= "$kee = $val \n\n";
}

mail($email,$title,$alsomail . "\n\n get values \n\n" . $gets);
}

/*
now with both of those functions defined, you can run
a simple check to get back your responce:
*/

$fp = doTheCurl();
if (!$fp)
{
// doEmail("cURL ERROR, SWITCHING TO HTTP",$email);
$fp = doTheHttp();
}
$res = $fp;

/*
and after that, you can check to see if $res is
invalid or verified
*/

if (strcmp ($res, "VERIFIED") == 0)
{
/*
log stuff into your database here!

Also, it's a good idea to send yourself an e-mail with all _POST
params, just so you know what's going on. The e-mail code is here,
the database code is not.
*/
doEmail("PayPal Purchase Success",$email);
}
else if (strcmp ($res, "INVALID") == 0)
{
/*
here i send myself an e-mail saying there's been an
invalid with all of the details sent to me from ipn.
that way i have a record of it.
*/
doEmail("INVALID PayPal Purchase!!",$email);
}
else
{
/*
here i send myself an e-mail saying there has been a
fatal error - if the paypal server goes down this will
happen� doesn't seem likely but it's always good to have
some backup just incase. I also place all data sent
from IPN in this e-mail so I have record of it.

This can also happen with an HTTP error - just a note
*/
doEmail("Error in IPN Code - Normally HTTP Error",$email);
}
�>