【问题标题】:Paypal (sandbox/simulator) IPN returning INVALID, despite payment being successful?Paypal(沙盒/模拟器)IPN返回无效,尽管支付成功?
【发布时间】:2016-09-25 21:48:00
【问题描述】:

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

下面是我的监听代码:

请注意,我已对 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 段落(见下文)也完全相同..

这是在一个共享的 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

【问题讨论】:

    标签: php paypal paypal-ipn paypal-sandbox


    【解决方案1】:

    终于找到问题了!!

    几天来在其他人的大量帖子中遇到相同问题后,我发现许多人正在使用 preg_replace 从 paypal 帖子变量值中删除 url 编码字符 (%)..

    假设你已经仔细检查过你没有 php 语法错误并且 not 在运行 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
    

    【讨论】:

    • "我用常规的 php 日期替换了默认的 IPN 日期字段,一切正常。"你是怎么做到的?
    • 对不起,我的解释不好。在 IPN 模拟器中,日期字段不断中断流程。删除所有 gmt 内容并仅使用 mysql 日期格式后我的工作..不确定这是否是一个错误但解决了我的问题..
    猜你喜欢
    • 2010-12-01
    • 2018-08-14
    • 2013-05-19
    • 2015-08-28
    • 2016-10-27
    • 2016-03-29
    • 2018-07-31
    • 2013-07-17
    • 2014-10-20
    相关资源
    最近更新 更多