PayPal一直无效ipn

时间:2016-02-28 06:12:05

标签: php paypal paypal-ipn

这是我的IPN处理程序:

<?php
include 'constants.php';
include 'classes/class.database.php';

$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'));

$res = curl_exec($ch);

if (curl_errno($ch) != 0) // cURL error
    {
    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 {
    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) {
    $status = $_POST['payment_status'];
    $currency = $_POST['mc_currency'];
    $receiver = $_POST['receiver_email'];
    $buyer = $_POST['payer_email'];
    $buyer_name = $_POST['custom'];

    $db = new Database($sql_host, $sql_user, $sql_pass, $sql_data, "payments");
    $db->connect();

    for ($i = 0; $i < 10; $i++) {
        if (isset($_POST['item_name'.$i])) {
            $db->addPayment($_POST['item_name'.$i], $_POST['item_number'.$i], $status, $_POST['mc_gross_'.$i], $currency, $buyer, $receiver, $buyer_name);
        }
    }

    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的回归:

  

INVALID
  [2016-02-28 05:55 UTC]无效的IPN:cmd = _notify-validate&amp; mc_gross = 0.01&amp; protection_eligibility =合格&amp; address_status =已确认&amp; item_number1 = 36&amp; payer_id = 7GXJG3SBPYHEY&amp; tax = 0.00&amp; address_street = &安培; PAYMENT_DATE = 21%3A55%3A22 +二月+ 27%2C + 2016 + PST&安培; PAYMENT_STATUS =完成&安培;字符集= UTF-8和; address_zip = &安培; mc_shipping = 0.00&安培; mc_handling = 0.00&安培;如first_name = &安培; mc_fee = 0.01&安培; address_country_code = GB&安培; ADDRESS_NAME = &安培; notify_version = 3.8&安培;定制=亚历&安培; payer_status =验证&安培;业务= &安培; ADDRESS_COUNTRY =美国+英国&安培; num_cart_items = 1&安培; mc_handling1 = 0.00&安培; ADDRESS_CITY = &安培; verify_sign = AzEkqtmA1WmF4x7TDnCOWp34hPjAASvAQSuGR4.BNB0U46x2nInwBmEo&安培; PAYER_EMAIL = &安培; mc_shipping1 = 0.00&安培; tax1 = 0.00&安培; txn_id = 50446034DM994872C&安培; payment_type =即时&安培; payer_business_name = &安培;姓氏= &安培; ADDRESS_STATE = < /强>&安培; item_name1 =测试+%28do +未+买%29&安培; RECEIVER_EMAIL = ***&安培; payment_fee = 0.01&安培;数量1 = 1&安培; receiver_id = RJ5DJ76VDC7RC&安培; txn_type =购物&安培; mc_gross_1 = 0.01&安培; mc_currency = USD&安培; residence_country = GB&安培; transaction_subject =安培; payment_gross = 0.01&安培; ipn_track_id = 487a9f43c92

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

看起来您没有将IPN消息回发给https://www.paypal.com/cgi-bin/webscr,在这种情况下,您需要检查您的IPN脚本相关日志,可能有一些错误消息可能表明某些连接问题,然后你可以相应地解决问题。

答案 1 :(得分:0)

您需要处于测试模式才能使IPN Simulator正常工作。

确保设置define("USE_SANDBOX", 1);。 那你应该没问题