WTFranklin Posted August 27, 2011 Share Posted August 27, 2011 Hello, I've been going through PayPal's documentation for setting up the IPN. I set up the listener like they have in the documentation (pasted below). I know there's still more to add on my end for if the response is valid or not (I have basic 'Valid' or 'not valid' responses for now), but I was trying to do the IPN test in the sandbox and it says it tests okay. I don't get any emails and it doesn't show up in my IPN history. I'm not sure if there is something I'm missing or if there's a better example out there somewhere that is a little more practical or illustrative than the documentation, but any help is appreciated. Thanks in advance! -Frank <?php error_reporting(E_ALL ^ E_NOTICE); $email = $_GET['ipn_email']; $header = ""; $emailtext = ""; //Read the post from PayPal and add 'cmd' $req = 'cmd=_notify-validate'; if(function_exists('get_magic_quotes_gpc')) { $get_magic_quotes_exists = true; } //Handle escape chars, which depends on setting of magic quotes foreach ($_POST as $key => $value) { if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { $value = urlencode(stripslashes($value); } else { $value = urlencode($value); } $req .= "&$key=$value"; }//foreach //Post back to paypal 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('ssl:www.sandbox.paypal.com', 443, $errno, $errstr, 30); //Process validation from Paypal //TODO: This sample does not test the HTTP response code. All //HTTP response codes must be handles or you should use an HTTP //library, such as cURL if(!$fp) { //HTTP ERROR echo 'not opening socket'; } else { //NO HTTP ERROR fputs ($fp, $header . $req); while(!feof($fp)) { $res = fgets($fp, 1024); if(strcmp ($res, "VERIFIED") == 0) { //TODO: //Check the payment_status is completed //Check that receiver_email is your primary paypal email //check that the payment amount/payment currency are correct //Process payment //If 'VERIFIED', send an email of IPN variables and values to the //specificed email address foreach($_POST as $key => $value) { $emailtext .= $key . " = " . $value . "\n\n"; } if!(mail($email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req)) { echo 'not emailing if verified'; } echo 'verified!'; } else if (strcmp ($res, "INVALID") == 0) { //If 'INVALID', send an email. TODO: Log for manual investigation. foreach($_POST as $key => $value) { $emailtext .= $key . " = " . $value . "\n\n"; } if(!mail($email, "Live-INVALID IPN", $emailtext . "\n\n" . $req)) { echo 'not emailing if invalid'; } } } } fclose($fp); ?> Quote Link to comment Share on other sites More sharing options...
MicahCarrick Posted September 11, 2011 Share Posted September 11, 2011 The initial testing on a PayPal IPN listener can be tricky. I just wrote a blog post on this very subject: PayPal IPN with PHP. In a nutshell... Before you even try to get your listener working with PayPal IPN, make sure it is setup to log errors and focus your attention on error handling every scenario you can think of. If you setup your listener such that hard errors are logged, and proper IPN responses are emailed, then you can isolate the problem to your own IPN code versus a PayPal configuration or missing field. Use the IPN simulator in the PayPal sandbox. Make sure your IPN listener properly handlers the IPN first. Then you can begin doing sandbox transaction testing. If you know your IPN script works, and it doesn't when you're doing sandbox testing, you can assume that your seller account is not setup with an IPN listener or you are not passing notify_url properly. If you use PHP-PayPal-IPN then it will handle the post back to paypal and let you focus on what to do with verified IPNs (insert in datatabse, activate membership, fulfill orders, etc.). If you do use this class to implement your listener and have any problems with it connecting to PayPal, let me know and I'll help you troubleshoot the issue. Quote Link to comment Share on other sites More sharing options...
WTFranklin Posted September 19, 2011 Author Share Posted September 19, 2011 Hey Micah, Sorry for the delay in response. Thank you so much for writing that tutorial, that cleared up so much! The one issue I ran into so far is during the testing it asks me to log in with the generated user account I set up in my sandbox account and my password doesn't want to work for that. I created this account maybe six months ago, would it be better to just start new accounts to see that helps? Also, this is thinking ahead (and probably a dumb question) when I'm ready to go live with this, would I just point to this script from my main paypal account and change the field in the form for the business email? Thanks! -Frank Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.