PayPal IPN

I’m tring to get PayPal IPN to work with my site, heres the code:

else if(isset($_GET['source']) && $_GET['source'] == "paypal")
{
	$postvars = array();
	while (list ($key, $value) = each ($_POST)) {
	$postvars[] = $key;
	}
	
	if(count($postvars) < 1){die("No parameters");}

	$req = 'cmd=_notify-validate';
	for ($var = 0; $var < count ($postvars); $var++) {
	$postvar_key = $postvars[$var];
	$postvar_value = $$postvars[$var];
	$req .= "&" . $postvar_key . "=" . urlencode ($postvar_value); }
	
	$command = "curl -d \\"$req\\" http://www.paypal.com"; 
	exec($command, $output, $return);
	echo $command;
	print_r($output);
	
    if($output['0'] === "VERIFIED") {
      $username = $_POST["custom"];
      $untilDate = $_POST["payment_date"];
      $email = $_POST["payer_email"];
      if(checkUser($username)){
        if(isset($_POST["txn_type"]) &&
		  strtolower($_POST["txn_type"]) == "subscr_signup" || strtolower($_POST["txn_type"]) == "subscr_payment") {
          updateLicense($username, "PayPal");
          $type = strtolower($_POST["txn_type"]);
       	  sendNotification("PayPal", 1, $type, $email);
          echo "SUCCESS add";
        }else if(isset($_POST["txn_type"]) &&
		strtolower($_POST["txn_type"]) == "subscr_cancelled" || strtolower($_POST["txn_type"]) == "subscr_failed") { 
          updateLicense($username, "null");
          $type = strtolower($_POST["txn_type"]);
       	  sendNotification("PayPal", 0, $type, $email);
          echo "SUCCESS remove";
		}		
        else {
          mail( "me@janbirsa.com", "PayPal IPN Notice", "User: $username Status: Incorrect Transaction Type ", "From: noreply@janbirsa.com" );
        } 
      }
}
}

But for some reason, it doenst work. I checked with PayPal, its all configured properly. I tried to make test HTTP POST, but PayPal returns “INVALID” as $output[‘0’]

I am obviously doing something wrong because neither with curl or fsock it doesnt work!!

Apparently support questions for the IPN module can be posted at the Official PayPal IPN Support Thread

One of the posts says:

In regard to feedback, it should be noted that PayPal themselves sometimes return an ‘INVALID’ response even though the transaction is authentic and qualified, a workaround suggested by PayPal is to return a 500 Server Error response which will cause PayPal to resend the IPN so that it maybe successfully processed on the following attempts etc…

You might find out more from them if that doesn’t help.

I have never seen anyone use curl to do the post back

Well now you have :slight_smile: works fine here.

But is the code OK? I didn’t really know how to do it so I just used curl, but are the post parameters ok? Is validation procedure OK? If code is all OK, I will contact PayPal.

Your account may not have permission to make the request. I ran into that problem twice using sandbox accounts. One would think the sandbox account has access to all API methods but this is not exactly true. I’m not sure if its a bug with the system or what but I had to call PayPal support to resolve the the issue.

for some reason fsockopen does timeout so I went with curl to see if that would work? Can I make httpwebrequest and specify headers (content-type…)…any function for that or tutorial?

I have never seen anyone use curl to do the post back. All the examples when I set up mine used fsockopen so I set mine up to work using a copy of that sample code as my starting point.

You have to post back the values exactly the same as you received them using Content-Type: application/x-www-form-urlencoded

ahh, I see fsock is the easiest way, I will re-try it…