2012-03-05 62 views
2

跟進my previous newbie question about Paypal,我有一個新的新手問題。我收到PayPal IPN時應該在哪裏執行UPDATE查詢?

我成功地獲得了一個PayPal沙盒設置,並在我的服務器上實現了一個PHP「偵聽器」,當我測試我的Paypal付款按鈕時,一切似乎都正常工作。所以看起來我擁有支付系統的基本骨架。

現在我對如何把它定製到我的需要的下一步感到有點困惑。我要複製the code offered on Paypal's site。我的希望和假設是我可以在Paypal告訴我支付已經到來的時候在代碼中找到一個位置。

但是,我不清楚這一點在哪裏。如果我根據付費或未付款設置某種「if」聲明,我看不出我將測試的價值。

我想要做的就是如果貝寶告訴我該人已經付款,那麼我的MySQL數據庫中的一個標誌將被標記爲「已付款」。它應該是定期付款,所以如果下一次自動付款失敗,那麼數據庫中的標誌應該標記爲「未付款」。

在他們的代碼中是否有地方可以用作添加自定義操作的起點?還是我需要完全使用其他代碼庫?

供參考,在這裏是有問題的PHP代碼:

<?php 

error_reporting(E_ALL^E_NOTICE); 
$email = $_GET['ipn_email']; 
$header = ""; 
$emailtext = ""; 
// Read the post from PayPal and add 'cmd' 
$req = 'cmd=_notify-validate'; 
if (function_exists('get_magic_quotes_gpc')) 
{ 
    $get_magic_quotes_exists = true; 
} 
foreach ($_POST as $key => $value) 
// Handle escape characters, which depends on setting of magic quotes 
{ 
    if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) 
    { 
     $value = urlencode(stripslashes($value)); 
    } 
    else 
    { 
     $value = urlencode($value); 
    } 
    $req .= "&$key=$value"; 
} 
// Post back to PayPal 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); 


// Process validation from PayPal 
// TODO: This sample does not test the HTTP response code. All 
// HTTP response codes must be handles or you should use an HTTP 
// library, such as cUrl 

if (!$fp) 
{ // HTTP ERROR 
} 
else 
{ 
// NO HTTP ERROR 
    fputs($fp, $header . $req); 
    while (!feof($fp)) 
    { 
     $res = fgets($fp, 1024); 
     if (strcmp($res, "VERIFIED") == 0) 
     { 
      // TODO: 
      // Check the payment_status is Completed 
      // Check that txn_id has not been previously processed 
      // Check that receiver_email is your Primary PayPal email 
      // Check that payment_amount/payment_currency are correct 
      // Process payment 
      // If 'VERIFIED', send an email of IPN variables and values to the 
      // specified email address 
      foreach ($_POST as $key => $value) 
      { 
       $emailtext .= $key . " = " . $value . "\n\n"; 
      } 
      mail($email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req); 
     } 
     else if (strcmp($res, "INVALID") == 0) 
     { 
      // If 'INVALID', send an email. TODO: Log for manual investigation. 
      foreach ($_POST as $key => $value) 
      { 
       $emailtext .= $key . " = " . $value . "\n\n"; 
      } 
      mail($email, "Live-INVALID IPN", $emailtext . "\n\n" . $req); 
     } 
    } 
    fclose($fp); 
} 
?> 

回答

2

您必須更進一步並實施TODO步驟。

現在,PayPal有一個名爲item_number的標準傳遞字段。您可以使用該字段來傳遞你的數據庫的訂單ID:

<input type="hidden" name="item_number" value="<?php echo $OrderID ?>"> 

既然是傳遞變量,PayPal將回顯在此IPN變量的值。所以你驗證了IPN,並通過待辦列表走後,更新數據庫:

// if ($_POST["payment_status"] == "Completed" 
// && $_POST["receiver_email"] == the_email_you_use_in_business_field 
// && etc etc 
//) 
"UPDATE Orders SET paid = 1 WHERE OrderID = {$_POST[item_number]}" 

驗證和數據庫查詢使用前消毒POST變量。

編輯

我現在看到你要處理重複接收這些變量付款。不像看起來那樣直截了當:

  • 仔細閱讀變量引用。您可以使用幾個字段來識別訂閱(自定義,發票,item_number)。
  • 您將在多個訂閱階段收到多個IPN,包括訂閱本身,第一次付款,後續付款以及訂閱被終止的時間。
  • 每種付款更改都會通過的變量。您必須徹底查閱文件以確定您應該爲每種IPN類型檢查哪些字段。
2

將你的數據庫相關的代碼裏面:

 

if (strcmp($res, "VERIFIED") == 0) { 
//your database query here 
 
2

你有這樣的評論:

> // TODO: 
>    // Check the payment_status is Completed 
>    // Check that txn_id has not been previously processed 
>    // Check that receiver_email is your Primary PayPal email 
>    // Check that payment_amount/payment_currency are correct 
>    // Process payment 
>    // If 'VERIFIED', send an email of IPN variables and values to the 
>    // specified email address 

把你的代碼在這裏:

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

    ... 

    } 
相關問題