Paypal(沙盒/模拟器)IPN返回INVALID,尽管付款成功了吗?

时间:2016-05-27 14:00:36

标签: php paypal paypal-ipn paypal-sandbox

很抱歉不得不再问这个问题 - 但是我试过的选项都不适合我..

以下是我的听众代码:

注意我已经对ipn文件(来自paypal github)做了以下操作 - 没有这些问题我在日志文件中遇到了SSL错误。

 1. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);//1 - changed
 2. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);//2 - changed
 3. curl_setopt($ch, CURLOPT_SSLVERSION, 6); // - added
 4. added the CURL headers after default CURL options

SendBasicEmail是一个电子邮件功能,它只向我发送POST数据。

我还通过Paypal故障排除指南工作,URL正确,日志文件中的两个名称/值url段落(见下文)也完全相同..

这是一个SHared hostgator帐户 - 我没有得到任何php错误,并且我已经将沙箱的字符集设置为utf-8 ..

我正在获取IPN模拟器发送的正确值,但IPN只是说没有任何押韵或原因的INVALID ..

我的ipn.log文件包含以下内容:

[2016-05-27 08:13 America/Chicago] HTTP request of validation request:POST /cgi-bin/webscr HTTP/1.1
Accept: */*
Content-Type:application/x-www-form-urlencoded
Host: www.sandbox.paypal.com
Content-Length: 934
Connection: close

 for IPN payload: cmd=_notify-validate&payment_type=instant&payment_date=Fri+May+27+2016+15%3A08%3A35+GMT+0200+%28South+Africa+Standard+Time%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=429875949&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AoVeQZD-CT8vDxSGDZcG6pciC5eEAcQRkIs5dl-N0cnLYMm89A4qOJ7B
[2016-05-27 08:13 America/Chicago] HTTP response of validation request: HTTP/1.1 200 OK
Date: Fri, 27 May 2016 13:13:01 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Set-Cookie: c9MWDuvPtT9GIMyPc3jwol1VSlO=0g7tVdxHaY6Zg55enbaKcWevON6OjuZOp-9GlUHpUmFZPDdL2ctJ_ipwOgPQtZgsJB37HUmD6-vfYr2TmKpcq6CrX81SyThtTxD_VPiGzhTpF0velEglL5BnltyBzDVI8wzJOQgyWxio60HFFGxVd67xy6e088O7CBWNS9i64IuHjc2jAux4dD7NU1zU7kvgsDLCFjW08ibZ1gQzEU3EafhMQpg7q_q8ntofjs1SXs_a-V1GiVueQQDN5eQALgvk_T2iwg51Gv630VvftWPJSnr3N2hqsW6gshwJY3ctvkR4bs9k_kS8rWYBkTxE1zqwwmSA9MdS_izc35-mwEzD8bKp3Xlyoc1FREU4wo-fnCzun6YPRZKaCDBzvY6O9smT9-k5b4nt6pnOcCikDTbZ4r0DAJu-XaJUK9mGnG; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: cookie_check=yes; expires=Mon, 25-May-2026 13:13:01 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navcmd=_notify-validate; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: navlns=0.0; expires=Sun, 27-May-2018 13:13:01 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Set-Cookie: Apache=10.72.108.11.1464354781199222; path=/; expires=Sun, 20-May-46 13:13:01 GMT
Vary: Accept-Encoding,User-Agent
Connection: close
HTTP_X_PP_AZ_LOCATOR: sandbox.slc
Paypal-Debug-Id: 96473e862c576
Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.WEB.1%26silo_version%3D1880%26app%3Dappdispatcher%26TIME%3D3712436311%26HTTP_X_PP_AZ_LOCATOR%3Dsandbox.slc; Expires=Fri, 27 May 2016 13:43:01 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
[2016-05-27 08:13 America/Chicago] Invalid IPN: cmd=_notify-validate&payment_type=instant&payment_date=Fri+May+27+2016+15%3A08%3A35+GMT+0200+%28South+Africa+Standard+Time%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=429875949&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AoVeQZD-CT8vDxSGDZcG6pciC5eEAcQRkIs5dl-N0cnLYMm89A4qOJ7B

下面是我的ipn监听器文件:

//Begin IPN FILE
// CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
            // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
            // Set this to 0 once you go live or don't require logging.
            define("DEBUG", 1);
            // Set to 0 once you're ready to go live
            define("USE_SANDBOX", 1);
            define("LOG_FILE", "./ipn.log");
            // Read POST data
            // reading posted data directly from $_POST causes serialization
            // issues with array data in POST. Reading raw POST data from input stream instead.
            $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]);
            }
            // read the post from PayPal system and add 'cmd'
            $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";
            }
            // Post IPN data back to PayPal to validate the IPN data is genuine
            // Without this step anyone can fake IPN data
            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, 0);//1
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);//2
            curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
            curl_setopt($ch, CURLOPT_SSLVERSION, 6);   



             $headers= array('POST /cgi-bin/webscr HTTP/1.0',
                          'Content-Type:application/x-www-form-urlencoded',
                          'Host: www.sandbox.paypal.com',
                          "Content-Length: " . strlen($req),
                            'Connection: close'
                        );
                          curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);



        if(DEBUG == true) {
          curl_setopt($ch, CURLOPT_HEADER, 1);
          curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
        }
        // CONFIG: Optional proxy configuration
        //curl_setopt($ch, CURLOPT_PROXY, $proxy);
        //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
        // Set TCP timeout to 30 seconds
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

        #curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));




        // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
        // of the certificate as shown below. Ensure the file is readable by the webserver.
        // This is mandatory for some environments.
        //$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(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);
        }

        SendBasicEmail("fran@strike1.co.za","res",strcmp (var_dump($raw_post_array), "VERIFIED"));

        // Inspect IPN validation result and act accordingly
        // Split response headers and payload, a better way for strcmp
        $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'];

            SendBasicEmail("fran@strike1.co.za", "IPN DATA - Pass", "$ipnrows");

          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) {
          // log for manual investigation
          // Add business logic here which deals with invalid IPN messages

            SendBasicEmail("fran@strike1.co.za", "IPN DATA - Fail", "$ipnrows");


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

//END IPN FILE

1 个答案:

答案 0 :(得分:0)

终于找到了问题!!

在成群结队的其他民族帖子中徘徊几天相同的问题后 - 我发现很多人使用preg_replace从paypal post变量值中删除url编码字符(%)..

假设你已经仔细检查过你没有php语法错误而获取运行ipn模拟器时创建的php error.log 文件 - 请查看ipn.log文件(或自己发送电子邮件来自ipn监听器的$ req的输出)...

mail("*email@what.where*","Ipn Res", curl_getinfo($ch, CURLINFO_HEADER_OUT) .$res );

查找以% -

开头的所有字符

看看上面的例子:

payment_date=Fri+May+27+2016+15**%3A**08**%3**A35+GMT+0200+

默认的IPN模拟器添加了大量的垃圾时区垃圾,导致各种%(url编码)值失败了我的ipn。

(注意我已经在帐户设置中将我的编码更改为utf-8)

我用普通的php日期替换了默认的IPN日期字段,并且一切正常 - 最终!

所以 - 确保你没有在帖子网址中获得任何%值,一切都应该是好的!

另外 - 我必须更改这些选项以通过:

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);//1 - changed
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);//2 - changed
  curl_setopt($ch, CURLOPT_SSLVERSION, 6); // - added