2011-08-20 53 views
0

我使用貝寶Sandbox來測試IPN,這是成功的,但它並沒有更新我的MYSQL數據庫。我如何更改下面的代碼,以便當PayPal發送IPN到我的網站它更新MySQL數據庫?下面的代碼是paypalipn.php貝寶sanbox IPN和mysql的幫助

// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 
foreach ($_POST as $key => $value) { 
$value = urlencode(stripslashes($value)); 
$req .= "&$key=$value"; 
} 
// post back to PayPal system to validate 
$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 ('ssl://www.paypal.com', 443, $errno, $errstr, 30); 

if (!$fp) { 
// HTTP ERROR 
} else { 
fputs ($fp, $header . $req); 
while (!feof($fp)) { 
$res = fgets ($fp, 1024); 
if (strcmp ($res, "VERIFIED") == 0) { 

// PAYMENT VALIDATED & VERIFIED! 
$email = $_POST['payer_email']; 
$email = mysql_escape_string($email); 
$voted = mysql_query("INSERT INTO user VALUES ('','','','','','','','','','','','','','',''")or die(mysql_error()); 
mysql_query("UPDATE users SET `suscribed`=1 WHERE `email`='$email'")or die(mysql_error()); 

} 

else if (strcmp ($res, "INVALID") == 0) { 

// PAYMENT INVALID & INVESTIGATE MANUALY! 


} 
} 
fclose ($fp); 
} 
+0

爲什麼你有一個空的插入語句?你確定沒有mysql錯誤嗎? – Shoan

+0

當我在PayPal沙箱中測試它說'IPN成功發送'。插入語句只是我添加的一個空行來測試它是否工作,事實並非如此。 – user892134

+0

假設你的查詢工作正常,你是否檢查過'fgets'返回值爲'VERIFIED'的行? – Josh

回答

0

首先總是使誤差發展時error_reporting(E_ALL)報告,再加上(顯然在安全的地方)記錄的IPN的文本文件來引用,看看是否正在接收的實際IPN的&通過路由器ECT

乍一看我看到你的努力在user表中插入一個空白記錄得到,也沒有添加閉括號)的聲明。

那麼你的更新不同的表格users有可能一個錯字:suscribed,不要使用已棄用mysql_escape_string功能... mysql_real_escape_string應改爲使用,或者更好的使用準備好的語句。

編輯: 一個簡單的例子,你可以從這裏,包括PDO和記錄IPN。希望能幫助到你。

<?php 
/**Simple Paypal validation class**/ 
class paypal_class { 

    var $last_error; 
    var $ipn_log; 
    var $ipn_log_file; 
    var $ipn_response; 
    var $ipn_data = array(); 

    function paypal_class() { 
     $this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr'; 
     $this->last_error = ''; 
     $this->ipn_response = ''; 
     $this->ipn_log_file = 'ipn_results.log'; 
     $this->ipn_log = true; 
    } 

    function validate_ipn(){ 
     $url_parsed=parse_url($this->paypal_url); 
     $post_string = ''; 
     foreach($_POST as $field=>$value){ 
      $this->ipn_data["$field"] = $value; 
      $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; 
     } 
     $post_string.="cmd=_notify-validate"; 

     $fp = fsockopen($url_parsed[host],"80",$err_num,$err_str,30); 
     if(!$fp){ 
      $this->last_error = "fsockopen error no. $errnum: $errstr"; 
      $this->log_ipn_results(false); 
      return false; 
     }else{ 
      // Post the data back to paypal 
      fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n"); 
      fputs($fp, "Host: $url_parsed[host]\r\n"); 
      fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
      fputs($fp, "Content-length: ".strlen($post_string)."\r\n"); 
      fputs($fp, "Connection: close\r\n\r\n"); 
      fputs($fp, $post_string . "\r\n\r\n"); 

      while(!feof($fp)){ 
       $this->ipn_response .= fgets($fp, 1024); 
      } 
      fclose($fp); 
     } 
     if(eregi("VERIFIED",$this->ipn_response)){ 
      $this->ipn_log(true); 
      return true; 
     }else{ 
      $this->last_error = 'IPN Validation Failed.'; 
      $this->ipn_log(false); 
      return false; 
     } 
    } 

    function ipn_log($success){ 
     if (!$this->ipn_log) return; 
     $text = '['.date('m/d/Y g:i A').'] - '; 
     if ($success) $text .= "SUCCESS!\n"; 
     else $text .= 'FAIL: '.$this->last_error."\n"; 
     $text .= "IPN POST Vars from Paypal:\n"; 
     foreach ($this->ipn_data as $key=>$value) { 
      $text .= "$key=$value, "; 
     } 
     $text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response; 
     $fp=fopen($this->ipn_log_file,'a'); 
     fwrite($fp, $text . "\n\n"); 
     fclose($fp); 
    } 
} 



class database{ 
    /**PDO Connect**/ 
    public function connect($host,$db,$user,$pass){ 
     $this->dbh = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass); 
    } 
    /**Pre Query for prepared statement**/ 
    public function update_valid($email){ 
     $this->value = $email; 
     $this->prepare(); 
    } 
    /**Delete pending user, when user clicks cancel @ paypal**/ 
    public function delete_pending($email){ 
     $this->result = $this->dbh->prepare('DELETE FROM users where email=":value" and subscribed=0'); 
     $this->result->bindParam(':value', $email); 
     $this->execute(); 
    } 

    /**Prepare query for insert**/ 
    private function prepare(){ 
     /* Execute a prepared statement by binding PHP variables */ 
     $this->result = $this->dbh->prepare('UPDATE users SET subscribed=1 WHERE email=":value"'); 
     $this->result->bindParam(':value', $this->value); 
     $this->execute(); 
    } 

    /**Execute prepared statement**/ 
    private function execute(){ 
     $this->result->execute(); 
    } 
    /**Close db**/ 
    public function close(){ 
     $this->result = null; 
    } 
} 


?> 


<?php 
//Handle payment (Set You IPN url too http://yoursite.com?payment=ipn & Cancel url to http://yoursite.com?payment=cancel) 
if(isset($_GET['payment'])){ 

    switch ($_GET['payment']) { 
     case 'cancel': 
      //Order Cancelled 
      $db=new database(); 
      $db->connect('localhost','table','root','password'); 
      $db->delete_pending($_SESSION['email']); //hold email in session after submitting form 
      $db->close(); 
      header('Location: index.php'); 
      die(); 
      break; 

     case 'ipn': 
      $pp = new paypal_class; 

      if ($pp->validate_ipn()){ 
       //Success 
       $db=new database(); 
       $db->connect('localhost','table','root','password'); 
       $db->update_valid($ipn['payer_email']); 
       $db->close(); 
      } 
      die(); 
      break; 
    } 
} 
?> 
+0

謝謝我解決了這兩個問題,它仍然說'IPN已成功發送',但數據庫中沒有任何反應。 IPN流程由Paypal隱藏,因此在沙箱中測試時我會如何看到任何錯誤? – user892134