贝宝IPN脚本无法正常工作,错误400错误请求

时间:2018-07-03 23:42:13

标签: php paypal paypal-ipn

我正在尝试创建一个订阅网站。除了Paypal IPN脚本不起作用外,其他所有东西都工作正常。日志显示“ 400-错误的请求错误”。我发现的原因是Paypal对其IPN脚本要求进行了更改。新脚本与我正在使用的脚本有很大不同。我已经尝试了所有方法,但是无法使IPN脚本正常工作(无论新旧)。我到处搜索,并在这里找到解决方案:http://larryullman.com/forums/index.php?/topic/1577-chapter-6-ipn/&tab=comments#comment-9605。但是它不再适用,因为Paypal对脚本进行了进一步的更改。)有人可以更新我的IPN脚本,使其重新开始工作。非常感谢您的帮助。

下面是我的IPN脚本:

<?php


// Require the configuration before any PHP code as the configuration controls error reporting:
require ('./includes/config.inc.php');
// The config file also starts the session.

// Open the text file:
// The text file must be writable by PHP!
$file = fopen('./ipn.txt', 'a');

// Write the POST data to the file:
fwrite($file, "Received:\n");
fwrite($file, print_r($_POST, true));
fwrite($file, "\n");

// Start by creating a request variable:
$req = 'cmd=_notify-validate';

// Add each received key=value pair to the request:
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

// Open a socket connection to PayPal:
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live

if (!$fp) { // If we couldn't connect, send an email:

    trigger_error('Could not connect for the IPN!');

} else { // Send the request to PayPal:

    $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    fputs ($fp, $header . $req);

    // Write the PayPal request to the text file:
    fwrite($file, "Sent:\n");
    fwrite($file, "$header\n");
    fwrite($file, "$req\n");

    // Read in the response:
    while (!feof($fp)) {

        $res = fgets ($fp, 1024);

        // Write the PayPal response to the text file:
        fwrite($file, "Received:\n");
        fwrite($file, "$res\n");

        if (strcmp ($res, "VERIFIED") == 0) {

            // Check for the right values:
            if ( ($_POST['payment_status'] == 'Completed')
             && ($_POST['receiver_email'] == 'seller_1281297018_biz@mac.com')
             && ($_POST['mc_gross'] == 10.00)
             && ($_POST['mc_currency']  == 'USD') 
             && (!empty($_POST['txn_id']))
            ) {

                // Need the database connection now:
                require(MYSQL);

                // Check for this transaction in the database:
                $txn_id = mysqli_real_escape_string($dbc, $_POST['txn_id']);                
                $q = "SELECT id FROM orders WHERE transaction_id='$txn_id'";
                $r = mysqli_query ($dbc, $q);
                if (mysqli_num_rows($r) == 0) { // Add this new transaction:

                    $uid = (int) $_POST['custom'];
                    $status = mysqli_real_escape_string($dbc, $_POST['payment_status']);
                    $amount = (float) $_POST['mc_gross'];
                    $q = "INSERT INTO orders (user_id, transaction_id, payment_status, payment_amount) VALUES ($uid, '$txn_id', '$status', $amount)";
                    $r = mysqli_query ($dbc, $q);
                    if (mysqli_affected_rows($dbc) == 1) {

                        // Update the users table:
                        $q = "UPDATE users SET date_expires = IF(date_expires > NOW(), ADDDATE(date_expires, INTERVAL 1 YEAR), ADDDATE(NOW(), INTERVAL 1 YEAR)), date_modified=NOW() WHERE id=$uid";
                        $r = mysqli_query ($dbc, $q);
                        if (mysqli_affected_rows($dbc) != 1) {
                            trigger_error('The user\'s expiration date could not be updated!');
                        }

                    } else { // Problem inserting the order!
                        trigger_error('The transaction could not be stored in the orders table!');                      
                    }

                } // The order has already been stored!

            } // The right values don't exist in $_POST!

        } elseif (strcmp ($res, "INVALID") == 0) {
            // log for manual investigation
        }

    } // End of the WHILE loop.

    // Close the connection:
    fclose ($fp);

} // End of $fp IF-ELSE.

// Inidicate the end of this transaction in the text file:
fwrite($file, "--------------\n");
fclose($file);

?>

0 个答案:

没有答案