Php 如何从PayPal IPN添加结果以更新数据库
我期待能够从贝宝IPN帖子的信息,并使用某些项目来更新我的数据库。这是我的ipn.php的当前代码Php 如何从PayPal IPN添加结果以更新数据库,php,mysql,paypal,Php,Mysql,Paypal,我期待能够从贝宝IPN帖子的信息,并使用某些项目来更新我的数据库。这是我的ipn.php的当前代码 <?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('
<?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());
}
?>
//将此订单添加到表中
$user\u id=mysql\u real\u escape\u字符串($\u POST['item\u name']);
$credit\u amount=mysql\u real\u escape\u字符串($\u POST['item\u number']);
$type=mysql\u real\u escape\u字符串($\u POST['custom']);
$sql=“插入到表中\u名称值
(空,“$user_id”、“$credit_amount”、“$type”);
if(!mysql_query($sql)){
错误日志(mysql\u error());
出口(0);
}
}
}否则{
//手动调查无效的IPN
邮件('NOTIFICATION EMAIL ADDRESS','Invalid IPN',$listener->getTextReport());
}
?>
在使用PayPal Sandbox的IPN测试服务进行测试时,这似乎工作正常,我可以为item_name、item_number和custom输入所需的值(或者使用以下代码时)
您可以传递多个值,并将它们填充到单个变量中,例如变量“custom”。这不会有什么问题。过去我自己也做过这件事。当我编写此代码时,我使用|分隔值,然后让我的IPN解析出我的IPN脚本中的值。是的,很好,我不能说我100%熟悉PHP,我以前也从未使用过IPN(脚本中预配置的侦听器除外),我不确定如何让我的IPN解析出脚本中的值,然后将它们定义为单独的值,如果您可以发布用于此的代码片段,我相信我将能够调整它并使用它来解决我的问题。另外,当两个值匹配时,您对如何更新行而不是创建新行有何想法?非常感谢。
<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>
<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);
?>