paypal ipn not working, send to failed.php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: paypal ipn not working, send to failed.php

Post by Eric! »

I'm sorry I don't have time to go through all that code. I do see a couple of things.

First if I remember right the IPN verification string you're reading contains a lot of data along with the word "VERIFIED" or "INVALID". However you're using strcmp to test this long string for one substring. Strcmp returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. I don't think the $res string will just have the single word "VERIFIED" or "INVALID" but I'm going from memory, you'll want to check that.

Try using preg_match instead:

Code: Select all

if (preg_match("/VERIFIED/",$res) === 1) {
            echo "PAYPAL VERIFIED IPN DATA!\n";
}
else echo "Invalid IPN\n";
Also you should be posting back to PayPal with http 1.1 as they are stopping support for 1.0 soon. Change to HTTP/1.1 and add the host line with the paypal_url.

Code: Select all

$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: " . $paypal_url . "\r\n";
$header .= "Connection: close\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
I might have more time to look at this further tomorrow.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: paypal ipn not working, send to failed.php

Post by jonnyfortis »

Hi Thanks for that, i will try your suggestions and post the results
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: paypal ipn not working, send to failed.php

Post by jonnyfortis »

if (preg_match("/VERIFIED/",$res) === 1) {
echo "PAYPAL VERIFIED IPN DATA!\n";
}
else echo "Invalid IPN\n";
i am rather hesitant in using this new code (preg_match) as will the make a difference to what i have already done?

also i have change over to 1.1 as suggested
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: paypal ipn not working, send to failed.php

Post by Eric! »

jonnyfortis wrote:i am rather hesitant in using this new code (preg_match) as will the make a difference to what i have already done?
strcmp won't work the way you want it to because $res is a string that contains a lot of other characters than what you are searching for. preg_match will search the full string $res for your search phrase.

Check the manual to see what it does, because I don't understand your concern.
http://php.net/manual/en/function.strcmp.php
vs
http://php.net/manual/en/function.preg-match.php
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: paypal ipn not working, send to failed.php

Post by jonnyfortis »

OK I will try it, I was a bit concerned as it was script that was pre written so presumed it would work.

thanks for your time so far
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: paypal ipn not working, send to failed.php

Post by Eric! »

Sorry I haven't had time to look through this in more detail. I have some time now. Since you're still having problems with this and you haven't checked the contents of $res. I tested IPN with a bad verification to see what came back because my memory is faulty:

https://www.paypal.com/cgi-bin/webscr?c ... y-validate

And unlike what I remember the response is a single word "INVALID". So I decided to read the instructions (which I suggest you do too) and found a successful verification produces only the word "VERIFIED". Thus strcmp will work. It's not robust, but it should work. This means the problem is elsewhere.

Since you're saying "it seems nothing is in the database" I think you should look at that code. Also you should confirm that nothing is happening in your database.

I don't quite understand your database code. On the surface it appears that when a new transaction comes in, you try to select a paypal transaction id. When was this ID put into the database because it has to come from PayPal? Does this code in notify.php return any results?

Code: Select all

mysql_select_db($database_lachic, $lachic); // find Txn ID
$query_rsTxnId = "SELECT transactID FROM lachic_Orders WHERE transactID = '" . $txnID . "'";
$rsTxnId = mysql_query($query_rsTxnId, $lachic) or die(mysql_error());
$row_rsTxnId = mysql_fetch_assoc($rsTxnId);
$totalRows_rsTxnId = mysql_num_rows($rsTxnId);
As pseudo code you should be doing something like this:

*(optional) shopping cart can preconfigure database with unverified/unpaid order -- but it can not know the paypal transaction id
*paypal calls your IPN listener (notify.php) with $_POST data
*your code verifies $_POST data
*if valid add purchase information into database (using INSERT, or update to the shopping cart's initial information)
*Let the user know the status of their purchase (via email and/or return to merchant link like success.php which waits for the database to show successful)
*Perform any tasks relevant to their purchase

Also I'm not sure if PayPal guarantees the IPN will be fully processed before the user can click "Return to Vendor". This could cause a problem if success.php tries to access the database before the IPN data is complete. I've never tried to use the IPN this way. I did a search and couldn't find any information on that subject.

Is your shopping cart trying to preconfigure the order? Where did you get the code for using the XCart? It's possible there's a problem with how you've configured that to work with your database too.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: paypal ipn not working, send to failed.php

Post by jonnyfortis »

ok thanks i am still trying to sort this and thanks for the suggestions, will post the results
Post Reply