2013-03-07 57 views
0

我期待能夠從PayPal IPN帖子中獲取信息,並使用特定項目更新我的數據庫。這是當前代碼我有我的ipn.php如何從PayPal IPN添加結果以更新數據庫

<?php 
// tell PHP to log errors to ipn_errors.log in this directory 
ini_set('log_errors', true); 
ini_set('error_log', dirname(__FILE__).'/ipn_errors.log'); 

// intantiate the IPN listener 
include('ipnlistener.php'); 
$listener = new IpnListener(); 

// tell the IPN listener to use the PayPal test sandbox 
$listener->use_sandbox = true; 

// try to process the IPN POST 
try { 
$listener->requirePostMethod(); 
    $verified = $listener->processIpn(); 
} catch (Exception $e) { 
    error_log($e->getMessage()); 
    exit(0); 
} 

if ($verified) { 

$errmsg = ''; // stores errors from fraud checks 

// 1. Make sure the payment status is "Completed" 
if ($_POST['payment_status'] != 'Completed') { 
    // simply ignore any IPN that is not completed 
    exit(0); 
} 

// 2. Make sure seller email matches your primary account email. 
if ($_POST['receiver_email'] != 'PRIMARY EMAIL ADDRESS') { 
    $errmsg .= "'receiver_email' does not match: "; 
    $errmsg .= $_POST['receiver_email']."\n"; 
} 

// 3. Make sure the currency code matches 
if ($_POST['mc_currency'] != 'USD') { 
    $errmsg .= "'mc_currency' does not match: "; 
    $errmsg .= $_POST['mc_currency']."\n"; 
} 

// 4. Ensure the transaction is not a duplicate. 
mysql_connect('localhost', '[DB_USER]', '[DB_PW') or exit(0); 
mysql_select_db('DB_NAME') or exit(0); 

$txn_id = mysql_real_escape_string($_POST['txn_id']); 
$sql = "SELECT COUNT(*) FROM orders WHERE txn_id = '$txn_id'"; 
$r = mysql_query($sql); 

if (!$r) { 
    error_log(mysql_error()); 
    exit(0); 
} 

$exists = mysql_result($r, 0); 
mysql_free_result($r); 

if ($exists) { 
    $errmsg .= "'txn_id' has already been processed: ".$_POST['txn_id']."\n"; 
} 

if (!empty($errmsg)) { 

    // manually investigate errors from the fraud checking 
    $body = "IPN failed fraud checks: \n$errmsg\n\n"; 
    $body .= $listener->getTextReport(); 
    mail('NOTIFICATION EMAIL ADDRESS', 'IPN Fraud Warning', $body); 

} else { 

    <?php 
    $csvData = file_get_contents($_POST['custom']); 
    $csvNumColumns = 3; 
    $csvDelim = ";"; 
    $data = array_chunk(str_getcsv($csvData, $csvDelim), $csvNumColumns); 
    ?> 

    // add this order to a table 
    $user_id = mysql_real_escape_string($_POST['item_name']); 
    $credit_amount = mysql_real_escape_string($_POST['item_number']); 
    $type = mysql_real_escape_string($_POST['custom']); 

    $sql = "INSERT INTO TABLE_NAME VALUES 
     (NULL, '$user_id', '$credit_amount', '$type')"; 

    if (!mysql_query($sql)) { 
     error_log(mysql_error()); 
     exit(0); 

    } 


} 

} else { 
    // manually investigate the invalid IPN 
    mail('NOTIFICATION EMAIL ADDRESS', 'Invalid IPN', $listener->getTextReport()); 
} 

?> 

這似乎與貝寶沙箱的IPN測試服務測試時,它做工精細,我可以在需要的值ITEM_NAME,進入ITEM_NUMBER和自定義(或使用時,下面的代碼)

<form name="_xclick" action="https://www.sandbox.paypal.com/cgi-bin/webscr" 
    method="post"> 
    <input type="hidden" name="cmd" value="_xclick"> 
    <input type="hidden" name="business" value="SANDBOX EMAIL ADDRESS"> 
    <input type="hidden" name="currency_code" value="USD"> 
    <input type="hidden" name="amount" value="9.99"> 
    <input type="hidden" name="custom" value="<?=$this->package['0']['delivered'];?>" 
    <input type="hidden" name="item_name" value="<?=$_SESSION["user_id"]?>" 
    <input type="hidden" name="item_number" value="<?=$this->package['0']['number'];?>" 
    <input type="hidden" name="return" value="WEBSITE_URL/success"> 
    <input type="hidden" name="notify_url" value="WEBSITE_URL/ipn.php"> 
    <input type="image" src="http://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" 
    border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"> 
</form> 

但是我很快意識到,這將是很大的,受到客戶的ITEM_NAME「要的東西辨認更好,而不是它被定義爲「USER_ID」。是否有可能有「自定義」通過定義爲我所需要的變量的所有3,用分號PayPal按鈕在其中分離(如下似乎)

<input type="hidden" name="custom" value="<?=$_SESSION["user_id"]?>;<?=$this->package['0']['number'];?>;<?=$this->package['0']['delivered'];?>" 

然後使用類似

<?php 
    $csvData = file_get_contents($_POST['custom']); 
    $csvNumColumns = 3; 
    $csvDelim = ";"; 
    $data = array_chunk(str_getcsv($csvData, $csvDelim), $csvNumColumns); 
    ?> 

給我,然後可以被定義爲「USER_ID」,「credit_amount」和「類型」

一旦這些變量分離和確定不同的變量,然而,它們被張貼到數據庫中,如果表中已經有一列是h與試圖發佈的'user_id'以及試圖發佈的'type'相同,那麼它應該只更新該行(在嘗試添加之前表中已有的行),方法是添加' credit_amount'到舊行中相應的'credit_amount'單元格。

回答

0

您可以傳遞多個值,並將它們填充到單個變量中,例如變量「custom」。這不會有問題。我自己也是這樣做的。當我編碼時,我使用了|分開這些值,然後讓我的IPN解析出我的IPN腳本中的值。

+0

是的,很好,我不能說我對PHP 100%熟悉,我以前也沒有使用過IPN(除了腳本中預配置的監聽器),我不確定如何準確地進行IPN解析在腳本中輸出值,然後將它們定義爲單獨的值,如果您可以發佈您用於此的代碼片段,我相信我將能夠調整它並使用它來解決我的問題。另外,如果在兩個值匹配的情況下如何更新行而不是創建新行,您是否有任何想法?非常感激。 – 2013-03-08 00:29:34