php - PayPal IPN在沙箱上工作,但不是直播

时间:2015-06-14 13:15:20

标签: php paypal paypal-ipn

我已经获得了这个PayPal IPN脚本,在沙盒模式下进行了测试,它运行得很好但是当我在实时模式下使用它时它不起作用。没有错误或其他。我该怎么做才能解决这个问题?

    <?php
define("DEBUG", 0);
define("USE_SANDBOX", 0);
define("LOG_FILE", "./ipn.log");
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
    $keyval = explode ('=', $keyval);
    if (count($keyval) == 2)
        $myPost[$keyval[0]] = urldecode($keyval[1]);
}
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
        $value = urlencode(stripslashes($value));
    } else {
        $value = urlencode($value);
    }
    $req .= "&$key=$value";
}
if(USE_SANDBOX == true) {
    $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
    $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
}
$ch = curl_init($paypal_url);
if ($ch == FALSE) {
    return FALSE;
}
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if(DEBUG == true) {
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$cert = __DIR__ . "./cacert.pem";
curl_setopt($ch, CURLOPT_CAINFO, $cert);
$res = curl_exec($ch);
if (curl_errno($ch) != 0)
    {
    if(DEBUG == true) { 
        error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
    }
    curl_close($ch);
    exit;
} else {
        // Log the entire HTTP response if debug is switched on.
        if(DEBUG == true) {
            error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
            error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
        }
        curl_close($ch);
}
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp ($res, "VERIFIED") == 0) {

    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];
    $payment_status = $_POST['payment_status'];
        $item_number = $_POST['item_number'];

        if($payment_status == 'Completed') {
        require_once("../assets/includes/core.php");
        require_once("../assets/includes/settings.php");

if(USE_SANDBOX == true) {
        $rec_email = "sandboxemail@email.com";

} else {
        $rec_email = "mypaypalemail@email.com";
}

        if($receiver_email == $rec_email) {
        $con = mysql_connect($dbhost,$dbuser,$dbpass);
        mysql_select_db('website');

        function generateString($length)
{   
    $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";

    for($i=0; $i<$length; $i++) 
        $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 

    return $key;
}
$code = generateString("30");

           $insert_code = mysql_query("INSERT INTO `$dbtable_wsc` (`code`,`payer_email`,`item_number`,`used`,`gen_date`) VALUES('$code','$payer_email','$item_number','no', NOW())") or die("MYSQL INSERT CODE QUERY ERROR: " . mysql_error());

        mysql_close($con);
    require("phpmailer/class.phpmailer.php");

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Host     = "ssl://smtp.gmail.com";
    $mail->Port     = 465;
    $mail->Username = "mydomain@email.com";
    $mail->Password = "******";
    $mail->FromName = "mydomain";
    $mail->Subject  = "mydomain | Redeem Code";
    $mail->Body     = "
    Your redeem code is ".$code."

    To use your code click here: $root_link/webshop/red.php?code=".$code."&step=step2

    Thank you for have given your support with a donation to mydomain, we really appreciate it!

    mydomain";
    $mail->AddAddress($payer_email);
    if(!$mail->Send()){
        echo "Mailer Error. Contact our Support Team giving them this error: " . $mail->ErrorInfo;
    }

echo "Email sent to $payer_email with the redeem code. If you didn't received your email go <a href='./red.php'>there</a> to redeem you code.";
    } else {
        die("You are trying to pay to the wrong email!");
        }

}
    if(DEBUG == true) {
        error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
    }
} else if (strcmp ($res, "INVALID") == 0) {
    if(DEBUG == true) {
        error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
    }
}
?>

在沙箱模式下对此进行了多次测试,但每次我设置此项并且我的客户尝试购买某些内容时,所有作品都会通过IPN确认,因此他们无法获得任何结果。我还想了解如何更好地检查以确保付款尚未处理或不是假的。谢谢!

P.S。我知道不推荐使用php的mysql扩展,但目前这不是我的主要问题。

1 个答案:

答案 0 :(得分:0)

没有阅读代码,但我知道我看了,有两种不同的ITN:一种用于实时,一种用于沙盒。

必须同时设置它们

希望有所帮助