php - Paypal (IPN) MySQL 查询调整

标签 php mysql paypal paypal-ipn

我希望有人能在这里帮助我。

我的代码工作正常,但我想稍微调整一下,以便在事务发生时向数据库添加更多数据。到目前为止,我的调整已经破坏了脚本。

基本上,我在设置为十进制的“用户”表中创建了一个“捐赠”列。我希望将交易的 $mc_gross 值添加到表“用户”中的用户 ID 与 Paypal 返回的 $option_selection2 匹配的列。 $option_selection2 在用户点击站点上的捐赠时获取用户 ID(数字)并将其包含在 IPN 中。然后,这应该为每个用户捐赠的所有内容创建一个运行总计。

我要添加的代码是:

$userquery = UPDATE ".PREFIX."user SET donated = donated + '".$mc_gross."' WHERE userID = '".$option_selection2."'; 
$result = mysql_query($userquery);

您可以在下面看到完整的脚本以及我尝试插入额外查询的位置

<?php
// 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";

// If testing on Sandbox use:
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr,         30);
$fp = fsockopen ('ssl://ipnpb.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'];
$mc_gross = $_POST['mc_gross'];
$txn_id = $_POST['txn_id'];
$quantity = $_POST['quantity'];
$payment_date = $_POST['payment_date'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$payment_type = $_POST['payment_type'];
$memo = $_POST['memo'];
$payer_email = $_POST['payer_email'];
$txn_type = $_POST['txn_type'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];
$address_country = $_POST['address_country'];
$item_number = $_POST['item_number'];
$tax = $_POST['tax'];
$option_name1 = $_POST['option_name1'];
$option_selection1 = $_POST['option_selection1'];
$option_name2 = $_POST['option_name2'];
$option_selection2 = $_POST['option_selection2'];
$mc_currency = $_POST['mc_currency'];
$mc_fee = $_POST['mc_fee'];
$parent_txn_id  = $_POST['parent_txn_id'];
$pending_reason = $_POST['pending_reason'];
$reason_code = $_POST['reason_code'];


// subscription specific vars

$subscr_id = $_POST['subscr_id'];
$subscr_date = $_POST['subscr_date'];
$subscr_effective  = $_POST['subscr_effective'];
$period1 = $_POST['period1'];
$period2 = $_POST['period2'];
$period3 = $_POST['period3'];
$amount1 = $_POST['amount1'];
$amount2 = $_POST['amount2'];
$amount3 = $_POST['amount3'];
$mc_amount1 = $_POST['mc_amount1'];
$mc_amount2 = $_POST['mc_amount2'];
$mc_amount3 = $_POST['mcamount3'];
$recurring = $_POST['recurring'];
$reattempt = $_POST['reattempt'];
$retry_at = $_POST['retry_at'];
$recur_times = $_POST['recur_times'];
$username = $_POST['username'];
$password = $_POST['password'];

//auction specific vars

$for_auction = $_POST['for_auction'];
$auction_closing_date  = $_POST['auction_closing_date'];
$auction_multi_item  = $_POST['auction_multi_item'];
$auction_buyer_id  = $_POST['auction_buyer_id'];



//DB connect creds and email 
include("_mysql.php");
$DB_Server = $host; //your MySQL Server
$DB_Username = $user; //your MySQL User Name
$DB_Password = $pwd; //your MySQL Password
$DB_DBName = $db; //your MySQL Database Name


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



//create MySQL connection
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" .        mysql_errno());


//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database:<br>" . mysql_error(). "<br>" .  mysql_errno());


$fecha = date("m")."/".date("d")."/".date("Y");
$fecha = date("Y").date("m").date("d");

//check if transaction ID has been processed before
$checkquery = "select txnid from ".PREFIX."donate_transactions where       txnid='".$txn_id."'";
$sihay = mysql_query($checkquery) or die("Duplicate txn id check query     failed:<br>" . mysql_error() . "<br>" . mysql_errno());
$nm = mysql_num_rows($sihay);
 if ($nm == 0){

//execute query
if($payment_status == "Completed"){

    $urlquery = mysql_query("SELECT hpurl FROM ".PREFIX."settings");
    $urlfa = mysql_fetch_array($urlquery);
    $returnurl = $urlfa['hpurl'];        
    $fields = array('url'=>urlencode($returnurl),'prnr'=>urlencode("1")); 
    foreach($fields as $key=>$value) { $fields_string .=  '&'.$key.'='.$value; }
    rtrim($fields_string,'&amp;');
    $ch = curl_init();

$userquery = UPDATE ".PREFIX."user SET donated = donated +  '".$mc_gross."' WHERE userID = '".$option_selection2."';
    $result = mysql_query($userquery);



     $strQuery = "insert into  ".PREFIX."donate_transactions(paymentstatus,buyer_email,firstname,lastname,stree t,city,state,zipcode,country,mc_gross,mc_fee,itemnumber,itemname,os0,on0,os1,on1 ,quantity,memo,paymenttype,paymentdate,txnid,pendingreason,reasoncode,tax,datecr eation,txntype,mc_currency) values  ('".$payment_status."','".$payer_email."','".$first_name."','".$last_name."','". $address_street."','".$address_city."','".$address_state."','".$address_zip."',' ".$address_country."','".$mc_gross."','".$mc_fee."','".$item_number."','".$item_ name."','".$option_name1."','".$option_selection1."','".$option_name2."','".$opt ion_selection2."','".$quantity."','".$memo."','".$payment_type."','".$payment_da te."','".$txn_id."','".$pending_reason."','".$reason_code."','".$tax."','".$fech a."','".$txn_type."','".$mc_currency."')";
     $result = mysql_query($strQuery) or die("Default - donate_transactions,  Query failed:<br>" . mysql_error() . "<br>" . mysql_errno());







     //check if there is an historical entry
     $checkhistorical = "select * from ".PREFIX."donate_history where   year='".date("Y")."' AND month='".date("m")."'";
     $thism = mysql_query($checkhistorical);
     $nums = mysql_num_rows($thism);
     if ($nums == 0){
         //create entry
         $donatesettings=mysql_query("SELECT * FROM    ".PREFIX."donate_settings");
         $ds = mysql_fetch_array($donatesettings);
         $strQuery = "insert into   ".PREFIX."donate_history(year,month,total,target,donates) values   ('".date("Y")."','".date("m")."','".$mc_gross."','".$ds['monthlytarget']."',1)";
         $result = mysql_query($strQuery);
     }
     else{
         //update existing
         $ts = mysql_fetch_array($thism);
         $newTotal = $ts['total'] + $mc_gross;
         $donates = $ts['donates'] + 1;
         $strQuery = "UPDATE ".PREFIX."donate_history SET  total='".$newTotal."',donates='".$donates."' WHERE year='".date("Y")."' AND  month='".date("m")."'";
         $result = mysql_query($strQuery);
     }

}

 echo "Verified";

 }
} //END OF VALIDE

// if the IPN POST was 'INVALID'...do this


else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation

}
}
 fclose ($fp);
}
 ?>

我希望有人能在这里为我指明正确的方向!

非常感谢

最佳答案

$userquery = UPDATE ".PREFIX."user SET donated = donated +  '".$mc_gross."' WHERE userID = '".$option_selection2."';

您似乎缺少查询开头的引号?您没有意识到这个语法错误?

关于php - Paypal (IPN) MySQL 查询调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340532/

相关文章:

php - 从其他网站剪辑图像并将其添加到我的网站

php - Javascript解码包含编码字符串的JSON字符串

php - 复选框和文本不想显示值

mysql - 同时运行 MySQL 和 Webrick Windows 7

python - 付款时出现 HTTP 400

php - 如何将图像添加到 Laravel 5.3 的新通知服务生成的电子邮件中?

php - MYSQL查询错误-检查数据库中的用户

mysql - 如何获取通过加入返回的多个帐户的金额总和

windows-phone-7 - 我们如何在 Windows Phone 7 中实现应用内购买

php - header 显示在 apache 错误日志中(curl 和 paypal)