2012-10-10 23 views
1

嗨,這裏一直試圖讓這個工作大約2天。支付是通過沒有問題去,但它無法更新數據庫:(貝寶IPN幫助(不更新數據庫)

這裏是代碼:

<?php 

require_once(WWW_DIR."/lib/users.php"); 


      // Connect to database 
$con = mysql_connect("localhost","root","******"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

// 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.sandbox.paypal.com', 443, $errno, $errstr, 30); // Test 
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live 
// assign posted variables to local variables 
$item_name = $_POST['item_name']; 
$payment_status = $_POST['payment_status']; 
$payment_amount = $_POST['mc_gross']; 
$txn_id = $_POST['txn_id']; 
$payer_email = $_POST['payer_email']; 
$name = $_POST['first_name'] . " " . $_POST['last_name']; 
$address_street = $_POST['address_street']; 
$address_zip = $_POST['address_zip']; 
$address_city = $_POST['address_city']; 
$contact_phone = $_POST['contact_phone']; 
$email = $_POST['payer_email']; 
$uid = $users->currentUserId(); 
//timezone set 
date_default_timezone_set('Europe/London'); 

if (!$fp) 
{ 
    // HTTP ERROR 
} else 
{ 
    fputs($fp, $header . $req); 
    while (!feof($fp)) 
    { 
     $res = fgets($fp, 1024); 
     if (strcmp($res, "VERIFIED") == 0) { 
if($payment_status != "Completed"){ 
       if($payment_amount == 2.00) 
       { 
        $role = 6; 
       } 
       else 
       if($payment_amount == 5.00) 
       { 
        $role = 8; 
       } 
       else 
       if($payment_amount == 8.00) 
       { 
        $role = 9; 
       } 

       //update user records 
$sql = sprintf("UPDATE users SET role = %d WHERE ID = %d", $role, $uid); 
mysql_query($sql); 


//log payment 
      //mysql_query("INSERT INTO payments (email, item_name, payment_status, txn_id, payment_ammount) VALUES('" . mysql_escape_string($email) . "', '" . $item_name . "', '" . $payment_status . "', '" . $txn_id . "', '" . $payment_amount . "') ") or die(mysql_error()); 
     }} else 
      if (strcmp($res, "INVALID") == 0) 
      { 
       // log for manual investigation 
      } 
    } 
    fclose($fp); 
} 
?> 

如果您有任何問題,只是問我會嘗試儘快回答,如果你能得到這個工作我會很高興

感謝

回答

0

變化:

// 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"; 

要:

// post back to PayPal system to validate 
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n"; 
$header .= "Host: www.sandbox.paypal.com\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: " . strlen($req) . "\r\n"; 
$header .= "Connection: close\r\n\r\n"; 

變化:

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

要:

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

最後,變化:

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

要:

if (strcmp(trim($res), "INVALID") == 0) 

原因:
貝寶沙箱被配置爲拒絕未指定主機頭的HTTP 1.0請求,因此您的當前腳本永遠不會被正確驗證。