2013-12-17 28 views
0

由於某種原因,我有更多的麻煩,然後我應該...我有一個IPN列表爲PayPal和IPN模擬器說成功每次和不同的方法,但我可以不要讓它根據成功的響應來操縱數據庫。IPN模擬器成功但不運行數據庫操作

任何想法的人?

<?php 

//INCLUDE CONNECTION STRING 
include('connect.php'); 

// STEP 1: read POST data 

// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST. 
// Instead, read raw POST data from the input stream. 
$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 IPN message sent from PayPal and prepend 'cmd=_notify-validate' 
$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"; 
} 


// STEP 2: POST IPN data back to PayPal to validate 

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr'); 
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')); 

if(!($res = curl_exec($ch))) { 

error_log("Got " . curl_error($ch) . " when processing IPN data"); 
curl_close($ch); 
exit; 
} 
curl_close($ch); 


// STEP 3: Inspect IPN validation result and act accordingly 

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

// 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']; 

if($payment_status=="Completed"){ 

    $selectuser = mssql_query("select statsmemberid from statsmembers where email='$payer_email'"); 

      if(mssql_num_rows($selectuser) != 0){ 
      $row = mssql_fetch_row($selectuser); 
      $statsmemberid = $row[0]; 

      $getCredits = mssql_query("select creditsbought from statsmuplayers where statsmemberid='$statsmemberid'"); 

      $row = mssql_fetch_row($getCredits); 
      $totalCredits = $row[0]+11;    

      $updatemu = mssql_query("update statsmuplayers set creditsbought='$totalCredits' where statsmemberid='$statsmemberid'"); 
      echo "Credits Applyed"; 
      }else{ 
       echo "Invalid Email"; 
      } 




} 


} else if (strcmp ($res, "INVALID") == 0) { 
// IPN invalid, log for manual investigation 
echo "The response from IPN was: <b>" .$res ."</b>"; 
} 
?> 
+0

由於在這裏閱讀echo'es非常困難,因爲貝寶在後臺調用腳本,您應該將任何輸出重定向到文件。例如$ f = fopen(「log.txt」,「a」); - 然後fwrite($ f,「我想寫/記錄...」); - 這應該可以幫助你調試它。同時檢查Apache/NGINX日誌查找可能的錯誤/警告。 –

回答

0

我建議你將所有這些代碼封裝到幾個對象中。這真的會幫助你找出事情出錯的地方。

你要處理與貝寶的IPN通信(你不需要寫從無到有這裏的第一個PHP實現我在谷歌搜索https://github.com/dodev34/paypal-ipn-response-client發現)

然後你想有一個基本對象的對象,處理您的數據庫連接。最後,您需要一個擴展數據庫連接對象並強制執行業務邏輯的statsmembers對象。您可能會提取一些PHP ORM代碼,如您在這裏看到的http://www.phpactiverecord.org/projects/main/wiki/Quick_Start

這將允許您單獨測試與實際的PayPal IPN通信分開的更新功能。好消息是,在這一點上,您不必依賴寫入系統文件進行調試,就像上面評論中提到的某人一樣。

我沒有在代碼中看到具體的錯誤,只是一目瞭然對不起,你確定你通過PayPal正確傳遞payer_email?

相關問題