2012-08-09 91 views
-1

我无法将PayPal的IPN集成到我的PHP中,我有以下脚本,并且在PayPal沙箱中进行付款时,它会一直落入默认情况。PayPal IPN集成到PHP

任何帮助,将不胜感激!

$request = "cmd=_notify-validate"; 
    foreach ($_POST as $varname => $varvalue){ 
     $email .= "$varname: $varvalue\n"; 
     if(function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc()){ 
      $varvalue = urlencode(stripslashes($varvalue)); 
     } 
     else { 
      $value = urlencode($value); 
     } 
     $request .= "&$varname=$varvalue"; 
    } 
    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,"https://www.sandbox.paypal.com/cgi-bin/webscr"); 
    //curl_setopt($ch,CURLOPT_URL,"https://www.paypal.com"); 
    curl_setopt($ch,CURLOPT_POST,true); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$request); 
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 
    $result = curl_exec($ch); 
    curl_close($ch); 

    switch($result){ 
     case "VERIFIED": 
        mail('[email protected]','worked','worked'); 
      break; 
     case "INVALID": 
      mail('[email protected]','invaild','invaild'); 
      break; 
     default: 
      mail('[email protected]','failed','failed'); 
    } 

如果我发给自己$导致它仅仅是空白。

编辑:我发现这是我的LAMP服务器端问题,但我不确定如何解决它。

注:我确实在服务器上安装了curl,但我不确定它是否配置正确。

回答

2

我建议使用的var_dump做一些调试和Paypal的测试工具位于:https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session

我能理解它变得困难,并与第三方服务工作时耗费时间。

它可能是值得简单地抓住POST数据,序列化并将其应用到变量,所以你可以测试没有贝宝击中你的回调。

我会这样做,最初抓住贝宝POST。

<?php 
    file_put_contents(serialize($_POST), 'post.log'); 
    //Now you have the post request serialized we can grab the contents and apply it to a variable for fast testing. 
?> 

的启动代码:

<?php 
    $_POST = unserialize(file_get_content('post.log')); 
    //Now you can execute the script via command line or within your browser without requiring PayPal's testing tool. Use var_dump to investigate what's the issue. 
    $request = "cmd=_notify-validate"; 
    foreach ($_POST as $varname => $varvalue){ 
     $email .= "$varname: $varvalue\n"; 
     if(function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc()){ 
      $varvalue = urlencode(stripslashes($varvalue)); 
     } 
     else { 
      $value = urlencode($value); 
     } 
     $request .= "&$varname=$varvalue"; 
    } 
?> 

NOW:这是一个比较有效的,高效的,当涉及到测试。在你的例子中,你正在给自己发送电子邮件,但不包括邮件功能体内任何地方的$ result。 http://php.net/manual/en/function.mail.php

PayPal的IPN示例使用fsock,尽管CURL更有效且更易于使用。 PayPal的沙箱也在不断变化。 https://www.paypal-community.com/t5/Selling-on-your-website/IPN-response-problem/m-p/519862/message-uid/519862#U519862

另外:要确定是什么主要原因,正如你所说,它看起来像你的LAMP堆栈。通常从他们的日志目录(通常是/ var/log /)中检查你将能够确定哪些是失败的。

+0

我解决了这个问题,看起来像贝宝有问题通过SSL通信到我的服务器,我把它放在下面的行,它的工作: curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); – Jeff 2012-08-09 18:14:27