沙箱/贝宝阶段超级慢。大约20秒响应我的IPN处理程序。现在根本没有回应

时间:2016-05-25 01:13:57

标签: php paypal

i took the example implementation of the IPN handler from paypals official website

现在这在一开始就非常有效。但答案越来越慢。有一段时间他们工作得很好,延迟20秒。现在我几乎没有任何流量。到底是怎么回事?也许是paypal的沙盒ipn处理程序本身接近下降?他们的设置不好?

I want to mention this post(它有点明白)

注意我甚至没有初始日志。贝宝似乎根本不再打电话给我。

IPN是可调用的,我可以从隐身窗口调用它,它会写入日志文件。

为了完整性,这里是我的文件:

<?php

    use ...;
    // 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");
    require_once(__DIR__ . "/../../bootstrap.php");



    //define("LOG_FILE", __DIR__ . "/../../../../../japi/logs/ipn.log");
    error_log(date('[Y-m-d H:i e] '). "Lukas: Initial call to log". PHP_EOL, 3, LOG_FILE);
    // 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]);
    }
    //$mmessage ="hi lukas <br/>";
    //
    //    include_once JCR_KINT_CLASS;
    //$mmessage .= @\Kint::dump($myPost);
    //mail("lukas.meier@gmail.com", "IPN from paypal test", $mmessage);
    // 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, 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);
    }
    // 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);
    }
    // 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'];


      $jppa = new JcrPayPalAnswer();
      $jppa->createFromPayPalIpnResponseArray($myPost);

      if($jppa->is_completed){
        //content of file: paykey => array($project_id, serialize($fd), $statisticsId);
        $r = parse_ini_file(JCR_PAYKEY_INIFILE);
        if($r){
          $pkArr = $r[$jppa->pay_key];
          $project_id = $pkArr[0];
          /** @var FundingDetails $fd */
          $fd = unserialize(base64_decode($pkArr[1]));
          $statisticsId = $pkArr[2];
          $jcrp = new JewcerProject($project_id);
          $jewcerFee = $jcrppa->amount_fee_account;
          $fundingAmount = $jcrppa->amount_funding_account_brutto;

          $x = null;
          $js = new JcrStatistic($x, $statisticsId);

          //fna [$amount, $paypalfee, $jewcerFeeAmount]
          $fnA = JcrPayPalService::getFeesAndAmount($fd->amount, $fd->coverfee, $jcrp->getFundingFee());
          $amount = $fnA[0];
          $paypalfee = $fnA[3];
          $jewcerFeeAmount = $fnA[2];

          $fd->wepayFee = $paypalfee;
          $fd->jcrFee = $jewcerFeeAmount;
          $amount_with_fee = $amount;
          if ($fd->coverfee) {
            $fd->amount_without_fees = $amount - $paypalfee - $jewcerFeeAmount;
          } else {
            $fd->amount_without_fees = $fd->amount;
          }
          $fd->amount = $amount_with_fee;
          $jcrf = new JcrFunder($project_id);
          $jcrf->setBasicFunderValues($fd);
          $jcrf->save();
          $js->add_stats_from_fundingdetails($fd, "jfp3");
          EmailService::sendDonationSuccessEmails($jcrp, $fd);

          unset($r[$jppa->pay_key]);
          UtilityService::write_ini_file($r, JCR_PAYKEY_INIFILE);
          UtilityService::write_ini_file(array('ok',  $jcrf->id ), JCR_PAYPAL_STATUS_FOLDER . $jppa->pay_key);
        }else{
          error_log(date('[Y-m-d H:i e] '). "JEWCER ERROR 3200: couldn't find entry for paykey in inifile, inifile val: " . var_export($r, true). PHP_EOL, 3, LOG_FILE);
        }
    //    mail("lukas.meier@gmail.com", "IPN COMPLETED", serialize($jppa));
    //    JcrPayPalKeyPool::$keys[$jppa->pay_key] = $jppa; //eventually verify emails
    //    JcrPayPalKeyPool::$keys[$jppa->pay_key] = $jppa; //eventually verify emails

      }

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

1 个答案:

答案 0 :(得分:0)

我多年来一直在使用IPN进行各种各样的项目。根据我的经验,如果一切都配置正确并且您的IPN脚本没有问题,那么它将非常接近实时工作而没有多少打嗝。

但是,您可能遇到的问题是,如果您的IPN脚本因任何原因失败,或者只是在PayPal被击中时向其发送200以外的响应代码,它将重新尝试,但每次重试时它延迟了它发送的时间。因此它可能会立即发送一个,如果它不提供200响应,它将在5秒,10秒,20秒,40秒等时发送另一个(这不是它使用的确切增量,但它是一个示例它做了什么。)

如果您的脚本在某个时刻没有返回200响应,或者如果发送到您的脚本的IPN发生了很多,那么PayPal的系统会将您的IPN移动到比正在运行的其他IPN更慢的队列好。

最终,如果它仍未修复,则只会在您的帐户中完全禁用它。

检查您的PayPal帐户中的IPN历史记录,以查看您在收到的IPN上收到的回复。当然,这将允许您验证它们确实也被发送了。

您还需要检查服务器上的PHP错误日志,以查看IPN脚本被点击时是否发生任何事情,导致其因任何原因而失败。例如,只有某些IPN类型或数据中包含特定字符时才会发生这种情况。