Paypal IPN blues... in php

17 replies
Hello all, I have been working on a clients project for about a week now. I have it hosted on my server so that I may be able to have easy access to it. I have everything else requested for the project done EXCEPT for one thing that is getting on my last nerve. That is integrating the paypal IPN into the project. I have the button installed, the success, fail and ipn scripts as well as registration page set up and everything works until I get to the IPN script. I am losing my mind not to mention sleep over this and would like to try and figure it out within the next day or so as the deadline for the project is wednesday..

So now that I have the intro set here is the url for the Recipes site.

Here is the php code for the ipn.php file.

PHP Code:
<?php
session_start
();
include 
'inc/connect.inc';
$name=$_POST['first_name'].' '.$_POST['last_name'];
$email $_POST['payer_email'];
$phone $_POST['contact_phone'];
$table="users";
$msg="<HTML><BODY>";
$msg.="Hello,<br />";
$msg.='Your name:             '.$name.'<br />';
$msg.='Email:                 '.$email.'<br />';
$msg.='Phone #:                '.$phone.'<br />';
$msg.="<br />";
$msg.='Username:            '.$_POST['custom'].'<br />';
$msg.='Password:            '.$_POST['pass'].'<br />';
$msg.='Password Hint:        '.$_POST['passhint'].'<br />';
$msg.='Password Answer:        '.$_POST['passanswer'].'<br /></BODY></HTML>';
$headers="From: Your From Name Here <jason@bitsonline.us>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso-8859-1";
$to      'jason@bitsonline.us';
$subject 'Recipe Area | Invalid Payment';
$message '

Dear Administrator,

A payment has been made but is flagged as INVALID.
Please verify the payment manualy and contact the buyer.

