Paypal IPN监听器不发送mysql插入语句

时间:2016-09-27 21:30:00

标签: php mysql paypal listener paypal-ipn

我从某处获得了paypal ipn代码。当你从日期字段中删除日期时,无法记住哪里可以使用ipn模拟器。它不适用于日期。我正在测试实时付款,它从paypal成功发送ipn,没有错误日志。 HTTP响应为200,但没有任何内容插入到我的数据库中。

为什么它不起作用的任何想法?

以下是代码:

<?php
class PayPal_IPN{
function infotuts_ipn($im_debut_ipn) {

define('SSL_P_URL', 'https://www.paypal.com/cgi-bin/webscr');
 define('SSL_SAND_URL', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
 $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
 if (!preg_match('/paypal\.com$/', $hostname)) {
 $ipn_status = 'Validation post isn\'t from PayPal';
 if ($im_debut_ipn == true) {
 // mail test
 }

return false;
 }

// parse the paypal URL
 $paypal_url = ($_REQUEST['test_ipn'] == 1) ? SSL_SAND_URL : SSL_P_URL;
 $url_parsed = parse_url($paypal_url);

 $post_string = '';
 foreach ($_REQUEST as $field => $value) {
 $post_string .= $field . '=' . urlencode(stripslashes($value)) . '&';
 }
 $post_string.="cmd=_notify-validate"; // append ipn command
 // get the correct paypal url to post request to
 $paypal_mode_status = $im_debut_ipn; //get_option('im_sabdbox_mode');
 if ($paypal_mode_status == true)
 $fp = fsockopen('ssl://www.sandbox.paypal.com', "443", $err_num, $err_str, 60);
 else
 $fp = fsockopen('ssl://www.paypal.com', "443", $err_num, $err_str, 60);

$ipn_response = '';

if (!$fp) {
// could not open the connection. If loggin is on, the error message
// will be in the log.
 $ipn_status = "fsockopen error no. $err_num: $err_str";
 if ($im_debut_ipn == true) {
 echo 'fsockopen fail';
 }
 return false;
 } else {
// Post the data back to paypal
 fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
 fputs($fp, "Host: $url_parsed[host]\r\n");
 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
 fputs($fp, "Content-length: " . strlen($post_string) . "\r\n");
 fputs($fp, "Connection: close\r\n\r\n");
 fputs($fp, $post_string . "\r\n\r\n");

// loop through the response from the server and append to variable
 while (!feof($fp)) {
 $ipn_response .= fgets($fp, 1024);
 }
 fclose($fp); // close connection
 }

// Invalid IPN transaction. Check the $ipn_status and log for details.
 if (!preg_match("/VERIFIED/s", $ipn_response)) {
 $ipn_status = 'IPN Validation Failed';

if ($im_debut_ipn == true) {
 echo 'Validation fail';
 print_r($_REQUEST);
 }
 return false;
 } else {
 $ipn_status = "IPN VERIFIED";
 if ($im_debut_ipn == true) {
 echo 'SUCCESS';

 }

return true;
 }
 }
 function ipn_response($request){
 mail("info@vertexskysports.com","Order Recieved",print_r($request,true));
 $im_debut_ipn=true;
 if ($this->infotuts_ipn($im_debut_ipn)) {

 // if paypal sends a response code back let's handle it
 if ($im_debut_ipn == true) {
 $sub = 'PayPal IPN Debug Email Main';
 $msg = print_r($request, true);
 $aname = 'infotuts';
 //mail send
 }

// process the membership since paypal gave us a valid +
 $this->insert_data($request);
 }
 }
function issetCheck($post,$key){
if(isset($post[$key])){
$return=$post[$key];
}
else{
$return='';
}
return $return;
}
 function insert_data($request){




$con=mysql_connect("xxx.xxx.xx.xx","USERNAME","PASSWORD") or die("Failed to connect with database!!!!");
mysql_select_db("DATABASENAME", $con); 


$datetime = date("Y-m-d H:i:s");

$order_number = $_POST['custom'];
$receiver_email = $_POST['receiver_email'];
$txn_id = $_POST['txn_id'];
$payer_email = $_POST['payer_email'];
$payer_id = $_POST['payer_id'];
$payer_status = $_POST['payer_status'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address_city = $_POST['address_city'];
$address_country = $_POST['address_country'];
$address_state = $_POST['address_state'];
$address_status = $_POST['address_status'];
$address_country_code = $_POST['address_country_code'];
$address_name = $_POST['address_name'];
$address_street = $_POST['address_street'];
$address_zip = $_POST['address_zip'];
$item_name = $_POST['item_name1'];
$fee = $_POST['mc_fee'];
$amount = $_POST['mc_gross_1'];
$payment_status = $_POST['payment_status'];
$shipping = $_POST['mc_shipping'];


$design = "INSERT INTO orders(datetime, order_number, receiver_email, txn_id, payer_email, payer_id, payer_status, first_name, last_name, address_city, address_country, address_state, address_country_code, address_name, address_street, address_zip, item_name, fee, amount, payment_status, shipping)
VALUES('".$datetime."', '".$order_number."', '".$receiver_email."', '".$txn_id."', '".$payer_email."', '".$payer_id."', '".$payer_status."', '".$first_name."', '".$last_name."', '".$address_city."', '".$address_country."', '".$address_state."', '".$address_country_code."', '".$address_name."', '".$address_street."', '".$address_zip."', '".$item_name."', '".$fee."', '".$amount."', '".$payment_status."', '".$shipping."')";

mysql_query($design);

$design2 = "INSERT INTO order_status(datetime, order_number, status) VALUES('".$datetime."','".$order_number."','Received')";

mysql_query($design2);


 }
 }
 $obj = New PayPal_IPN();
 $obj->ipn_response($_REQUEST);

 ?>

1 个答案:

答案 0 :(得分:0)

测试此类事情的最佳方法是设置您自己的模拟器,以便您可以在浏览器中运行它并在屏幕上查看结果。这将帮助您解决任何潜在的SQL插入问题,丢失数据或可能导致问题的任何问题。

因此,只需设置一个HTML表单,其中隐藏的字段与您希望从IPN获得的字段相匹配。在浏览器中直接将其提交给您的听众。

当然,请确保处理数据不是来自PayPal的事实,因此它不会验证,但您可以设置逻辑来相应地处理。

我在撰写有关how to test PayPal IPN的文章中提供了更多详细信息。