title: Codeigniter paypal_lib IPN not working
body: <p>I am trying to capture when the user made a payment in paypal(sandbox). IPN was the answer for this but it seems I am not getting any response from it. I tried to put it in a log and even the DB but there are no data entering. I know IPN is a listener but I dunno why isnt working.. Any clarification and thoughts about this?</p>
  
  <p>I am using codeigniter <a href="https://github.com/ollierattue/codeigniter-paypal/blob/master/libraries/Paypal_Lib.php" rel="nofollow">Paypal_Lib</a></p>
  
  <p>Here are my script:</p>
  
  <p>First I declared the fields</p>
  
  <pre><code>$this-&gt;paypal_lib-&gt;add_field('cmd','_cart');
  $this-&gt;paypal_lib-&gt;add_field('upload','1');
  $this-&gt;paypal_lib-&gt;add_field('business', $business_email);
  $this-&gt;paypal_lib-&gt;add_field('return', site_url('invoice/payment_success/'.$invoice_id.'/'.$album_id));
  $this-&gt;paypal_lib-&gt;add_field('cancel_return', site_url('invoice/payment_cancel/'.$invoice_id));
  $this-&gt;paypal_lib-&gt;add_field('notify_url', site_url('invoice/payment_validate/'.$invoice_id)); // &lt;-- IPN url
  $this-&gt;paypal_lib-&gt;add_field('custom', $invoice_id); // &lt;-- Verify return
  </code></pre>
  
  <p>Here is for my IPN , just checking if it works by having a log but no luck.</p>
  
  <pre><code>public function payment_validate(){
          //$this-&gt;paypal_lib-&gt;dump();
          $invoice_id = $this-&gt;uri-&gt;segment(3);
  
          if ($this-&gt;paypal_lib-&gt;validate_ipn()){
  
              $payer_email = $this-&gt;paypal_lib-&gt;ipn_data['payer_email'];
  
  
               $fp=fopen('temp/logs/paypal_ipn.log','a');
                  fwrite($fp, $this-&gt;paypal_lib-&gt;ipn_data  . "\n");
  
                  fclose($fp);  // close file
  
          }
  
      }
  </code></pre>
  
  <p>And here is the validate_ipn in the library.</p>
  
  <p>function validate_ipn()
  {</p>
  
  <pre><code>// get instance
  $CI =&amp; get_instance();
  
  // parse the paypal URL
  $url_parsed = parse_url($this-&gt;paypal_url);
  
  // generate the post string from the _POST vars aswell as load the
  // _POST vars into an arry so we can play with them from the calling
  // script.
  $post_string = '';
  #if(count($_POST))
  if($_POST)
  {
      #foreach (array_keys($_POST) as $field)
      foreach ($_POST as $field =&gt; $value)
      {
          #$value = $CI-&gt;input-&gt;post($field, true);
          $this-&gt;ipn_data[$field] = $value;
          $post_string .= $field.'='.urlencode(stripslashes($value)).'&amp;';
      }
  }
  
  $post_string.='cmd=_notify-validate'; // append ipn command
  
  // open the connection to paypal
  $fp = fsockopen($url_parsed['host'],'80',$err_num,$err_str,30);
  if(!$fp)
  {
      // could not open the connection.  If loggin is on, the error message
      // will be in the log.
      $this-&gt;last_error = 'fsockopen error no. '.$err_num.': '.$err_str;
      $this-&gt;log_ipn_results(false);
      return false;
  }
  else
  {
      // Post the data back to paypal
      fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
      fputs($fp, "Host: $url_parsed[host]\r\n");
      fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
      fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
      fputs($fp, "Connection: close\r\n\r\n");
      fputs($fp, $post_string . "\r\n\r\n");
  
      // loop through the response from the server and append to variable
      while(!feof($fp))
          $this-&gt;ipn_response .= fgets($fp, 1024);
  
      fclose($fp); // close connection
  }
  
  
  if (eregi('VERIFIED',$this-&gt;ipn_response))
  {
      // Valid IPN transaction.
      if ($this-&gt;ipn_log_method == 'db')
      {
          return $this-&gt;log_ipn_results(true);
      }
  
  
      /*// Valid IPN transaction.
      $this-&gt;log_ipn_results(true);*/
      return true;
  }
  else
  {
      // Invalid IPN transaction.  Check the log for details.
      $this-&gt;last_error = 'IPN Validation Failed.';
      $this-&gt;log_ipn_results(false);
      return false;
  }
  </code></pre>
  
  <p>}</p>