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

[eluser]edhrx[/eluser]
Hi,
I am using the paypal lib from the wiki. I can go to paypal and come back to a success page , however the logfile is always showing an INVALID ipn. Looking at the code in the library the log file should be displaying some of the $_post vars but it is'nt and so I am finding it hard to debug what is going on here. I have read most of the previous posts in relation to this type of thing and implemented some small suggested code changes in relation to line endings.. but to no effect

best wishes.
#2

[eluser]spheroid[/eluser]
What code are you trying out? Are you testing using Paypal's Sandbox?
#3

[eluser]edhrx[/eluser]
spheroid
Yes I,m playing in the sandbox. All the posts to paypal and return urls etc all work and the function ipn gets called in the lib..but is consistently shown to be invalid


Ed.
#4

[eluser]spheroid[/eluser]
Please post your controller/view, etc code. I've used the paypal lib successfully for both shopping cart + subscription payments.
#5

[eluser]edhrx[/eluser]
spheroid
for testing i am just firing of this auto_form
function auto_form()
{
$this->paypal_lib->add_field('business', '[email protected]');
$this->paypal_lib->add_field('return', base_url().'index.php/paypal/success');
$this->paypal_lib->add_field('cancel_return', base_url().'index.php/paypal/cancel');
$this->paypal_lib->add_field('notify_url', base_url().'index.php/paypal/ipn'); // <-- IPN url
$this->paypal_lib->add_field('custom', '12345fddf67890'); // <-- Verify return

$this->paypal_lib->add_field('item_name', 'Paypal Test Transaction');
$this->paypal_lib->add_field('item_number', '6942fgfddfsd7');
$this->paypal_lib->add_field('amount', '200');

$this->paypal_lib->paypal_auto_form();


In the validate_ipn of the lib is this

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)
{ // str_replace("\n", "\r\n", $value)
// put line feeds back to CR+LF as that's how PayPal sends them out
// otherwise multi-line data will be rejected as INVALID

$value = str_replace("\n", "\r\n", $value);
$this->ipn_data[$field] = $value;
$post_string .= $field.'='.urlencode(stripslashes($value)).'&';

}
}
/*
if ($this->CI->input->post())
{
foreach ($this->CI->input->post() as $field=>$value)
{
$this->ipn_data[$field] = $value;
$post_string .= $field.'='.urlencode(stripslashes($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;
}
}


}



You can see where I have made changes to the post_string in response to earlier topics on the subject


Ed
#6

[eluser]spheroid[/eluser]
You have this in your form:

Code:
$this->paypal_lib->add_field(’notify_url’, base_url().’index.php/paypal/ipn’); // <-- IPN url

...but your function is actually called
Code:
function validate_ipn()

Try changing your form line to:

Code:
$this->paypal_lib->add_field(’notify_url’, base_url().’index.php/paypal/validate_ipn’); // <-- IPN url
#7

[eluser]edhrx[/eluser]
sorry should have put more in the text

ipn is in the controller which then calls validate_ipn in the lib like this
if ($this->paypal_lib->validate_ipn())
{
etc etc..
}



Ed.
#8

[eluser]CI Lee[/eluser]
In my experience the PayPal sandbox does not send back an IPN.
Just test with $0.01 transactions and refund the amount later to avoid any fees.

Testing this way also ensures that your app will work in the real world.

-Lee
#9

[eluser]edhrx[/eluser]
the strange thing is I have wrapped some debug in the ipn in the controller
and at the top of the function lib function validate_ipn and looks like the validate_ipn does not get called called. It should as the load of library is one of the first calls in the controller



Ed
#10

[eluser]edhrx[/eluser]
Hi..
I guess that it was to do with how the library was being loaded. Whilst I know it is'nt the CI way I dumped all the library functions / variables into the same controller and it worked with no problems at all.

I'm on a deadline with this site, an no doubt I will go back to the issue resolve it in a more satisfactory manner.

Ed.




Theme © iAndrew 2016 - Forum software by © MyBB