2013-08-23 154 views
0

我有一個PHP腳本,我已經使用了2年,並修改它的工作時,貝寶更改爲http 1.1。它幾乎適用於每一個交易,但最近失敗了,我不明白爲什麼。以下是代碼。PHP Paypal IPN失敗有時

嘗試查看響應時失敗。它是無效的

它可能與地址中的非標準字符有關嗎?

我已經試過一次又一次發送IPN請求,它總是失敗

function paypal_ipn() { 
    $req = 'cmd=_notify-validate'; 

    foreach($_POST as $key => $value) { 
     $value = urlencode(stripslashes($value)); 
     $req .= "&{$key}={$value}"; 
    } 

    $res = ''; 
    $ch = curl_init(paypal_url()); 
    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); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); 
    $res = curl_exec($ch); 
    curl_close($ch); 

    if(PAYPAL_IPN_DEBUG && !empty($_POST)) { 
     $fname = 'ipn_intial_' . date('Y.m.d-h.i.s', time()) . '.txt'; 
     file_put_contents('log/' . $fname, $req); 

     $fname = 'ipn_intial_result_' . date('Y.m.d-h.i.s', time()) . '.txt'; 
     file_put_contents('log/' . $fname, $res); 

    } 

    if(strcmp($res, "VERIFIED") == 0) { 

     switch($_POST['txn_type']) 
{ 
     case 'web_accept': 
      $data = array(); 
      $data['name'] = $_POST['first_name'] . ' ' . $_POST['last_name']; 
      $data['email'] = $_POST['payer_email']; 
      $data['txn_id'] = $_POST['txn_id']; 
      $data['payment_status'] = 'Completed'; 
      $res = save_payment($data); 
      if ($res) { 
       $data = payment_details(array('txn_id' => $data['txn_id'])); 
       mail_notification($data); 
      }    
      break; 

     case 'subscr_signup': 
      $params = array(); 
      $params['name'] = $_POST['first_name'] . ' ' . $_POST['last_name']; 
      $params['email'] = $_POST['payer_email']; 
      $params['subscr_id'] = $_POST['subscr_id']; 
      $params['subscr_status'] = 'payment'; 

      $data = save_subscription($params); 
      subscr_notification($data); 
      break; 
     case 'subscr_cancel': 
      $data = subscr_details(array('subscr_id' => $_POST['subscr_id'])); 

      if ($data) 
      { 
       mysql_update('subscriptions', array('subscr_id' => $_POST['subscr_id']), array('subscr_status' => 'cancelled', 'cancel_date' => date('Y-m-d H:i:s'))); 
      } 
      break; 
     } 

    } 

    if(PAYPAL_IPN_DEBUG && !empty($_POST)) { 
     $fname = 'ipn_' . date('Y.m.d-h.i.s', time()) . '.txt'; 
     file_put_contents('log/' . $fname, serialize($_POST)); 
    } 
} 

回答

0

這是一個棘手的一個。魔術引號已關閉,此腳本假定魔術引號處於打開狀態。因此沒有必要調用stripslashes這導致錯誤