Php PayPal IPN:读取多个商品名称和数量并保存到数据库
下面是我的IPN代码。我的问题是,我无法在表Php PayPal IPN:读取多个商品名称和数量并保存到数据库,php,paypal,Php,Paypal,下面是我的IPN代码。我的问题是,我无法在表paypal中添加多个项目名称或数量。事实上,IPN并没有监听付款人id、数量、项目名称(多个项目) 我们如何在这里发布item_name1或item_name2等,以便在验证时,我可以在另一个将成为成功页面的页面中获得它。虽然我通过其他方法实现了这一点,但这不是正确的方法。所以我想知道为什么这些变量不是以字符串的形式来自ipn,只有这些变量以字符串的形式来自txn、smt、st、currency,其余的都没有。如果我们必须在这里使用loop,那么如何
paypal
中添加多个项目名称或数量。事实上,IPN并没有监听付款人id、数量、项目名称(多个项目)
我们如何在这里发布item_name1或item_name2等,以便在验证时,我可以在另一个将成为成功页面的页面中获得它。虽然我通过其他方法实现了这一点,但这不是正确的方法。所以我想知道为什么这些变量不是以字符串的形式来自ipn,只有这些变量以字符串的形式来自txn、smt、st、currency,其余的都没有。如果我们必须在这里使用loop,那么如何从这里将值发送到insert-in-loop中ipn.php
任何帮助都将不胜感激
$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]);
}
$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";
}
/*
* Post IPN data back to PayPal to validate the IPN data is genuine
* Without this step anyone can fake IPN data
*/
$paypalURL = PAYPAL_URL;
$ch = curl_init($paypalURL);
if ($ch == FALSE) {
return FALSE;
}
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_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
// Set TCP timeout to 30 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close', 'User-Agent: company-name'));
$res = curl_exec($ch);
/*
* Inspect IPN validation result and act accordingly
* Split response headers and payload, a better way for strcmp
*/
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp($res, "VERIFIED") == 0 || strcasecmp($res, "VERIFIED") == 0) {
// Retrieve transaction info from PayPal
$item_number = $_POST['item_number'];
$txn_id = $_POST['txn_id'];
$payment_gross = $_POST['mc_gross'];
$currency_code = $_POST['mc_currency'];
$payment_status = $_POST['payment_status'];
$quantity = $_POST['quantity'];
// Check if transaction data exists with the same TXN ID
$prevPayment = $db->query("SELECT payment_id FROM payments WHERE txn_id = '".$txn_id."'");
if($prevPayment->num_rows > 0){
exit();
}else{
// Insert transaction data into the database
$insert = $db->query("INSERT INTO payments_paypal(item_number,txn_id,payment_gross,currency_code,payment_status) VALUES('".$item_number."','".$txn_id."','".$payment_gross."','".$currency_code."','".$payment_status."')");
}
}
您在
//从PayPal检索交易信息
一节中使用的代码似乎非常简单,并且不是为了查找多个项目而编写的,例如编号变量,如项目名称1
,项目名称2
,等等
您应该记录您正在接收但未正确处理的IPN的文本,或者从您的PayPal帐户历史记录中获取其文本:
此外,请查看多个项目的变量名称文档:
然后,您可以修改代码来读取和处理这些多个编号的变量,并将它们存储在您想要的任何数据库模式中,这可能需要额外的列或其他内容