paypal IPN not returning VALID or INVALID

madein83
Contributor
Contributor

Hi,

 

Im implementing a paypal IPn script on a new website and im basically using one that i already have working for another customer on a different server.

 

But on both sandbox test payments and also live payments i cant get a valid or invalid response.

 

After the IPN script runs im emailing myself the $res and $req variables.

 

Im not an expert at this but from the documentation etc the string is supposed to contain either the word VALID or INVALID.

 

When i look through both $resand $req they are nowhere.

 

As i say the script runs fine and reports valid on another server just not this new one.

 

i've tried 15+ possible fixes but some report to work for some but not others...i have also tried live payments and tried re-sending the IPjn notification but am geting nowhere.

 

Here is a simplified version of my script;

 

// read the post from PayPal system and add 'cmd'
$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.1\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.paypal.com', 443, $errno, $errstr, 30);

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

//update order and send emails etc

}

// if the IPN POST was 'INVALID'...do this

else if (strcmp(trim($res), "INVALID") == 0) {

// log and send email for manaul investigation

}else{
$newres=$newres.$res;
} }
//email myself $newres for review }

 

Login to Me Too
5 REPLIES 5

PayPal_david
Moderator
Moderator

Hi there Madein83,

 

Welcome to the Community!

 

I don't have much knowledge in regards to website integration and IPN's. If you try going to our developer website at www.x.com or directly to our technical team on www.paypal.com/mts, they should be able to assist you further with the issues that you are currently experiencing.

 

Please let me know how it goes.

 

David.

Login to Me Too

madein83
Contributor
Contributor

Hi,

 

I also submitted a support request on the link you suggested but havent had a response.

 

Im wondering about the first LIVE payment i tested which was an actual payment using a real paypal account.....when i log into the paypal account and check IPN history and view the IPN message...in the message it doesn't say anything about VERIFIED or INVALID.....am i wrong in assuming that the VERIFIED or INVALID is posted with this message?

 

I just re-ran a test on the sandbox mode and managed to get VERIFIED but when i tried to re-send the LIVE IPN message i didnt get an email at all which suggests that it didnt return VERIFIED OR INVALID?

 

 

Login to Me Too

madein83
Contributor
Contributor

Hi,

 

I've rang support and was told that theres about a 3 week response time with MTS....im sorry but i cant wait 3 weeks to hear a response on a technical question.....im getting closer through trial and error so if some one could answer me on this questions i would greatly appreciate it.

 

I am emailing myself the $res variable after the strcmp function checks for VERIFIED or INVALID.....but each time it loops through i am also putting $res into a new variable like so $newres=$newres.$res along with the strcmp result.

 

Now my email containts the following.

 

Res: VERIFIED 

 New Res: HTTP/1.0 200 OK
(-14)
X-Frame-Options: SAMEORIGIN
(2)
Strict-Transport-Security: max-age=14400
(-3)
Content-Type: text/html; charset=UTF-8
(-19)
Date: Mon, 26 Nov 2012 11:46:29 GMT
(-18)
Content-Length: 8
(-19)
Connection: close
(-19)
Set-Cookie: cwrClyrK4LoCV1fydGbAxiNL6iG=SQms2OmWjtZJgiXRa7nbgPf0NvIT_OaaVjgHZoc_dpkvUXsC9_lIgxEsc52KoCURQU9rtVCXRyyDpJL-Bfjn4iu3puouvJuVPbCEPVNHQ1TsxPm8qqL7Z7xyJ5HykYS_aYaOAW%7c_6Tmgke76Nr4yHIVQG8vY-cl75QIh9Px_Fdj2-hfdHejAHBCSW0JBmIqkdv87a9d99Uatm%7cuv98AlMvEZ1yh4zrP-ivzJTrNYJJiR6-CylO7gJpFY6cJYc1Ei00KwJm1mGt7yFuMm0AM0%7c1353930389; domain=.paypal.com; path=/; Secure; HttpOnly
(-3)
Set-Cookie: cookie_check=yes; expires=Thu, 24-Nov-2022 11:46:29 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
(-3)
Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly
(-3)
Set-Cookie: navlns=0.0; expires=Sun, 21-Nov-2032 11:46:29 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
(-3)
Set-Cookie: TSd78272=8e29c5728b513f4ed1998b625ef093ea9e546f2e28af491a50b35694a0e24589ef5956ad634137d16081497ffaa6df6385ae782bba64fcf83ecccf34b4608ba71e873c42; Path=/
(-3)
Set-Cookie: aksession=1353930689~id=cookieC4Bd+Hk8wStNxlCAhTxA+GFDewf91zzC33Zq337NzCiG6M8pq0f0ezcV+IO6ysBLznl6r7v9v3To4F4Y+vE0lhq2vVrTkzKyRYWlHm3bMvvrAtDkb+e5TluoPCH9iiAo; expires=Mon, 26-Nov-2012 11:51:29 GMT; path=/; domain=.paypal.com
(-3)

