PayPal IPN: HTTP/1.1 400 Bad Request -


i know has been asked times, none of solutions worked me. using instant payment notification (ipn) simulator paypal (https://developer.paypal.com/webapps/developer/applications/ipn_simulator) receive: unexpected response paypal: http/1.1 400 bad request

<?php     //open socket acknowledgement request         $fp = fsockopen (ssl://www.sandbox.paypal.com, 443, $errno, $errstr, 30);          if (!$fp) {              // fsockopen error             throw new exception("an error occured while using fsockopen(): [$errno] $errstr");         }           //set acknowledgement request headers         $header .= "post /cgi-bin/webscr http/1.1\r\n";         $header .= "host: ssl://www.sandbox.paypal.com\r\n";         $header .= "content-type: application/x-www-form-urlencoded\r\n";         $header .= "content-length: ".strlen($data)."\r\n";         $header .= "connection: close\r\n\r\n";          // post request paypal validation         fputs ($fp, $header . $data);         while (!feof($fp)) {                     // while not eof             $res = fgets ($fp, 1024);              // acknowledgement response             if (strcmp ($res, "verified") == 0)  {  // response verified                 $response = 'verified';             }             else if (strcmp ($res, "invalid") == 0)  { // response invalid                 $response = 'invalid';             }              else {                 throw new exception("unexpected response paypal: $res");             }         } 

edit: after changing

$header .= "host: ssl://www.sandbox.paypal.com\r\n"; 

to

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

i receive http/1.1 200 ok response. problem is, if() part not working, jumps last

 else {                 throw new exception("unexpected response paypal: $res");   } 

is there wrong if (strcmp ($res, "verified") == 0) ?

update 2: here full code. maybe can find mistakes:

class paypal {     protected $sandbox = false;     protected $data = null;      const sandbox_url = 'www.sandbox.paypal.com';     const paypal_url = 'www.paypal.com';       public function __construct($sandbox = false) {          $this->sandbox = $sandbox;     }      public function receivedata() {         if (empty($_post)) {             throw new exception('no $_post data found');         }         $this->data = $_post;         // read notification paypal , create acknowledgement response         $req = 'cmd=_notify-validate';               // add 'cmd' beginning of acknowledgement send paypal          foreach ($_post $key => $value) {         // loop through notification nv pairs             $value = urlencode(stripslashes($value));  // encode values             $req .= "&$key=$value";                    // add nv pairs acknowledgement         }          if ($this->fsock($req))             return true;         else             return false;     }      public function getdata() {         return $this->data;     }      private function fsock($data) {         //open socket acknowledgement request         $fp = fsockopen ('ssl://'.$this->geturl(), 443, $errno, $errstr, 30);          if (!$fp) {              // fsockopen error             throw new exception("an error occured while using fsockopen(): [$errno] $errstr");         }           //set acknowledgement request headers         $header .= "post /cgi-bin/webscr http/1.1\r\n";         $header .= "host: ".$this->geturl()."\r\n";         $header .= "content-type: application/x-www-form-urlencoded\r\n";         $header .= "content-length: ".strlen($data)."\r\n";         $header .= "connection: close\r\n\r\n";          // post request paypal validation         fputs ($fp, $header . $data);         while (!feof($fp)) {                     // while not eof             $res = fgets ($fp, 1024);              // acknowledgement response             if (strcmp ($res, "verified") == 0)  {  // response verified                 $response = 'verified';                 // notification protocol complete, ok process notification contents                  // possible processing steps payment might include following:                  // check payment_status completed                 // check receiver_email primary paypal email                 // check payment_amount/payment_currency correct                 // process payment             }             else if (strcmp ($res, "invalid") == 0)  { // response invalid                 $response = 'invalid';             }              else {                 throw new exception("unexpected response paypal: $res");             }         }         fclose ($fp);  //close file pointer          if ($response == 'verified')             return true;         else             return false;     }      private function geturl() {         if ($this->sandbox)              return self::sandbox_url;         else                             return self::paypal_url;     }    }  $paypal = new paypal(true); try {     if ($paypal->receivedata()) {         // success     }     else {         // fail     } } catch (exception $e) {     // exception     echo 'exception: ',  $e->getmessage(), "\n"; } 

there seems number of issues script. example, $data not initialized posting nothing. reason getting 400 error becase $header .= "host: ssl://www.sandbox.paypal.com\r\n";

should

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


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -