Paypal沙箱notify_url IPN无法正常工作

时间:2014-08-05 22:05:27

标签: php paypal paypal-ipn sandbox

我正在尝试使用PHP通过notify_url将变量发送到我的IPN处理程序。 实时/正常的paypal交易工作得很好,并将交易信息发送给我的paypal处理程序,但是当我发送沙盒(Paypal API)交易进行测试时,它不起作用。 我的paypal沙盒帐户中的IPN已启用。

这是为每次购买生成网址的代码:

$paypalurl = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&amount=".urlencode($row['price'])."&business=".urlencode($paypalemail)."&item_name=".urlencode($row['name'])."&item_number=".urlencode($row['ID']."_".$_SESSION['ID'])."&return=http://MYURL.MYURL.com/purchase.php"."&rm=2&notify_url=http://MYURL.MYURL/paypalhandler.php"."&cancel_return=http://MYURL.MYURL.com/purchase.php"."&no_note=1&currency_code=USD";

paypal IPN处理程序:

define("_VALID_PHP", true);
                  file_put_contents("log.txt", print_r($GLOBALS, true)."\r\n", FILE_APPEND);
  if (isset($_POST['payment_status'])) {
      require_once("../includes/db.php");
      require_once("../includes/init.php");
      set_time_limit(0);      

      function verifyTxnId($txn_id, $odb)
      {          
          $sql = $odb->prepare("SELECT COUNT(id) FROM `payments` WHERE tid = :tid LIMIT 1");
      $sql -> execute(array(":tid" => $txn_id));    
          if ($sql -> fetchColumn(0) > 0)
              return false;
          else
              return true;
      }

      $req = 'cmd=_notify-validate';

      foreach ($_POST as $key => $value) {
          $value = urlencode(stripslashes($value));
          $req .= '&' . $key . '=' . $value;
      }
      $url = 'www.sandbox.paypal.com';

      $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";
      $fp = fsockopen($url, 80, $errno, $errstr, 30);

      $payment_status = $_POST['payment_status'];
      $receiver_email = $_POST['business'];
      list($membership_id, $user_id) = explode("_", $_POST['item_number']);
      $mc_gross = $_POST['mc_gross'];
      $txn_id = $_POST['txn_id'];

      $getxn_id = verifyTxnId($txn_id, $odb);
      $pricesql = $odb -> prepare("SELECT `price` FROM `plans` WHERE id = :id");
      $pricesql -> execute(array(":id" => (int)$membership_id));
      $price = $pricesql -> fetchColumn(0);

      $pp_emailsql = $odb -> query("SELECT `email` FROM `gateway` LIMIT 1");
      $pp_email = $pp_emailsql->fetchColumn(0);

      if (!$fp) {
          echo $errstr . ' (' . $errno . ')';
      } else {
          fputs($fp, $header . $req);

          while (!feof($fp)) {
          $res = fgets($fp, 1024);
              if (strcmp($res, "VERIFIED") == 0) {
                  if (preg_match('/Completed/', $payment_status)) {
                      if ($receiver_email == $pp_email && $mc_gross == $price && $getxn_id == true) {


              $data = array(
                ':tid' => $txn_id, 
                ':plan' => (int)$membership_id,
                ':email' => $_POST['payer_email'],
                ':user' => (int)$user_id,
                ':paid' => (float)$mc_gross, 
              );
              $odb -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
              $insertsql = $odb -> prepare("INSERT INTO `payments` VALUES(NULL, :paid, :plan, :user, :email, :tid, UNIX_TIMESTAMP())");
              $insertsql -> execute($data);

              $getPlanInfo = $odb -> prepare("SELECT `unit`,`length` FROM `plans` WHERE `ID` = :plan");
                          $getPlanInfo -> execute(array(':plan' => (int)$membership_id));
                          $plan = $getPlanInfo -> fetch(PDO::FETCH_ASSOC);
                          $unit = $plan['unit'];
                          $length = $plan['length'];
                          $newExpire = strtotime("+{$length} {$unit}");
                          $updateSQL = $odb -> prepare("UPDATE `users` SET `expire` = :expire, `membership` = :plan WHERE `id` = :id");
                          $updateSQL -> execute(array(':expire' => $newExpire, ':plan' => (int)$membership_id, ':id' => (int)$user_id));

                }                    
                  }

              }
          }
          fclose($fp);
      }
  }

0 个答案:

没有答案