views:

214

answers:

1

Hi,

I'm implementing PayPal IPN & PDT. After some headache & time at the sandbox, IPN is working well and PDT returns the correct $_GET data. The implementation is as follows:

  • Pass user ID in form to PayPal
  • User buys product and triggers IPN which updates database for given user ID
  • PDT returns transaction ID when user returns to site
  • The return page says "please wait" and repeat-Ajax-checks for the transaction status
  • User is redirected to success/failure page

Everything works well, EXCEPT that when using the PayPal ready PHP code for PDT to do a return POST, the page hangs. The user never gets back to my site. I'm not getting a fail status, just nothing. The funny thing is that once the unknown error occurs, my test domain becomes unresponsive for a short period.

The code (PHP): https://www.paypal.com/us/cgi-bin/webscr?cmd=p/xcl/rec/pdt-code-outside

If I comment out the POST back, it all works fine. I'm able to pin down the problem to once the code enters the while{} loop. Unfortunately, I'm not experienced enough to write a replacement from scratch for the PayPal code, so would really appreciate any ideas on what might be wrong.

The POST back goes to ssl://www.sandbox.paypal.com, and I'm using button code and an authorisation token that have all been created via a sandbox test account.

Thanks in advance.

UPDATE:

I've narrowed the problem down to this line: $line = fgets($fp, 1024);

It simply hangs and I'm not sure why.

+1  A: 

RESOLVED:

Switching to cURL solves all problems. Here's the code in case someone comes by this and is as desperate as I was:

// Prepare data
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = '<-- your token (sandbox or live) -->';
$req .= '&tx='.$tx_token.'&at='.$auth_token;
// Post back to PayPal to validate
$c = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); // SANDBOX
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $req);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($c);
$response_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
if(!$contents || $response_code != 200) {
   // HTTP error or bad response, do something
} else {
   // Check PayPal verification (FAIL or SUCCESS)
   $status = substr($contents, 0, 4);
   if($status == 'FAIL') {
      // Do fail stuff
   } elseif($status == 'SUCC') {
      // Do success stuff
   }
}

Technically, the substr() is not checking for "SUCCESS" but "SUCC". However, given that only "SUCCESS" or "FAIL" are possible values, it doesn't matter.

This same code works for IPN as well, with the obvious minor modifications.

Tom