Link to home
Start Free TrialLog in
Avatar of marcodalzotto
marcodalzottoFlag for Italy

asked on

Paypal IPN: INVALID

Hi experts, I'm trying to use the IPN Paypal system.
I've tryed different code... but always the same answer: INVALID.

The strange thing is that the PayPal manual say that Paypal pass the parameters in POST... but I get anything in POST, I get data only in GET.

Here is my code:

<?php

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_GET 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 .= "Host: www.sandbox.paypal.com\r\n"; //<<<<<<<<<<<< ADD THIS LINE
$header .= "Referer: ".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].@$_SERVER['QUERY_STRING']."\r\n";
$header .= "Server: ".$_SERVER['SERVER_SOFTWARE']."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Accept: */*\r\n\r\n";

$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);

if (!$fp) {
      echo "PHP fsockopen() error: " . $errstr;
}
else {
      fputs ($fp, $header . $req);
      while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            echo "RECEIVED: ".$res."<BR>"; // for debug
            if (strcmp ($res, "VERIFIED") == 0) {
                  echo "VERIFIED";
            }
            else if (strcmp ($res, "INVALID") == 0) {
                  echo "INVALID";
            }
      }
}

?>


Here the answer:

cmd=_notify-validate&tx=9BL62873GK243402G&st=Completed&amt=100.00&cc=EUR&cm=&sig=rD%2fvVjLNDwD3dAm9sv9GPPiTyAvtYm4fcqJBqXhwWoV0Y1nyEkWDRsuJg2102NpBwPjDp0sARhPOF446Z6hfW8xzv3S%2f1ILdF1v964dm0M%2fCXItrhJI2RsD7d63EgMeVPRn8TZ4ndlrmEtCBvtLT8QOlViCPOoPCwgGhXd9I%2bM8%3dRECEIVED: HTTP/1.0 200 OK
RECEIVED: Date: Mon, 19 Jun 2006 16:53:49 GMT
RECEIVED: Server: Apache/1.3.27 (Unix) mod_ssl/2.8.12 OpenSSL/0.9.7a PHP/4.3.2
RECEIVED: Set-Cookie: cookie_check=yes; expires=Thu, 16-Jun-2016 16:53:49 GMT; path=/; domain=.paypal.com
RECEIVED: Set-Cookie: Apache=64.202.165.201.153601150736029465; path=/; expires=Wed, 11-Jun-36 16:53:49 GMT
RECEIVED: Content-Type: text/html; charset=UTF-8
RECEIVED: X-Cache: MISS from wc04.inet.mesa1.secureserver.net
RECEIVED: Connection: close
RECEIVED:
RECEIVED: INVALID


Thanks
Avatar of DataSmarts
DataSmarts

I've worked with Paypal before, and I understand mostly what you are trying to do, but can you provide a short two sentence narrative of what you are wanting to do?

Are you wanting to get the confirmation id from Paypal and add that to your own database?
Avatar of marcodalzotto

ASKER

The answer I've posted is a paramateter passed by Paypal to my ok.php when the payment transaction is completed.
The code I've posted connects to Paypal and repeat the parameters as received, adding 'cmd=_notify-validate'.
As a result I shoud get VERIFIED.

Yes I want to get a confirmation by paypal that the transaction really occured so I can permit the downalod of my product.
ASKER CERTIFIED SOLUTION
Avatar of DataSmarts
DataSmarts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well done, DataSmarts.

I'm going to accept your answer, and I'm going to give you a grade of 'A' if you tel me the difference between:

     $req = 'cmd=_notify-validate';

and

     $req = 'cmd=_notify-synch';

And why I couldn't use 'cmd=_notify-validate' that is present in a lot of example about how to receive IPN notification from Paypal?

Thanks
Well to be technical, I used the PDT instead of IPN as IPN has routinely had problems with the way they are sending the transaction confirmations.

As you may be aware, IPN provides a means where the user could, feasibly, close the browser and still receive the confirmation required to download your product.  PDT requires the user to got hrough the process of completing their transaction and then auto_return would direct them back to your site.  You also need a Cancel_return to be set so they are directed to your site where they can determine IF they want to continue or not...