PayPal 集成问题:PDT 在返回网站时挂起

发布于 2024-09-01 18:12:29 字数 894 浏览 8 评论 0原文

我正在实施 PayPal IPN &太平洋夏令时。经过一些头痛和在沙箱中,IPN 运行良好,并且 PDT 返回正确的 $_GET 数据。实现如下:

  • 将用户 ID 以表单形式传递给 PayPal
  • 用户购买产品并触发 IPN,更新给定用户 ID 的数据库
  • PDT 在用户返回站点时返回交易 ID
  • 返回页面显示“请稍候”并重复 Ajax 检查交易状态
  • 用户被重定向到成功/失败页面

一切正常,除了当使用 PayPal 就绪 PHP 代码进行 PDT 执行返回 POST 时,页面挂起。用户永远不会回到我的网站。我没有得到失败状态,只是什么都没有。有趣的是,一旦发生未知错误,我的测试域就会在短时间内变得无响应。

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

如果我注释掉 POST 回来,一切正常。一旦代码进入 while{} 循环,我就能确定问题。不幸的是,我没有足够的经验来从头开始编写 PayPal 代码的替代品,所以我真的很感激任何关于可能出现问题的想法。

POST 返回到 ssl://www.sandbox.paypal.com,我使用的是通过沙箱测试帐户创建的按钮代码和授权令牌。

提前致谢。

更新:

我已将问题范围缩小到这一行:$line = fgets($fp, 1024);

它只是挂起,我不知道为什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-09-08 18:12:29

已解决:

切换到 cURL 可以解决所有问题。下面是代码,以防有人遇到这个问题并且像我一样绝望:

// 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
   }
}

从技术上讲, substr() 不是检查“SUCCESS”而是“SUCC”。然而,考虑到只有“SUCCESS”或“FAIL”是可能的值,这并不重要。

相同的代码也适用于 IPN,但有明显的细微修改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文