我正在尝试设置Paypal IPN,但无法弄清楚我在下面编写的代码有什么问题。当我尝试使用Paypal的沙盒IPN模拟器进行测试时,它会返回“未发送IPN,并且未验证握手。请检查您的信息。”我认为问题是需要将标题更改为sanbox freindly格式的问题,但无法弄清楚我需要更改的内容。
这是代码所在的URL: http://pmoore17.altervista.org/TWADrama/ticketsales1.php
任何帮助表示赞赏!谢谢!
<?php
// Read the notification from PayPal which comes in the form of a POST array and create the acknowledgement response
$req = 'cmd=_notify-validate'; // add 'cmd' to beginning of the acknowledgement you send back to PayPal
foreach ($_POST as $key => $value)
{ // Loop through the notification NV pairs
$value = urlencode(stripslashes($value)); // Encode the values
$req .= "&$key=$value"; // Add the NV pairs to the acknowledgement
}
// Assign the paypal payment notification values to local variables
if($_POST){
$last_name = $_POST['last_name'];
$first_name = $_POST['first_name'];
$quantity = $_POST['quantity'];
$payer_email = $_POST['payer_email'];}
//Set up the acknowledgement request headers (this is the updated version for http 1.1)
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Connection: close\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
//Open a socket for the acknowledgement request
$fq = fsockopen ('www.paypal.com/cgi-bin/webscr', 80, $errno, $errstr, 30);
if(!$fq){
echo "HTTP ERROR";
}
else
{//start 1
// Post request back to PayPal for validation
fputs ($fq, $header . $req);
//once paypal receives the acknowledgement response, another message will be send containing the single word VERIFIED or INVALID
while (!feof($fq))
{ //start 2, while not EndOfFile
$res = fgets ($fq, 1024); // Get the acknowledgement response
$res = trim($res);
if (strcmp ($res, "VERIFIED") == 0)
{// start 3, Response is OK
$fn = "content.txt";
$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");
fputs($fp,"last_name: ".$last_name.", ");
fputs($fp,"first_name: ".$first_name.", ");
fputs($fp,"quantity: ".$quantity.", ");
fputs($fp,"payer_email: ".$payer_email."\n");
fclose($fp) or die ("Error closing file!");
}//end 3
else if(strcmp ($res, "INVALID") == 0)
{//start 4
$fn = "content.txt";
$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");
fputs($fp,"last_name: ".$last_name.", ");
fputs($fp,"first_name: ".$first_name.", ");
fputs($fp,"quantity: ".$quantity.", ");
fputs($fp,"payer_email: ".$payer_email."\n");
fclose($fp) or die ("Error closing file!");
}//end 4
} //end 2
fclose ($fq); //close file pointer
} //end 1
?>
答案 0 :(得分:0)
尝试更改此内容:
//Open a socket for the acknowledgement request
$fq = fsockopen ('www.paypal.com/cgi-bin/webscr', 80, $errno, $errstr, 30);
对此:
//Open a socket for the acknowledgement request
$fq = fsockopen ('ssl://www.sandbox.paypal.com/cgi-bin/webscr', 443, $errno, $errstr, 30);
答案 1 :(得分:0)
您可以使用旧的Paypal SDK 验证传入通知,而不是制作您自己的(通常是错误的)解决方案。有作曲家:
$ composer require paypal/adaptivepayments-sdk-php
或手动下载SDK。然后使用PPIPNMessage
类验证它:
// you can omit both arguments, mode defaults to "live"
$ipn = new PPIPNMessage('', ['mode' => 'sandbox']);
$valid = $ipn->validate(); // returns bool
$data = $ipn->getRawData(); // returns the IPN notification as array
// continue with things
将整个SDK仅用于一个课程可能太过分了,但无论如何这都是您的选择:)