PayPal IPN侦听器在沙箱内部工作,但不在外部

时间:2015-06-05 06:36:12

标签: php mysqli paypal paypal-ipn

我正在运行这个由tutsplus编写的PayPal IPN监听器,它已根据我的需要进行了一些修改。一切正常,直到我从沙盒移动到实时模式。我已经查看了代码,并且不太了解我是否需要切换任何内容,或者它正在检查sandbox / live本身。

<?php
class PayPal_IPN{
function infotuts_ipn($im_debut_ipn) {

        define('SSL_P_URL', 'https://www.paypal.com/cgi-bin/webscr');
        define('SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
        $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
        if (!preg_match('/paypal\.com$/', $hostname)) {
            $ipn_status = 'Validation post isn\'t from PayPal';
            if ($im_debut_ipn == true) {
                // mail test
            }

            return false;
        }

      // parse the paypal URL
        $paypal_url = ($_REQUEST['test_ipn'] == 1) ? SSL_SAND_URL : SSL_P_URL;
        $url_parsed = parse_url($paypal_url);

        $post_string = '';
        foreach ($_REQUEST as $field => $value) {
            $post_string .= $field . '=' . urlencode(stripslashes($value)) . '&';
        }
        $post_string.="cmd=_notify-validate"; // append ipn command
        // get the correct paypal url to post request to
        $paypal_mode_status = $im_debut_ipn; //get_option('im_sabdbox_mode');
        if ($paypal_mode_status == true)
            $fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);
        else
            $fp = fsockopen('ssl://www.paypal.com', "443", $err_num, $err_str, 60);

        $ipn_response = '';

        if (!$fp) {
// could not open the connection.  If loggin is on, the error message
// will be in the log.
            $ipn_status = "fsockopen error no. $err_num: $err_str";
            if ($im_debut_ipn == true) {
                echo 'fsockopen fail';
            }
            return false;
        } else {
// Post the data back to paypal
            fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
            fputs($fp, "Host: $url_parsed[host]\r\n");
            fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
            fputs($fp, "Content-length: " . strlen($post_string) . "\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, $post_string . "\r\n\r\n");

// loop through the response from the server and append to variable
            while (!feof($fp)) {
                $ipn_response .= fgets($fp, 1024);
            }
            fclose($fp); // close connection
        }

// Invalid IPN transaction.  Check the $ipn_status and log for details.
        if (!preg_match("/VERIFIED/s", $ipn_response)) {
            $ipn_status = 'IPN Validation Failed';

            if ($im_debut_ipn == true) {
                echo 'Validation fail';
                print_r($_REQUEST);
            }
            return false;
        } else {
            $ipn_status = "IPN VERIFIED";
            if ($im_debut_ipn == true) {
                echo 'SUCCESS';

                }

            return true;
        }
    }


    function ipn_response($request){
    mail("mssoad@gmail.com","My subject",print_r($request,true));
    $im_debut_ipn=true;
        if ($this->infotuts_ipn($im_debut_ipn)) {

            // if paypal sends a response code back let's handle it        
                   if ($im_debut_ipn == true) {
                    $sub = 'PayPal IPN Debug Email Main';
                    $msg = print_r($request, true);
                    $aname = 'infotuts';
                  //mail send
                }

                // process the membership since paypal gave us a valid +
                $this->insert_data($request);
            }
    }
function issetCheck($post,$key){
if(isset($post[$key])){
$return=$post[$key];
}
else{
$return='';
}
return $return;
}   
    function insert_data($request){
    require_once('dbconnect.php');


$post=$request;
$item_name=$this->issetCheck($post,'item_name');
$amount=$this->issetCheck($post,'mc_gross');
$currency=$this->issetCheck($post,'mc_currency');
$payer_email=$this->issetCheck($post,'payer_email');
$first_name=$this->issetCheck($post,'first_name');
$last_name=$this->issetCheck($post,'last_name');
$country=$this->issetCheck($post,'residence_country');
$txn_id=$this->issetCheck($post,'txn_id');
$txn_type=$this->issetCheck($post,'txn_type');
$payment_status=$this->issetCheck($post,'payment_status');
$payment_type=$this->issetCheck($post,'payment_type');
$payer_id=$this->issetCheck($post,'payer_id');
$date=$this->issetCheck($post,'custom');
$create_date=date('Y-m-d H:i:s');
$payment_date=date('Y-m-d H:i:s');

$firstLast = $first_name . $last_name;

$explode = explode('|', $item_name);

foreach($explode as $slot) {

    if(strlen($slot) > 0) {

        $query = "INSERT INTO bookings (date, start, name, email, phone, order_id) VALUES ('$date', '$slot', '$firstLast', '$payer_email', '$phone', '$orderid')"; 
        $result = mysqli_query($con, $query) or die(mysqli_error($link)); 

    } // Close if

} // Close foreach


mysqli_query($con,"INSERT INTO trans_tbl (item_name,ride_day,payer_email,first_name,last_name,amount,currency,country,txn_id,txn_type,payer_id,payment_status,payment_type,create_date,payment_date) 
VALUES ('$item_name','$date','$payer_email','$first_name','$last_name','$amount','$currency','$country','$txn_id','$txn_type','$payer_id','$payment_status','$payment_type','$create_date','$payment_date')");
mysqli_close($con);



    }
    }
    $obj = New PayPal_IPN();
    $obj->ipn_response($_REQUEST);

    ?>

在paypal网站的IPN历史记录中,它停留在已发送 - 重新发送。

我已设置了IPN设置并在个人资料设置中通知了网址,并拥有了一个商家帐户。我的电子邮件也在帐户中进行了验证。 另外需要注意的是,我一直在做简单的0.01美元来测试这个沙盒外模式,返回页面工作正常而不是ipn监听器。

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

$paypal_mode_status = $im_debut_ipn; //get_option('im_sabdbox_mode');
if ($paypal_mode_status == true)
    $fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);
else
    $fp = fsockopen('ssl://www.paypal.com', "443", $err_num, $err_str, 60);

可能不正确,你应该测试确认,但我认为这是因为你的$im_debut_ipn变量设置为TRUE时,不仅是“调试”,而且由于某种原因也被用于确定贝宝网址。然后它将url设置为paypals sandbox url(见上文)。

即,何时

$im_debut_ipn = true

然后,

$fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);

也是如此。

请注意,我认为$im_debut_ipn实际上是一个拼写错误,应该是..debug..,这意味着可以显示错误消息的调试模式。