Welcome Guest, Not a member yet? Register   Sign In
paypal invalid ipn
#11

[eluser]spheroid[/eluser]
Code:
In my experience the PayPal sandbox does not send back an IPN.

@CI Lee:

The Paypal Sandbox is extremely slow, and people have complained of that. But it does work.

@edhrx:

Try this:

Code:
function ipn()
    {        
        // Paypal variables list for IPN
        // https://www.paypal.com/IntegrationCenter/ic_ipn-pdt-variable-reference.html

        // For this example, we'll just email ourselves ALL the data.
        $to    = 'YOUREMAILHERE';    //  your email

        if ($this->paypal_lib->validate_ipn())
        {
            $data['order_detail'] = 'An instant payment notification was successfully received from ';
            $data['order_detail'] .= $this->paypal_lib->ipn_data['payer_email'] . ' on '.date('m/d/Y') . ' at ' . date('g:i A') . "<br /><br />";
            $data['order_detail'] .= " Details:<br /><br />";

            ksort($this->paypal_lib->ipn_data);
            
            foreach ($this->paypal_lib->ipn_data as $key=>$value)
            {
                $data['order_detail'] .= "$key: $value<br />";
            }        
            
            $data['order_detail'] .= "<br /><br />End of Paypal IPN Information recevied.";
            
            // load email lib and email results
            $this->load->library('email');
            $this->email->to($to);
            $this->email->from('EMAILADDRESS', 'Email Address Description');
            $this->email->subject('My Store Test - IPN SUCCESS');
            $message = $this->load->view('ORDER_VIEW_TEMPLATE', $data, TRUE);
            $this->email->message($message);    
            $this->email->send();            
        }
        else
        {
            $data['order_detail'] = 'An instant payment notification was NOT successfully received from ';
            $data['order_detail'] .= $this->paypal_lib->ipn_data['payer_email'] . ' on '.date('m/d/Y') . ' at ' . date('g:i A') . "<br /><br />";
    
            // load email lib and email results
            $this->load->library('email');
            $this->email->to($to);
            $this->email->from('EMAILADDRESS', 'Email Address Description');
            $this->email->subject('My Store Test - IPN FAILED');
            $message = $this->load->view('ORDER_VIEW_TEMPLATE', $data, TRUE);
            $this->email->message($message);    
            $this->email->send();
        }        
    }

You'll need to fill in your email address and pick a file location for the ORDER_VIEW_TEMPLATE (and a filename). For the ORDER_VIEW_TEMPLATE try:

Code:
&lt;?php echo $order_detail; ?&gt;

The library function should be:

Code:
function validate_ipn()
    {
        // parse the paypal URL
        $url_parsed = parse_url($this->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 (isset($_POST))
        {
            foreach ($_POST as $field=>$value)
            {
                $this->ipn_data[$field] = $value;
                $post_string .= $field.'='.urlencode(stripslashes(str_replace("\n", "\r\n", $value))).'&';
            }
        }
        
        $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->last_error = "fsockopen error no. $errnum: $errstr";
            $this->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->ipn_response .= fgets($fp, 1024);

            fclose($fp); // close connection
        }

        if (eregi("VERIFIED",$this->ipn_response))
        {
            // Valid IPN transaction.
            $this->log_ipn_results(true);
            return true;        
        }
        else
        {
            // Invalid IPN transaction.  Check the log for details.
            $this->last_error = 'IPN Validation Failed.';
            $this->log_ipn_results(false);    
            return false;
        }
    }
#12

[eluser]edhrx[/eluser]
spheroid,

Just got round to implementing your code snippet, works well , many thanks

Ed..
#13

[eluser]spheroid[/eluser]
@edhrx,
Glad I could help!
#14

[eluser]ngkong[/eluser]
awesome

thx spheroid your fix works perfect!
#15

[eluser]jtotheb[/eluser]
@spheroid, could you tell me why the following bit of code is required and what it is doing with paypal please?

Code:
$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->last_error = "fsockopen error no. $errnum: $errstr";
            $this->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->ipn_response .= fgets($fp, 1024);

            fclose($fp); // close connection
        }

        if (eregi("VERIFIED",$this->ipn_response))
        {
#16

[eluser]spheroid[/eluser]
In a nutshell, it connects to Paypal to receive the IPN data, whether it passed or not. If it passed, it puts the IPN fields in an array to parse later. Then you could store the IPN results for your cart, subscription, etc. in your database for instance.
#17

[eluser]jtotheb[/eluser]
In the paypal lib in the wiki, you send paypal a notify address and when the payment status is changed, paypal hits that notify address and gives you the status.

If paypal contacts you when the payment status changes, why would you need to contact paypal yourself? I'm just trying to understand in case i've implemented it incorrectly or if there is something else i need to factor into using paypal.

Thanks!
#18

[eluser]spheroid[/eluser]
Let's say you have a controller "store.php". Within the function "index()", you put together the Paypal form to display in the browser which contains:

Code:
$this->paypal_lib->add_field('notify_url', site_url('store/ipn'));

Once the user is sent to Paypal and completes his info, Paypal returns to the URL at your site like http://mysite.dev/store/ipn. When using the sandbox look at the URL returned in your browser (you'll likely see extra variables it posts back to your "notify_url".

In your store.php controller, your function "ipn()" has the line:

Code:
if ($this->paypal_lib->validate_ipn())

So it goes to the library function "validate_ipn" and then reads the response received from Paypal (i.e. the extra variables in the URL) or from the $_POST values. Does this make sense?
#19

[eluser]jtotheb[/eluser]
Yes i understand that part, what i was confused by was the "fputs" part. Is the validate_ipn function used for sending the variables to paypal as well as receiving them?
#20

[eluser]spheroid[/eluser]
The variables are sent to Paypal from your controller function that creates the form itself. When the form is submitted is when the initial variables are sent to Paypal. The validate_ipn function receives data back from Paypal once the user is returned back to your site at (example) http://mysite.dev/ipn. That function will also receive any other data at any other time related to that transaction. One example is that if you set up a recurring payment (i.e. $10 every month), Paypal will send to that URL an update each month that a transaction was processed.




Theme © iAndrew 2016 - Forum software by © MyBB