Reputation: 17
I have asked my paypal api to send email of $res to see what the response is. Inside of the while (!feof($fp)) {}
This is what i'm getting from $res to my email
HTTP/1.0 400 Bad Request
Server: BigIP
Connection: close
Content-Length: 19
Invalid Host header
This is my paypal code.
$req = 'cmd=_notify-validate';
$req .= payment_safe_check ($_POST);
$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";
$mode = strtolower($paymod_data['PAYMENT_MODE']);
mail("to email", "subject",serialize($_POST) );
if ($mode == 'test')
{
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
}
else
{
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
}
if (!$fp)
{
// HTTP ERROR
die ("Error");
}
else
{
// NO HTTP ERROR
fputs ( $fp, $header . $req );
while (!feof($fp))
{
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0)
{
processing ( $_POST );
}
else if (strcmp ($res, "INVALID") == 0)
{
}
}
fclose ($fp);
}
This doesn't work if (strcmp ($res, "VERIFIED") == 0){}
but if I change it to if (strcmp ($res, "VERIFIED") == -1){}
It works fine.
UPDATE
I have change my code to
$mode = $paymod_data['PAYMENT_MODE'];
$paypal_url = ($mode == 'test') ? 'www.sandbox.paypal.com' : 'www.paypal.com';
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval)
{
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
$req .= payment_safe_check ($_REQUEST);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$paypal_url.'/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: '.$paypal_url));
$res = curl_exec($ch);
curl_close($ch);
if (strcmp ($res, "VERIFIED") == 0) {
processing ( $_POST );
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
Can someone please confirm if they is any problem with this code.
Upvotes: 0
Views: 2616
Reputation: 43
the problem in the url could not be found ... you have to change the url to be like the following ...
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); // (for Sandbox IPNs)
$ch = curl_init(' https://www.paypal.com/cgi-bin/webscr'); // (for live IPNs)
Upvotes: 0
Reputation: 902
VERIFIED and INVALID are the two responses that you need to validate once you get the response back from PAYPAL.
You can refer the code samples at
https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623
Upvotes: 1
Reputation: 2337
strcmp check to see if one string is "larger" than the other. Is is case sensitive and can be thrown off by spaces and line feeds.
I would recommend you instead use
if ( stristr($res,"VERIFIED") === 0 ) {}
else if ( stristr($res,"INVALID") === 0 ) {}
Upvotes: 0