2010-04-16 109 views
2

我使用PayPal'現在購買'按鈕在我的網站上銷售產品。由於我跟蹤了MySQL數據庫中每個產品的庫存單位數量,我希望系統中的庫存跟蹤能夠實現自動化,因此我使用PayPal的即時付款通知功能讓我知道何時進行了購買完成。當Paypal通知我的處理程序已經進行了有效購買時,腳本通過從購買的產品庫存中減去「1」來更新我的MySQL數據庫。PayPal IPN處理程序的購物車

我已經在下面附上了我的IPN PHP代碼,可以成功使用Paypal現在購買它的按鈕(一次購買一個)。

我的問題是:我想用PayPal的「添加到購物車」按鈕替代「立即購買」按鈕,以便客戶一次可以購買多個產品。我不確定如何改變我的代碼,讓它遍歷所有購買的項目並相應地更新我的數據庫。任何幫助將不勝感激!

驗證碼:

// Paypal POSTs HTML FORM variables to this page 
// we must post all the variables back to paypal exactly unchanged and add an extra parameter cmd with value _notify-validate 

// initialise a variable with the requried cmd parameter 
$req = 'cmd=_notify-validate'; 

// go through each of the POSTed vars and add them to the variable 
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"; 

// In a live application send it back to www.paypal.com 
// but during development you will want to uswe the paypal sandbox 

// comment out one of the following lines 

$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30); 
//$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30); 

// or use port 443 for an SSL connection 
//$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) { 


     $item_name = stripslashes($_POST['item_name']); 
     $item_number = $_POST['item_number']; 
     $item_id = $_POST['custom']; 
     $payment_status = $_POST['payment_status']; 
     $payment_amount = $_POST['mc_gross'];   //full amount of payment. payment_gross in US 
     $payment_currency = $_POST['mc_currency']; 
     $txn_id = $_POST['txn_id'];     //unique transaction id 
     $receiver_email = $_POST['receiver_email']; 
     $payer_email = $_POST['payer_email']; 
     $size = $_POST['option_selection1']; 
     $item_id = $_POST['item_id']; 
     $business = $_POST['business']; 



     if ($payment_status == 'Completed') { 
// UPDATE THE DATABASE  


     } 

回答

0

目前尚不清楚你的要求。我爲網站付款專業版編寫了PayPal IPN監聽器。我從這裏開始尋找PayPal Documentation: IPN Sample Code,然後根據需要更改代碼。請爲您的問題添加更多詳細信息。

+0

非常感謝Christopher的回覆!我稍微改變了我的問題,希望能更清楚一點。我看了一下你提供的鏈接,它看起來非常有用......但是,也許我對它的理解錯了,這個腳本一次只能處理一個項目,對嗎?如果這是購物車(PayPal的「添加到購物車」按鈕)中的4個不同物品的結賬?我想知道爲了適應這種情況需要更改哪些代碼。 – kimmothy16 2010-04-16 03:09:16

1

您可能更容易將所有物品ID收集到訂單ID中,然後將該訂單ID與您的POST發送到PayPal。一旦IPN返回您的詳細信息,請檢查並計算訂單ID中所有項目的總金額與IPN表示支付的金額相匹配。

查詢您的數據庫中的訂單ID,並獲取所有與物品ID相關的物品ID,然後減少每個物品ID的庫存號。

希望它有幫助。這只是一種做法!