简体   繁体   English

修改PayPal IPN侦听器以在付款完成后处理多个查询

[英]Modifying PayPal IPN listener to handle multiple queries when payment is complete

I'm having trouble modifying my PayPal IPN listener due to my lack of knowledge of PHP. 由于缺乏PHP知识,我无法修改PayPal IPN侦听器。

After payment has completed my IPN listener inserts details of the transacation into the table 'payments', however I now need the script to also update another table named 'Members' so I can easily tell who has paid. 付款完成后,我的IPN侦听器将事务处理的详细信息插入到“付款”表中,但是现在我需要脚本来更新另一个名为“会员”的表,以便我可以轻松知道谁付款了。

This is the SQL I have come up with: 这是我想出的SQL:

"UPDATE `Members` SET paid='TRUE' WHERE id='".$data['custom']."'

I'm using the IPN variable 'custom' to send the users ID through from the original payment form. 我正在使用IPN变量“自定义”通过原始付款表单发送用户ID。

I have tried implementing mysqli_multi_query into the script but with no success. 我尝试将mysqli_multi_query实现到脚本中,但没有成功。 I'm really stuck on this one, if someone could point me in the right direction that would be awesome. 如果有人可以向我指出正确的方向,那真是太棒了。

Here is my PHP: 这是我的PHP:

function check_txnid($tnxid){
    global $link;
    return true;
    $valid_txnid = true;
    //get result set
    $sql = mysql_query("SELECT * FROM `payments` WHERE txnid = '$tnxid'", $link);       
    if($row = mysql_fetch_array($sql)) {
        $valid_txnid = false;
    }
    return $valid_txnid;
}

function check_price($price, $id){
    $valid_price = false;
    //you could use the below to check whether the correct price has been paid for the product

    /* 
    $sql = mysql_query("SELECT amount FROM `products` WHERE id = '$id'");       
    if (mysql_numrows($sql) != 0) {
        while ($row = mysql_fetch_array($sql)) {
            $num = (float)$row['amount'];
            if($num == $price){
                $valid_price = true;
            }
        }
    }
    return $valid_price;
    */
    return true;
}

function updatePayments($data){ 
    global $link;
    if(is_array($data)){                
        $sql = mysql_query("INSERT INTO `payments` (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
                '".$data['txn_id']."' ,
                '".$data['payment_amount']."' ,
                '".$data['payment_status']."' ,
                '".$data['item_number']."' ,
                '".date("Y-m-d H:i:s")."' 
                )", $link);

    return mysql_insert_id($link);
    }
}

//Database Connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);

// Check if paypal request or response
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){

    // Firstly Append paypal account to querystring
    $querystring .= "?business=".urlencode($paypal_email)."&";  

    // Append amount& currency (£) to quersytring so it cannot be edited in html

    //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
    $querystring .= "item_name=".urlencode($item_name)."&";
    $querystring .= "amount=".urlencode($item_amount)."&";

    //loop for posted values and append to querystring
    foreach($_POST as $key => $value){
        $value = urlencode(stripslashes($value));
        $querystring .= "$key=$value&";
    }

    // Append paypal return addresses
    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url))."&";
    $querystring .= "notify_url=".urlencode($notify_url);

    // Append querystring with custom field
    //$querystring .= "&custom=".USERID;

    // Redirect to paypal IPN
    header('location:https://www.paypal.com/cgi-bin/webscr'.$querystring);
    exit();

}else{

    // Response from Paypal

    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
        $req .= "&$key=$value";
}

    // assign posted variables to local variables
    $data['item_name']          = mysql_real_escape_string($_POST['item_name']);
    $data['item_number']        = mysql_real_escape_string($_POST['item_number']);
    $data['payment_status']     = mysql_real_escape_string($_POST['payment_status']);
    $data['payment_amount']     = mysql_real_escape_string($_POST['mc_gross']);
    $data['payment_currency']   = mysql_real_escape_string($_POST['mc_currency']);
    $data['txn_id']             = mysql_real_escape_string($_POST['txn_id']);
    $data['receiver_email']     = mysql_real_escape_string($_POST['receiver_email']);
    $data['payer_email']        = mysql_real_escape_string($_POST['payer_email']);
    $data['custom']             = mysql_real_escape_string($_POST['custom']);

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

    if (!$fp) {
        // HTTP ERROR
    } else {    

        fputs ($fp, $header . $req);
        while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp($res, "VERIFIED") == 0) {

                // Used for debugging
                //@mail("you@youremail.com", "PAYPAL DEBUGGING", "Verified Response<br />data = <pre>".print_r($post, true)."</pre>");

                // Validate payment (Check unique txnid & correct price)
                $valid_txnid = check_txnid($data['txn_id']);
                $valid_price = check_price($data['payment_amount'], $data['item_number']);
                // PAYMENT VALIDATED & VERIFIED!
                if($valid_txnid && $valid_price){               
                    $orderid = updatePayments($data);       
                    if($orderid){                   
                        // Payment has been made & successfully inserted into the Database                              
                    }else{                              
                    // Error inserting into DB
                    // E-mail admin or alert user
                    }
                }else{                  
                    // Payment made but data has been changed
                    // E-mail admin or alert user
                }                       

            }else if (strcmp ($res, "INVALID") == 0) {

                // PAYMENT INVALID & INVESTIGATE MANUALY! 
                // E-mail admin or alert user

                // Used for debugging
                //@mail("you@youremail.com", "PAYPAL DEBUGGING", "Invalid Response<br />data = <pre>".print_r($post, true)."</pre>");
            }       
        }       
    fclose ($fp);
    }   
}
?>

Just make two queries 只需查询两个

function updatePayments($data){ 
    global $link;
    if(is_array($data)){                
        $sql = mysql_query("INSERT INTO `payments` (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
                '".$data['txn_id']."' ,
                '".$data['payment_amount']."' ,
                '".$data['payment_status']."' ,
                '".$data['item_number']."' ,
                '".date("Y-m-d H:i:s")."' 
                )", $link);
         mysql_query("UPDATE `Members` SET paid='TRUE' WHERE id='".$data['custom'], $link);
    return mysql_insert_id($link);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM