【问题标题】:paypal IPN php script- mysql not working?贝宝 IPN php 脚本 - mysql 不工作?
【发布时间】:2013-01-04 21:41:53
【问题描述】:

我从 paypal 获取了标准的 IPN.php 示例,在 php 脚本中添加了错误处理和 mysqlconnection/mysqlquery。我相信它们可能是变量进入 ipn 脚本然后存储在 mysql db 中的问题。 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')");
?>

【问题讨论】:

  • 这些是通知,而不是错误,它应该仍然有效 - 是吗?
  • 没有。 mysql代码可能有问题。有没有办法将 POST 输出到文本文件,而不是 mysql db,这样我就可以在没有 mysql 的情况下进行测试?对不起,我是一个 vb.net/asp 人,但我必须使用带有 LAMP 的 linux 服务器。

标签: php mysql error-handling paypal paypal-ipn


【解决方案1】:

变量设置在这一行:

$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() 等来发出诊断消息。

【讨论】:

  • 我不知道如何使用ob_start。稍后我会查一下。我确实根据您的建议进行了更正,但是我仍然收到相同的通知。有没有办法将 POST 输出到文本文件,而不是 mysql db,这样我就可以在没有 mysql 的情况下进行测试?
猜你喜欢
  • 1970-01-01
  • 2013-09-02
  • 2012-01-07
  • 2019-01-09
  • 2012-02-22
  • 2015-06-16
  • 2012-08-19
  • 2014-06-30
  • 2013-03-06
相关资源
最近更新 更多