2017-03-03 125 views
1

我试图让PayPal IPN设置并且无法弄清楚我下面写的代码有什么问题。当我尝试使用Paypal的Sandbox IPN模拟器进行测试时,它返回“IPN未发送,握手未验证,请检查您的信息。”我认为问题是需要将标题改为sanbox freindly格式,但无法弄清楚我需要更改哪些内容。试图用沙盒测试Paypal IPN,但握手不通过

这是该代码是下网址: 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

尝试改变这一点:

//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); 
0

可以使用老贝宝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只为一类,但是这是你的选择反正:)

+0

也为过去几天的'notify_url'覆盖你的按钮/链接不起作用(看起来)。自从上一次亚马逊/ GoDaddy /其他花哨的大型“云”中断后,我没有收到IPN通知。不是说它是相关的,只是标记当下。 – kodeart

+0

你不需要整个SDK,你可以在这里获得IPN代码https://github.com/paypal/ipn-code-samples/tree/master/php最近几天,IPN模拟器还没有工作对我来说,但是我在向paypal.com或sandbox.paypal.com进行付款时收到了IPN通知 – JBart