2011-10-25 279 views
3

我有很多問題試圖捕獲訂閱取消。這是我從Paypal的代碼示例頁面獲得的IPN Im。當用戶結束訂閱時,我添加了mySQL腳本以減去4000個積分。當我使用另一個PayPal帳戶測試代碼時,一旦我取消訂閱,4000個積分不會被扣除。
是貝寶IPN錯誤或是按鈕錯誤或什麼!?收取PayPal訂閱取消

<?php 
    session_start(); 
    // 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.paypal.com', 443, $errno, $errstr, 30); 

// 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']; 
//////////////////////////////////////////////I added this here also. Check if its canceled 
    if("subscr_cancel" = $_POST['txn_type']){ 

if (!$fp) { 
// HTTP ERROR 
} else { 
fputs ($fp, $header . $req); 
while (!feof($fp)) { 
$res = fgets ($fp, 1024); 
if (strcmp ($res, "VERIFIED") == 0) { 
// 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 
    /////I added this mysql 
     $password = secret 
    $username = secret 
    $websites= "websites"; 
    $database = "k29803_1"; 
    mysql_connect("localhost", $username, $password) or die(mysql_error()); 
    mysql_select_db($database); 

    $query = "SELECT * FROM users WHERE username= '$_SESSION[username]' AND password = '$_SESSION[password]'"; 
    $results = mysql_query($query)or die(mysql_error()); 
    if(mysql_num_rows($results)==1){ 
     $add_credits = "UPDATE users SET credits = credits +1000 
     WHERE username= '$_SESSION[username]' AND password = '$_SESSION[password]'"; 
     mysql_query($add_credits) or die(mysql_error()); 


     } 

} 
else if (strcmp ($res, "INVALID") == 0) { 
// log for manual investigation 
} 
} 
fclose ($fp); 
} 
    } 
    ?> 

的按鈕看起來像這樣

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<input type="hidden" name="cmd" value="_s-xclick"> 
<input type="hidden" name="hosted_button_id" value="NQMGM2LCZUQRE"> 

    <input type="hidden" name="item_name" value="1" /> 
    <input type="hidden" name="item_number" value="01" /> 
    <input type="hidden" name="amount" value="1.99" /> 
    <input type="hidden" name="currency_code" value="USD" /> 
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_subscribeCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> 
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"> 
</form> 

我重定向到這個頁面,但我只是不明白的MYSQL值減去

回答

0

我看到的主要問題是,你使用的是會話變量。請注意,點擊您的PHP代碼的請求將直接來自PayPal服務器,並且沒有任何與當前登錄的用戶有關。

您需要將用戶信息傳遞給PayPal,以便PayPal可以將其作爲IPN請求的一部分傳回給您的服務器。您可以做的另一件事是檢查IPN中的一些值,例如付款人的電子郵件地址(payer_email)或parent_txn_idpayer_id

我不確定訂閱,但退款和取消IPN將包含提供原始購買的txn_id的parent_txn_id,因此您可以使用它來查找用戶的帳戶。

您需要做的是確保在購買時保存來自IPN的所有信息,並將其與txn_id或類似用戶的帳戶綁定。取消進入時,使用parent_txn_id或任何您用於在數據庫中查找帳戶的值,然後將更改應用到用戶的帳戶。

我在上面的代碼中看到了許多其他問題。

$query = "SELECT * FROM users WHERE username= '$_SESSION[username]' AND password = '$_SESSION[password]'"; 

您需要在您添加到查詢中所有變量使用mysql_real_escape_string。你應該這樣做:

$query = "SELECT * FROM users WHERE username='".mysql_real_escape_string($username)."' AND password = '.mysql_real_escape_string($password).'"; 

其次,數組引用應使用圍繞重點名字報價:

$_SESSION[password] => $_SESSION['password'] 

第三,你可能應該由它們的行ID的用戶定位在數據庫中的用戶表(你確實有一個「id」的主鍵被設置爲自動增量,對吧?),而不是他們的用戶名和密碼。在對用戶進行身份驗證之後,您確實不應該將密碼保存在會話變量中。

+0

我換成你說克里斯一切。我甚至在查詢中使用了純粹的用戶名和密碼。當我通過訂閱和取消訂閱進行測試時,它仍然沒有減少4000個學分。我讀了一些有關PayPal IPN已經過時的地方,我想你PDT(支付數據傳輸)。這是真的? – mbejda

+0

AHHHH WOOT WOOT得到它工作!問題是這樣的if(「subscr_cancel」= $ _POST ['txn_type']){ 需要if(「subscr_cancel」== $ _POST ['txn_type']){ – mbejda

+0

良好的捕獲,對不起,我錯過了。作業與比較操作員;一定要小心。 –

0

您的代碼在這裏有一個BUG:

if("subscr_cancel" = $_POST['txn_type']){ 

看錶的比較:

if("subscr_cancel" == $_POST['txn_type']){