php - paypal IPN php 脚本- mysql 不工作?

标签 php mysql error-handling paypal paypal-ipn

我从 paypal 获取了标准的 IPN.php 示例,向 php 脚本添加了错误处理和 mysqlconnection/mysqlquery。我相信它们可能是变量进入 ipn 脚本然后存储在 mysql 数据库中的问题。 Mysql 数据库连接工作正常。我真的认为它的代码与mysql查询有关,但我不知道如何更改它。任何帮助都会很棒!

错误来自 ipn_errors.log

[21-Jan-2013 13:45:57 UTC] PHP Notice:  Undefined variable: item_number in /var/www/ipn.php on line 78
[21-Jan-2013 13:45:57 UTC] PHP Notice:  Undefined variable: payment_status in /var/www/ipn.php on line 78
[21-Jan-2013 13:45:57 UTC] PHP Notice:  Undefined variable: payment_amount in /var/www/ipn.php on line 78
[21-Jan-2013 13:45:57 UTC] PHP Notice:  Undefined variable: payer_email in /var/www/ipn.php on line 78

ipn.php- 造成这些错误。

// STEP 1: Read POST data

// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST
// reading raw POST data from input stream instead. 
mysql_connect("localhost", "#####", "#####") or die(mysql_error());  
mysql_select_db("#####") or die(mysql_error()); 
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
 $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
   $get_magic_quotes_exists = true;
} 
foreach ($myPost as $key => $value) {        
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) { 
    $value = urlencode(stripslashes($value)); 
} else {
    $value = urlencode($value);
}
$req .= "&$key=$value";
}


// STEP 2: Post IPN data back to paypal to validate

$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

// In wamp like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) {
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);


// STEP 3: Inspect IPN validation result and act accordingly

if (strcmp ($res, "VERIFIED") == 0) {
// check whether 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

// assign posted variables to local variables
$payer_email = $_POST['payer_email'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
} else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
 ini_set('log_errors', true);
ini_set('error_log', dirname(__FILE__).'/ipn_errors.log');
}
 mysql_query("INSERT INTO paypalpayments (itemnum, paystatus, payamount, email) VALUES ('', '$item_number', 

'$payment_status', '$payment_amount',  '$payer_email')");
?>

最佳答案

变量设置在这一行:

$myPost[$keyval[0]] = urldecode($keyval[1]);

... 稍后在脚本中它使用 $_POST 来复制这样的变量:

$payer_email = $_POST['payer_email'];

建议:使用$myPost 代替$_POST 来定位这些变量。并且不要忘记在 INSERT 查询之前使用 mysql_real_escape_string()。

如果在顶部使用 ob_start() 并在末尾使用 $msg = ob_get_clean(),则可以在调试此异步脚本方面取得一些进展。然后你有一个可变字符串,你可以通过电子邮件发送给自己。这将使您能够使用 var_dump() 等来发出诊断消息。

关于php - paypal IPN php 脚本- mysql 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445354/

相关文章:

c++ - 如何在C++中正确使用hash_set

php - 如何用 Amazon S3 替换 PHP imagecopyresampled?

php - codeigniter count_all_results 与连接表

python - 为什么我看不到 readline.set_completion_display_matches_hook 的错误?

php - 获取没有文件的文件夹

Mysql连接用户和事件表

c++ - gRPC 错误返回意外的详细信息字段

php - 使用 HTML 和 PHP 将表单数据发送到电子邮件

php - 使用 php 删除除前 20 行以外的所有行

php - CodeIgniter 存储过程的多个结果