(-73)

 So $res is VERIFIED but when i do strcmp and add $res to a new variable it seems to be missing.

 

the reason i appended $res to $newres with the result of the strcmp was i thought the strcmp wasn't returning 0 as it should have been but as its not returning VERIFIED in my $newres so i cant check it.

 

Please help!!

Login to Me Too

PayPal_paula
Moderator
Moderator

Hi madein83, 

 

Apologies in advance for the long response Smiley Sad

 

The way an IPN works is broken into 2 steps:

first an IPN message will be received by your script, the script then sends this same IPN back to PayPal to see if it actually came from PayPal or not;

PayPal will respond with either “VERIFIED” or “INVALID”;

 

So looking at your script:

when the IPN listener receives the first message, it assigns it into $req;

then it sends this message back to PayPal and receives another message (either “VERIFIED” or “INVALID”) and places that into the variable $res.

 

So far so good!

After this point any processing should be done using the first variable that you assigned the POST variables to $req; (the $res variable may not contain the values that you want)

 

One thing to take note of is that you must be sure that you are sending the original IPN POST message to the correct end point, i.e. if you are swopping between working in sandbox/Live, you will have to change 2 parts of your script the $header and the $fp endpoint.

 

Here is a full working script for sandbox that will email you back the IPN message and whether it is verified or not, the only thing that you need to change are both of the $mail_To variables (to your own email address)

 

<?php

 

// read the post from PayPal system and add 'cmd'

$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";

 

// If testing on Sandbox use:

$header .= "Host: www.sandbox.paypal.com:443\r\n";

// $header .= "Host: www.paypal.com:443\r\n";

 

$header .= "Content-Type: application/x-www-form-urlencoded\r\n";

$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

 

                // If testing on Sandbox use:

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

 

 

if (!$fp) {

// HTTP ERROR

} else {

                fputs ($fp, $header . $req);

                while (!feof($fp)) {

                                $res = fgets ($fp, 1024);

                                if (strcmp ($res, "VERIFIED") == 0) {

                                                // 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

 

                                                $mail_From = "From: sandbox@pptest.com";

                                                $mail_To = "YourEmailAddress@YourDomain.com";

                                                $mail_Subject = "VERIFIED IPN";

                                                $mail_Body = $req;

 

                                                foreach ($_POST as $key => $value){

                                                                $emailtext .= $key . " = " .$value ."\n\n";

                                                }

 

                                                mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From);

 

                                }

                                else if (strcmp ($res, "INVALID") == 0) {

                                                // log for manual investigation

 

                                                $mail_From = "From: sandbox@pptest.com";

                                                $mail_To = "YourEmailAddress@YourDomain.com";

                                                $mail_Subject = "INVALID IPN";

                                                $mail_Body = $req;

 

                                                foreach ($_POST as $key => $value){

                                                                $emailtext .= $key . " = " .$value ."\n\n";

                                                }

 

                                                mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From);

 

                                }

                }

fclose ($fp);

}

?>

 

 

Try out the above script for testing, making note of two things:

Remember to change the 2 places in the script if you are moving from sandbox to live;

Don’t forget to have a look in your junk mail folder as IPN messages received often end up there!

 

Please let me know if this helps 

Paula 

 

 

Login to Me Too

madein83
Contributor
Contributor

Hi, i think i may have it sorted, just checking now.

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.