簡體   English   中英

嘗試使用Sandbox測試Paypal IPN,但握手沒有通過

[英]Trying to test Paypal IPN with Sandbox, but handshake does not go through

我正在嘗試設置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
?>

嘗試改變這個:

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

您可以使用舊的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僅用於一個類太多了,但這無論如何都是你的選擇:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM