PayPal IPN 响应无法插入数据库
PayPal IPN Response can't insert to database
所以最近我在别人的基础上做了一个IPN小系统,效果很好。我可以通过沙盒购买这个项目,它会通过 IPN 向我的听众发送信息,然后插入到数据库中。但是现在当我昨天尝试时,在文件没有改变之后,它不起作用。它确实会向我发送 IPN,我可以将其存储在 .txt 中 - 但我只能这样做。如果我随后手动获取响应并将字符串更改为该值(来自 .txt),则一切正常。我不明白这是为什么,我尝试了很多方法来解决这个问题。
下面是我从 paypal 得到的回复(我的自适应 IPN 也有同样的问题,之前有效)
VERIFIED -- cmd=_notify-validate&mc_gross=15.00&protection_eligibility=Eligible&address_status=confirmed&payer_id=UXU2LGHMZ8FQC&tax=0.00&address_street=1+Main+St&payment_date=02%3A36%3A31+Aug+20%2C+2015+PDT&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=Purple&mc_fee=0.74&address_country_code=US&address_name=Purple+Heart%27s+Test+Store¬ify_version=3.8&custom=3-2-FRT&payer_status=verified&business=viktor**-facilitator%40yahoo.com&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=ARlhq6hLAVqXSD5s.6w.I67yISfcA-u87czB5qI9QnD2Y2gKnJ3b.tHb&payer_email=terleto%40yahoo.com&txn_id=3NX1120866247313A&payment_type=instant&payer_business_name=Purple+Heart%27s+Test+Store&last_name=Heart&address_state=CA&receiver_email=viktor**-facilitator%40yahoo.com&payment_fee=0.74&receiver_id=FWCDTECER3V24&txn_type=web_accept&item_name=Featured+Status+for+Umper+Dumpar&mc_currency=USD&item_number=&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=3-2-FRT&payment_gross=15.00&shipping=0.00&ipn_track_id=dfee171e44f44
这是我的 IPN
<?php
class Paypal_IPN
{
/** @var string $_url The paypal url to go to through cURL
private $_url;
/**
* @param string $mode 'live' or 'sandbox'
*/
public function __construct($mode = 'sandbox')
{
if ($mode == 'live')
$this->_url = 'https://www.paypal.com/cgi-bin/webscr';
else
$this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
public function run()
{
$postFields = 'cmd=_notify-validate&' . file_get_contents("php://input");
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $this->_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postFields
));
$result = curl_exec($ch);
curl_close($ch);
/* $fh = fopen('result.txt', 'w');
fwrite($fh, $result . ' -- ' . $postFields);
fclose($fh); */
function decodePayPalIPN($postFields)
{
if (empty($postFields)) {
return array();
}
$post = array();
$pairs = explode('&', $postFields);
foreach ($pairs as $pair) {
list($key, $value) = explode('=', $pair, 2);
$key = urldecode($key);
$value = urldecode($value);
preg_match('/(\w+)(?:\[(\d+)\])?(?:\.(\w+))?/', $key, $key_parts);
switch (count($key_parts)) {
case 4:
if (!isset($post[$key_parts[1]])) {
$post[$key_parts[1]] = array(
$key_parts[2] => array(
$key_parts[3] => $value
)
);
} else if (!isset($post[$key_parts[1]][$key_parts[2]])) {
$post[$key_parts[1]][$key_parts[2]] = array(
$key_parts[3] => $value
);
} else {
$post[$key_parts[1]][$key_parts[2]][$key_parts[3]] = $value;
}
break;
case 3:
if (!isset($post[$key_parts[1]])) {
$post[$key_parts[1]] = array();
}
$post[$key_parts[1]][$key_parts[2]] = $value;
break;
default:
$post[$key] = $value;
break;
}
}
return $post;
}
$raw_post_parsed = decodePayPalIPN($postFields); // Now we are decoding it
/*echo "<pre>";
print_r($raw_post_parsed);
echo "</pre>";
$raw_post_parsed_Text = var_export($raw_post_parsed, true);
$fh = fopen('result2.txt', 'w');
fwrite($fh, $result . ' -- ' . $raw_post_parsed_Text);
fclose($fh); */
if ($raw_post_parsed['payment_status'] === "Completed") {
if ($raw_post_parsed['payment_gross'] === "15.00") {
include(dirname(__FILE__) . "/../../includes/config.php");
include(dirname(__FILE__) . "/../../includes/session.php");
$verify_sign = $raw_post_parsed['verify_sign'];
$payer_email = $raw_post_parsed['payer_email'];
$custom = $raw_post_parsed['custom'];
$custom_exploded = explode("-", $custom); // Exploding the item into arrays
$custom_exploded_sound_id = $custom_exploded[0]; //sound type
$custom_exploded_purchase_id = $custom_exploded[1]; //sound ID
$sql = "INSERT INTO as_sound_featured (sound_id, purchaser_id, purchaser_email, verify_sign) VALUES ('$custom_exploded_sound_id', '$custom_exploded_purchase_id', '$payer_email', '$verify_sign')";
if ($con->query($sql) === TRUE) {
$sql2 = "UPDATE as_sound SET extra='1' WHERE id=$custom_exploded_sound_id";
$website_url = "http://" . $_SERVER['SERVER_NAME'] . "/";
$comment_desc_notification = "Your sound is now featured";
$comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
$sql_not = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
mysqli_query($con, $sql_not);
$con->query($sql2);
} else {
$website_url = "http://" . $_SERVER['SERVER_NAME'] . "/";
$comment_desc_notification = "Featured status for your sound failed";
$comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
$sql_not = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
mysqli_query($con, $sql_not);
}
} else {
$website_url = "http://" . $_SERVER['SERVER_NAME'] . "/";
$comment_desc_notification = "Featured status for your sound failed";
$comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
$sql_not = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
mysqli_query($con, $sql_not);
}
}
}
}
nvm,我已经屏蔽了所有不属于我的 IP - 这就是问题所在。解决了。
所以最近我在别人的基础上做了一个IPN小系统,效果很好。我可以通过沙盒购买这个项目,它会通过 IPN 向我的听众发送信息,然后插入到数据库中。但是现在当我昨天尝试时,在文件没有改变之后,它不起作用。它确实会向我发送 IPN,我可以将其存储在 .txt 中 - 但我只能这样做。如果我随后手动获取响应并将字符串更改为该值(来自 .txt),则一切正常。我不明白这是为什么,我尝试了很多方法来解决这个问题。
下面是我从 paypal 得到的回复(我的自适应 IPN 也有同样的问题,之前有效)
VERIFIED -- cmd=_notify-validate&mc_gross=15.00&protection_eligibility=Eligible&address_status=confirmed&payer_id=UXU2LGHMZ8FQC&tax=0.00&address_street=1+Main+St&payment_date=02%3A36%3A31+Aug+20%2C+2015+PDT&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=Purple&mc_fee=0.74&address_country_code=US&address_name=Purple+Heart%27s+Test+Store¬ify_version=3.8&custom=3-2-FRT&payer_status=verified&business=viktor**-facilitator%40yahoo.com&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=ARlhq6hLAVqXSD5s.6w.I67yISfcA-u87czB5qI9QnD2Y2gKnJ3b.tHb&payer_email=terleto%40yahoo.com&txn_id=3NX1120866247313A&payment_type=instant&payer_business_name=Purple+Heart%27s+Test+Store&last_name=Heart&address_state=CA&receiver_email=viktor**-facilitator%40yahoo.com&payment_fee=0.74&receiver_id=FWCDTECER3V24&txn_type=web_accept&item_name=Featured+Status+for+Umper+Dumpar&mc_currency=USD&item_number=&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=3-2-FRT&payment_gross=15.00&shipping=0.00&ipn_track_id=dfee171e44f44
这是我的 IPN
<?php
class Paypal_IPN
{
/** @var string $_url The paypal url to go to through cURL
private $_url;
/**
* @param string $mode 'live' or 'sandbox'
*/
public function __construct($mode = 'sandbox')
{
if ($mode == 'live')
$this->_url = 'https://www.paypal.com/cgi-bin/webscr';
else
$this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
public function run()
{
$postFields = 'cmd=_notify-validate&' . file_get_contents("php://input");
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $this->_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postFields
));
$result = curl_exec($ch);
curl_close($ch);
/* $fh = fopen('result.txt', 'w');
fwrite($fh, $result . ' -- ' . $postFields);
fclose($fh); */
function decodePayPalIPN($postFields)
{
if (empty($postFields)) {
return array();
}
$post = array();
$pairs = explode('&', $postFields);
foreach ($pairs as $pair) {
list($key, $value) = explode('=', $pair, 2);
$key = urldecode($key);
$value = urldecode($value);
preg_match('/(\w+)(?:\[(\d+)\])?(?:\.(\w+))?/', $key, $key_parts);
switch (count($key_parts)) {
case 4:
if (!isset($post[$key_parts[1]])) {
$post[$key_parts[1]] = array(
$key_parts[2] => array(
$key_parts[3] => $value
)
);
} else if (!isset($post[$key_parts[1]][$key_parts[2]])) {
$post[$key_parts[1]][$key_parts[2]] = array(
$key_parts[3] => $value
);
} else {
$post[$key_parts[1]][$key_parts[2]][$key_parts[3]] = $value;
}
break;
case 3:
if (!isset($post[$key_parts[1]])) {
$post[$key_parts[1]] = array();
}
$post[$key_parts[1]][$key_parts[2]] = $value;
break;
default:
$post[$key] = $value;
break;
}
}
return $post;
}
$raw_post_parsed = decodePayPalIPN($postFields); // Now we are decoding it
/*echo "<pre>";
print_r($raw_post_parsed);
echo "</pre>";
$raw_post_parsed_Text = var_export($raw_post_parsed, true);
$fh = fopen('result2.txt', 'w');
fwrite($fh, $result . ' -- ' . $raw_post_parsed_Text);
fclose($fh); */
if ($raw_post_parsed['payment_status'] === "Completed") {
if ($raw_post_parsed['payment_gross'] === "15.00") {
include(dirname(__FILE__) . "/../../includes/config.php");
include(dirname(__FILE__) . "/../../includes/session.php");
$verify_sign = $raw_post_parsed['verify_sign'];
$payer_email = $raw_post_parsed['payer_email'];
$custom = $raw_post_parsed['custom'];
$custom_exploded = explode("-", $custom); // Exploding the item into arrays
$custom_exploded_sound_id = $custom_exploded[0]; //sound type
$custom_exploded_purchase_id = $custom_exploded[1]; //sound ID
$sql = "INSERT INTO as_sound_featured (sound_id, purchaser_id, purchaser_email, verify_sign) VALUES ('$custom_exploded_sound_id', '$custom_exploded_purchase_id', '$payer_email', '$verify_sign')";
if ($con->query($sql) === TRUE) {
$sql2 = "UPDATE as_sound SET extra='1' WHERE id=$custom_exploded_sound_id";
$website_url = "http://" . $_SERVER['SERVER_NAME'] . "/";
$comment_desc_notification = "Your sound is now featured";
$comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
$sql_not = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
mysqli_query($con, $sql_not);
$con->query($sql2);
} else {
$website_url = "http://" . $_SERVER['SERVER_NAME'] . "/";
$comment_desc_notification = "Featured status for your sound failed";
$comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
$sql_not = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
mysqli_query($con, $sql_not);
}
} else {
$website_url = "http://" . $_SERVER['SERVER_NAME'] . "/";
$comment_desc_notification = "Featured status for your sound failed";
$comment_link_notification = $website_url . "audio/item.php?sid=" . $custom_exploded_sound_id;
$sql_not = "INSERT INTO as_notifications (notification_owner_id, notification_text, notification_link) VALUES ($custom_exploded_purchase_id, '$comment_desc_notification', '$comment_link_notification')";
mysqli_query($con, $sql_not);
}
}
}
}
nvm,我已经屏蔽了所有不属于我的 IP - 这就是问题所在。解决了。