Buyer Email: '
.$email.'
'
;
$headers1 'From:jason@bitsonline.us' "\r\n";
// read the post from PayPal system and add 'cmd'
$req 'cmd=_notify-validate';
foreach (
$_POST as $key => $value) {
$value urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " strlen($req) . "\r\n\r\n";
$fp fsockopen ('www.paypal.com'80$errno$errstr);
if (!
$fp) {
// HTTP ERROR
echo "Error processing your request. Please try again later.";
exit();
} else {
fputs ($fp$header $req);
while (!
feof($fp)) {
$res fgets ($fp1024);
if (
strcmp ($res"VERIFIED") == 0) {
$q1="update $table set name='$name',email='$email',phone='$phone',status='approved' where user='$_SESSION[user]')";
$res=mysql_query($q1) or die("Error updating user information: ".mysql_error());
    if(!empty(
$q) && isset($res)){
        
$mailsent=mail("jason@bitsonline.us,$email","New user registration",$msg,$headers);
        echo 
$mailsent "Registration complete. Check your email in a few minutes for more details.<br /><a href=recipes.php>Log In</a>" "Registration failed. <a href=recipes.php>Go Back</a>";
        exit();
// PAYMENT VALIDATED & VERIFIED!
}
if (
strcmp ($res"INVALID") == 0) {
// PAYMENT INVALID & INVESTIGATE MANUALY!
mail($to$subject$message$headers1);
}
}
}
}
fclose ($fp);
?>
All of it seems to be working except for the database update. Any suggestions are greatly appreciated. Thanks for your help in advance.
#blues #ipn #paypal #php
  • Profile picture of the author mywebwork
    Hi

    Are you getting the "Error updating user information" message? I assume 'inc/connect.inc' is your database connection file?

    Also, is $table defined to the correct table - I don't see that part in your code sample.

    Just trying to establish if you're actually connecting to the database, as from what you say the e-mail portion is working fine.

    Bill
    {{ DiscussionBoard.errors[1441229].message }}
    • Profile picture of the author Elliott Bean
      I'm curious why you're doing a table update rather than a table insert. Has the customer already been added to the table?
      Signature

      {{ DiscussionBoard.errors[1441476].message }}
      • Profile picture of the author bazzais
        Is the script sending you an error from the mysql?

        If not try sending yourself the $q1and seeing what it contains.

        Ta

        Baz
        {{ DiscussionBoard.errors[1442944].message }}
  • Profile picture of the author goliathseo
    I am not sure what it is as though it goes straight to the success page after the payment is given. The reason there is not an insert there but an update instead is because on the recipes.php page the customers record gets created in the db. I have tried echoing things on the ipn page but it just goes directly to the success.php page without even pausing.
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[1443184].message }}
  • Profile picture of the author Andy Fletcher
    You are using $q rather than $q1 in that call to empty. I have no idea what these variables are but presumably that's what's causing your issue.
    {{ DiscussionBoard.errors[1443236].message }}
  • Profile picture of the author Elliott Bean
    Paypal will post the variables to the IPN script but it will not get loaded in a browser. I had trouble because of this so instead of echoing I printed things to a file and looked at the file after I had run the script.
    Signature

    {{ DiscussionBoard.errors[1443314].message }}
  • Profile picture of the author goliathseo
    So how would i go about doing that? It has been years since I have worked with paypal IPN. Your help btw everyone is appreciated so much!
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[1443329].message }}
  • Profile picture of the author mywebwork
    Why don't you try adding the value of $q1 to your e-mail message variable $message as a test? You will then see exactly what the update query looks like, and you can then use PHP MyAdmin, SSH or the MySQL GUI tools to run the query manually.

    It might shed some light on what is really going on.

    Bill
    {{ DiscussionBoard.errors[1443432].message }}
  • Profile picture of the author goliathseo
    @Andy: Thanks for noticing that little error that I didn't catch though even with my looking for $q instead of $q1 it would still at least update the database table...
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[1443443].message }}
  • Profile picture of the author Elliott Bean
    I think the problem is with the sql statement

    $q1="update $table set name='$name',email='$email',phone='$phone',
    status='approved' where user='$_SESSION[user]')"
    ;

    The user never actually accesses the IPN script, it is called by paypal
    as a backend process. Therefore the
    $_SESSION[user] will always be blank
    because it needs the customer to visit the script to look for an active
    session.
    Signature

    {{ DiscussionBoard.errors[1443686].message }}
  • Profile picture of the author goliathseo
    So then should I post that set of code in the success page?
    If so then what else can be put in its place to make sure that the user is getting to the success page solely after a verified and complete payment? Another issue that comes to mind is then how do I access the variables to get the persons name, email address, etc on the success page?

    Edit:

    I think I saw another problem that I just fixed.

    I have the variable to get results of $q1 set to $res which is the same as the one communicating with paypal. So that has been changed to $results instead of $res
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[1443695].message }}
  • Profile picture of the author Elliott Bean
    You could ask them for their paypal email in recipes.php then update the row in the table where the email= payer_email, that way you can do all the processing in the IPN script and only confirm people as members that have actually paid.

    Or you could just ask for a payment, send them random login info in the email which they can change once they have logged in to the members area

    Just some random ideas, might not be ideal sorry
    Signature

    {{ DiscussionBoard.errors[1443796].message }}
  • Profile picture of the author goliathseo
    Well I had an error in my mysql statement for one. But I have since moved the mysql stuff to success.php. I did ask for their email address on recipes.php as it was used to verify that there was not a duplicate record.

    So...

    I think I am going to move that stuff back to ipn, but it seems as though it is skipping over the if statements and thus not showing validity or not.

    Edit:

    This has been confirmed. somehow the if statements have been skipped over to check if a payment is invalid or valid. So back to square one...
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[1443886].message }}
  • Profile picture of the author Elliott Bean
    Looking at the code samples over at paypal it look there is a slight issue with opening and closing brackets in your code, you could try this

    while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (
    strcmp ($res, "VERIFIED") == 0) {
    $q1="update $table set name='$name',email='$email',phone='$phone',status= 'approved' where user='$_SESSION[user]')";
    $res=mysql_query($q1) or die("Error updating user information: ".mysql_error());
    if(!empty(
    $q) && isset($res)){
    $mailsent=mail("jason@bitsonline.us,$email","New user registration",$msg,$headers);
    echo
    $mailsent ? "Registration complete. Check your email in a few minutes for more details.<br /><a href=recipes.php>Log In</a>" : "Registration failed. <a href=recipes.php>Go Back</a>";
    exit();
    }
    // PAYMENT VALIDATED & VERIFIED!
    }
    if (
    strcmp ($res, "INVALID") == 0) {
    // PAYMENT INVALID & INVESTIGATE MANUALY!
    mail($to, $subject, $message, $headers1);

    }
    }
    fclose ($fp);
    }

    ?>
    Signature

    {{ DiscussionBoard.errors[1444019].message }}
  • Profile picture of the author goliathseo
    Thank you but it still skipped them... Any other suggestions?
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[1444193].message }}
  • Profile picture of the author Elliott Bean
    Modify two sections as follows

    if (!$fp) {
    $out = fopen("ipn.log", 'w'); //print error report to ipn.log file
    fwrite($out, $errstr);
    fclose($out);
    }

    if (strcmp ($res, "INVALID") == 0) {
    mail($to, $subject, $message, $headers1);
    foreach ($_POST as $key => $value) {
    $out = fopen("ipn.log", $key);
    fwrite($out, $key);
    fwrite($out,"\n");
    fwrite($out, $value);
    fwrite($out,"\n");
    fclose($out);// print payments details to ipn.log file if error
    }
    }

    If something goes wrong you can check ipn.log

    I expect that there is something wrong with the $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr);

    Looking at my IPN script I have

    $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

    you could try that as my ipn script works fine.
    Signature

    {{ DiscussionBoard.errors[1444238].message }}
  • Profile picture of the author goliathseo
    Good News! It WORKS!!!! Give me fifteen minutes and I will have the whole article written up and posted on my site. PM if you want the link as I don't want to break the rules of this totally awesome forum!! Thank You ALL for your assistance!
    Signature

    Do you want to make successes or excuses? Success? Alright then... See what's in store for you....
    - The AC Assassin

    {{ DiscussionBoard.errors[1445438].message }}

Trending Topics