Paypal IPN Sandbox始终返回INVALID(WITH DEBUG)

时间:2016-05-31 16:19:42

标签: php paypal paypal-ipn paypal-sandbox

我尝试实施PayPal IPN但总是返回INVALID。我知道这是一个反复出现的问题而且我已经看过类似的问题,但我无法解决它。 我在Paypal开发人员和沙箱上创建了一个帐户。我在具有IPN网址的沙箱上设置了帐户。 我测试了IPN模拟器,得到了绿色的消息"发送了IPN并且验证了握手。"

我的代码与提供Paypal示例相同,将网址更改为指向沙盒。

<?php

define("DEBUG", 1);
define("USE_SANDBOX", 1);
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, false); // default: 1
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // default: 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) // cURL error
    {
    if(DEBUG == true) { 
        error_log(PHP_EOL . "-------------------------------------------------------------------------\n" . PHP_EOL, 3, LOG_FILE);
        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);
        error_log(PHP_EOL . "-------------------------------------------------------------------------\n" . 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(PHP_EOL . "------------------------ START HTTP REQUEST OF VALIDATION REQUEST ---------------------------\n" . PHP_EOL, 3, LOG_FILE);
            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(PHP_EOL . "------------------------ END HTTP REQUEST OF VALIDATION REQUEST ---------------------------\n" . PHP_EOL, 3, LOG_FILE);
            error_log(PHP_EOL ."*". PHP_EOL, 3, LOG_FILE);
            error_log(PHP_EOL . "------------------------ START RESPONSE OF VALIDATION REQUEST -------------\n" . 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);
            error_log(PHP_EOL . "------------------------- END RESPONSE OF VALIDATION REQUEST -------------\n" . 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) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment and mark item as paid.
    // assign posted variables to local variables
    //$item_name = $_POST['item_name'];
    //$item_number = $_POST['item_number'];
    //$payment_status = $_POST['payment_status'];
    //$payment_amount = $_POST['mc_gross'];
    //$payment_currency = $_POST['mc_currency'];
    //$txn_id = $_POST['txn_id'];
    //$receiver_email = $_POST['receiver_email'];
    //$payer_email = $_POST['payer_email'];

    if(DEBUG == true) {
        error_log(PHP_EOL . "------------------------- START VERIFIED IPN-------------------------------\n" . PHP_EOL, 3, LOG_FILE);
        error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
        error_log(PHP_EOL . "------------------------- END VERIFIED IPN-------------------------------\n" . PHP_EOL, 3, LOG_FILE);
    }
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
    // Add business logic here which deals with invalid IPN messages
    if(DEBUG == true) {
        error_log(PHP_EOL . "------------------------ START INVALID IPN ----------------------------\n" . PHP_EOL, 3, LOG_FILE);
        error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
        error_log(PHP_EOL . "------------------------- END INVALID IPN ----------------------------\n" . PHP_EOL, 3, LOG_FILE);
    }
}

?>

这是调试文件。数据接收然后发送是相同的。我用diffchecker检查了这个。

--- START HTTP REQUEST OF VALIDATION REQUEST ---
[2016-05-31 11:50 UTC] HTTP request of validation request:POST /cgi-bin/webscr HTTP/1.1
Host: www.sandbox.paypal.com
Accept: */*
Connection: Close
Content-Length: 930
Content-Type: application/x-www-form-urlencoded

 for IPN payload: cmd=_notify-validate&payment_type=instant&payment_date=Tue+May+31+2016+13%3A50%3A22+GMT+0200+%28Hora+de+verano+romance%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John+Smith&address_country=United+States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San+Jose&address_street=123+any+street&business=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=seller%40paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=705782812&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AnTtKanWvA9fOSykMNVIDFCr8B6oAIL.gR5ZR2RjDD.y8fHCYDoudx6e
--- END HTTP REQUEST OF VALIDATION REQUEST ---

--- START RESPONSE OF VALIDATION REQUEST ---
[2016-05-31 11:50 UTC] HTTP response of validation request: HTTP/1.1 200 OK
Date: Tue, 31 May 2016 11:50:28 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Set-Cookie: c9MWDuvPtT9GIMyPc3jwol1VSlO=tiFAjXzjRap-cueIy6IvOIuyXEDeOqGj2EOZ4kGxnAcHI6wUU6Jt_CbHdK5imoAsJCKRirDu-jdw_IrfdkaJa77vQUvlkG6tZnaedRck1D8pqjwX3yVn7iaA-95NMS4yvpv4sIhWjFbeo7qr1dJVVoIB5Pi6fKMw1r8-ipWfhL10rl4yPYXrRMozluR_GfE6axFxKGZuuQa3oJrWNHAozFy4AgqbvRdT4tQdF2xmlxbjElokDVQQbG9z2K4aNECvAoVMl8TF1ilGIebFsQGhDD_uLXhrGxCaCiBBvMrUHagnlbXmrqeKomEl5zADjdrMHuvpnb4SpsDKGo-bjbgNsKNveM6j3Y1Mdn8TQlXjv4M5S5Ei19S1ks_H3AcHWLygzcsy5i5e7YJvor_PKyrnFepc3o9ILnj6Xx0izzZgf5vrQxkiddGZESmeJUe; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: cookie_check=yes; expires=Fri, 29-May-2026 11:50:29 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navlns=0.0; expires=Thu, 31-May-2018 11:50:29 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: Apache=10.72.108.11.1464695428758058; path=/; expires=Thu, 24-May-46 11:50:28 GMT
Vary: Accept-Encoding,User-Agent
Connection: close
HTTP_X_PP_AZ_LOCATOR: sandbox.slc
Paypal-Debug-Id: eaa609cbb480c
Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.WEB.1%26silo_version%3D1880%26app%3Dappdispatcher%26TIME%3D2222607703%26HTTP_X_PP_AZ_LOCATOR%3Dsandbox.slc; Expires=Tue, 31 May 2016 12:20:29 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
Strict-Transport-Security: max-age=14400
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

INVALID
--- END RESPONSE OF VALIDATION REQUEST ---

--- START INVALID IPN ---
[2016-05-31 11:50 UTC] Invalid IPN: cmd=_notify-validate&payment_type=instant&payment_date=Tue+May+31+2016+13%3A50%3A22+GMT+0200+%28Hora+de+verano+romance%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John+Smith&address_country=United+States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San+Jose&address_street=123+any+street&business=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=seller%40paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=705782812&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AnTtKanWvA9fOSykMNVIDFCr8B6oAIL.gR5ZR2RjDD.y8fHCYDoudx6e
--- END INVALID IPN ---

问题出在哪里?我无法找到它。

编辑*新测试代码

$raw_post_data = file_get_contents('php://input');
$pre = "cmd=_notify-validate";
$req = $pre."&".$raw_post_data;
RECEIVED
payment_type=instant&payment_date=Wed%20Jun%2001%202016%2017%3A23%3A30%20GMT+0200%20%28Hora%20de%20verano%20romance%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=218898678&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31A2H6KSDfssM5U2ik4231sae5BneW

SENT
cmd=_notify-validate&payment_type=instant&payment_date=Wed%20Jun%2001%202016%2017%3A23%3A30%20GMT+0200%20%28Hora%20de%20verano%20romance%29&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=218898678&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31A2H6KSDfssM5U2ik4231sae5BneW

RESULT = INVALID
RECEIVED
payment_type=instant&payment_date=Any%20character%20except%20%28more%29%20-%20123&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=218898678&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31A05enR9AkCnaKLwF3GcNFhj87aOl

SENT
cmd=_notify-validate&payment_type=instant&payment_date=Any%20character%20except%20%28more%29%20-%20123&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=218898678&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31A05enR9AkCnaKLwF3GcNFhj87aOl

RESULT = VERIFIED

0 个答案:

没有答案