Page 1 of 1

Paypal IPN listener help

Posted: Thu Oct 04, 2012 12:31 pm
by rh100
I need a simple Paypal IPN listener php script that will take the Paypal returned info and update a previously established "confirmation" field in my database table named "customer". The "email" field in the "customer" table would tell the script which row to put the data into. I found a lot of listener scripts but could not find one that would update a database field in a previously established row. Eternally grateful for help.

Re: Paypal IPN listener help

Posted: Thu Oct 04, 2012 1:19 pm
by requinix
Do you want someone to write it for you or are you asking for help adapting something you found/wrote to make it work?

Re: Paypal IPN listener help

Posted: Fri Oct 05, 2012 10:30 am
by rh100
I have a code. I just need help getting the script to update my database with the the "verified" or "invalid" Paypal received data into a specific column and row in my database, using the UPDATE and WHERE commands. The part I don't get is how to get the script to find the specific field, called "confirmation" and the row, belonging to the customer who made the purchase, using the information received from Paypal. Thank You for your reply and help.

Code: Select all

<!--?php  
  
// paypal is going to send a large post to the url we specified in the admin.  
// read the post from PayPal system, turn it into a query url,  
// and add the 'cmd' validate value to post it back to PayPal.  
// this step ensures that the information we received from PayPal is  
// legitimate.  
$req = 'cmd=_notify-validate';  
  
foreach ($_POST as $key =--> $value) {  
     $value = urlencode(stripslashes($value));  
     $req .= "&$key=$value";  
}  
  
// post back to PayPal system for validation  
$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('ssl://www.paypal.com', 443, $errno, $errstr, 30);  
  
// turn the original post data into an object we can work with  
$txn = (object) $_POST;  
  
if (!$fp) {  
     log_error('HTTP CONNECTION ERROR', $_POST);  
} else {  
     fputs($fp, $header . $req);  
     while (!feof($fp)) {  
          $res = fgets($fp, 1024);  
  
          // proceed if transaction is verified  
          if (strcmp ($res, "VERIFIED") == 0) {  
  
               // Do some simple error checks:  
               // check if the payment_status is Completed  
               if ($txn->payment_status != 'Completed') {  
                    log_error('NOT COMPLETED', $txn);  
                    exit;  
               }  
               // check that receiver_email is your Primary PayPal email  
               if ( $txn->receiver_email != RECEIVER_EMAIL ) {  
                    log_error('INVALID RECEIVER EMAIL', $txn);  
                    exit;  
               }  
               // check that txn_id has not been previously processed  
               if ( check_existing_transaction($txn) ) {  
                    log_error('ALREADY PROCESSED', $txn);  
                    exit;  
               }  
  
               // Here's where the 'custom' magic happens. Use parse_str  
               // to take the still url encoded custom str and turn it  
               // into an array we can work with  
               $custom = array();  
               parse_str($txn->custom, $custom);  
  
               // Now we can do things with the $txn and $custom array  
               // like add a database record for the user/transaction,  
               // send a response email, etc.  
          }  
          else if (strcmp ($res, "INVALID") == 0) {  
               log_error('TRANSACTION INVALID', $txn);  
          }  
     }  
}  
  
fclose ($fp);  
exit;  
?>

Re: Paypal IPN listener help

Posted: Mon Oct 08, 2012 8:55 am
by rh100
I've had 34 people look at this thread and have not received an answer. Does anyone on this forum actually know how to write PHP?

Re: Paypal IPN listener help

Posted: Mon Oct 08, 2012 6:36 pm
by requinix
rh100 wrote:I've had 34 people look at this thread and have not received an answer. Does anyone on this forum actually know how to write PHP?
No, we just hang out here for the fun of it.

Posted: Tue Oct 09, 2012 3:46 pm
by Eric!
Your question is pretty vague. FIrst off PayPal is switching to HTML 1.1, so you need to fix your script to handle that condition. From PayPal
paypal wrote:Action Required before February 1, 2013

If your IPN (Instant Payment Notification) or PDT (Payment Data Transfer) scripts use the HTTP 1.0 protocol, you’ll need to update your scripts to HTTP 1.1, and include the “Host” header in the IPN postback script.
I suggest you create a sandbox account and test your IPN script. While you are doing this you can do a var_dump($txn) and see all the parameters passed from PayPal. Or of you just want to loop through your $_POST values that come from paypal, do something like this:

Code: Select all

foreach ($_POST as $key => $value) {
     echo "$key: $value\n";
}
That should be enough info for you to answer your own questions.