Jump to content

Php Ipn Listener For Paypal


rmarsh9374

Recommended Posts

I am new to PHP and really enjoying learning it. I am stuck on a key part of my project for my small company. I am trying to learn how the IPN Listener for Paypal works. They provided an example in PHP. So I figured i'd modify it to send me an email when a person does a "normal" purchase. After I got that working I'd take that and work it into the project. I got a responce from the IPN Listnener when I had a typo in the url to send info back to Paypal for varification, but once I fixed that I have got nothing. I am sure I am making a lot of Noobie mistakes in it, please help me learn. I've been looking at it so long I am lost as how to fix it. Ryan (The project will in the end take the verified message from Paypal and pull information from the db and put in a file and email it to the client.) below is my code: <?php$dvd_purchase = TRUE;//variables to compaire against IPN information$dvd_cost = 149.00;$test_custom = 25;$boss_email = 'boss@mycompany.com';//Showme the errorsini_set ('display_error', 1);error_reporting (E_ALL | E_STRICT);// read the post from paypal systemand add 'cmd'$req = 'cmd=_notify-validate';foreach ($_POST as $key => $value) { $value = urlencode (stripslashes ($value)); $req .= "&$key=$value";}//postback to Paypal system to validate$header = '';$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);$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);//assign posted variables to local variablesif (isset($_POST['item_name'])){$item_name = $_POST['item_name'];}if (isset($_POST['item_number'])){$item_number = $_POST['item_number'];}if (isset($_POST['payment_status'])){$payment_status = $_POST['payment_status'];}if (isset($_POST['mc_gross'])){$payment_amount = $_POST['mc_gross'];}if (isset($_POST['txn_id'])){$txn_id = $_POST['txn_id'];}if (isset($_POST['receiver_email'])){$receiver_email = $_POST['receiver_email'];}if (isset($_POST['payer_email'])){$payer_email = $_POST['payer_email'];}if (isset($_POST['custom'])){$custom = $_POST['custom'];}if (!$fp) { // HTTP ERROR $to= myemail@mycompany.com; $subject= "DVD Order Could Not Connect to PayPal!"; $message= "PayPal connection failed ". "\r\n" . $from = "IPN"; $headers = "From: $from" . "\r\n"; mail($to,$subject,$message,$headers);} else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, "VERIFIED") ==0) { if ($payment_amount != $dvd_cost){ $dvd_purchase = FALSE; } if ($custom != $test_custom){ $dvd_purchase = FALSE; } if ($receiver_email != $boss_email){ $dvd_purchase = FALSE; } if ($dvd_purchase = TRUE){ //Email me it is ok. $to= myemail@mycompany.com; $subject= "DVD Order Good to Go!"; $message= "DVD ORDER: "."$payer_email". "\r\n" . "Iten Name: " ."$item_name". "\r\n" . "Iten Number: " ."$item_number". "\r\n" . "Payment status: " ."$payment_status". "\r\n" . "Payment Amount: " ."$payment_amount". "\r\n" . "Transaction ID: " ."$txn_id". "\r\n" . "receiver_email: " ."$receiver_email". "\r\n" . "payer_email: " . "$payer_email" . "\r\n"; $from = "$payer_email"; $headers = "From: $from" . "\r\n"; mail($to,$subject,$message,$headers); } if ($dvd_purchase = FALSE){ $to= myemail@mycompany.com; $message = "Somthing is off:"."$payment_amount". "\r\n". "$custom"."\r\n". "$receiver_email"."\r\n"; $from = "IPN"; $headers = "From: $from" . "\r\n"; mail($to,$subject,$message,$headers); } } else if (strcmp ($res, "INVALID") ==0) { //log for manual investigation $to= "myemail@mycompany.com"; $subject= "DVD Order IPN Verification Failed!"; $message= "DVD ORDER: "."$payer_email". "\r\n" . "Iten Name: " ."$item_name". "\r\n" . "Iten Number: " ."$item_number". "\r\n" . "Payment status: " ."$payment_status". "\r\n" . "Payment Amount: " ."$payment_amount". "\r\n" . "Transaction ID: " ."$txn_id". "\r\n" . "receiver_email: " ."$receiver_email". "\r\n" . "payer_email: " . "$payer_email" . "\r\n"; $from = "$payer_email"; $headers = "From: $from" . "\r\n"; mail($to,$subject,$message,$headers); } } fclose ($fp);}?>

Link to comment
Share on other sites

$boss_email = 'boss@mycompany.com';//Showme the errors If those square brackets are in your actual code, they shouldn't be. You should also change your error settings to write to a log instead of display the errors. PayPal is making the requests, so there's no reason to show the errors to PayPal instead of seeing them yourself. You can set the log_errors option to on, and the error_log option holds the name of the file you want to write errors to. This will send errors to a file called error.log in the same directory: ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('display_errors', 0); So, step 1 is to make sure you can get the error messages. In your script you can use the error_log function to write whatever you want to the error log, so you should use that to send debugging output to make sure the code is doing what you expect it to. You can use this to write all of $_POST to the error log: error_log(print_r($_POST, true));

Link to comment
Share on other sites

Thanks for the input, somthing must have happened with the cut/past. Theextra stuff in the code is not there in my "origional".Should look like this:$boss_email = 'boss@mycompany.com';//show me the errors Didn't think about the error message, i'm just use to it being there from my other pages, I'll work it in thanks. What else have I missed? :) Thanks so much for the help so far

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...