未收到 Paypal IPN Sandbox 的响应

2021-12-29 00:00:00 php paypal paypal-ipn paypal-sandbox

我正在我的网站上使用 paypal 结账,但我对听众失望了.对于那些不熟悉 Paypal IPN 系统的人,基本上 Paypal 会向您的脚本发送一条有关交易的消息,然后您将添加的几位发回.如果 Paypal 收到正确的回复,它会回复VERIFIED",否则回复INVALID".

I'm putting a paypal checkout onto my website but am falling down with the listener. For those of you who are unfamiliar with the Paypal IPN system, basically Paypal sends your script with a message about the transaction, which you send back with a couple of bits added. If Paypal receives the correct reply, it'll reply with 'VERIFIED', and if not it'll say 'INVALID'.

我已经成功了第一点.我的代码能够从贝宝接收信息,添加额外内容并将其发回.但是,我没有收到沙箱的回复,说已验证"或无效".我几乎从贝宝网站复制了我的代码,所以我希望这会相当简单,所以如果你能花点时间看看我的代码,也许一些新人可以找出我哪里出错了.

I've succeeded with the first bit. My code is able to receive the info from paypal, add on the extras and post it back. However, I get no response from the Sandbox saying either 'VERIFIED' or 'INVALID'. I've pretty much copied my code from the paypal website so I was hoping this was going to be fairly straightforward, so if you could take a minute to look at my code, perhaps some new eyes could pick out where I've gone wrong.

这是代码.没什么特别的,它实际上只是获取信息,调整它,将其传回并读取响应(它要么没有得到,要么没有意识到它正在得到)

Here's the code. Nothing special, it literally just gets the info, adjusts it, passes it back and reads the response (which it either isn't getting or doesn't realise it's getting)

<?php

$debug=true;

//Put together postback info

$postback = 'cmd=_notify-validate';

foreach($_POST as $key =>$value){
     $postback .= "&$key=$value";
}

// build the header string to post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.1
";
$header .= "Host: www.sandbox.paypal.com
";
$header .= "Content-Type: application/x-www-form-urlencoded
";
$header .= "Content-Length: " . strlen($postback) . "

";

$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);//open the connection

if(!$fp){ //no conn
    die();
}

//post data back
fputs($fp, $header . $postback);

while(!feof($fp)){

    $res=fgets ($fp, 1024);

    if((strcmp($res, "VERIFIED")) == 0){ //verified!
        if($debug){         
            $filename = 'debug/debug5_verified.txt'; //create a file telling me we're verified
            $filehandle=fopen($filename, 'w');
            fwrite($filehandle,'VERIFIED!');
            fclose($filehandle);
        }
    }
}

?>

提前致谢!

推荐答案

所以我想我找到了解决方案.事实证明,连接到 ssl://sandbox .... 没有问题,它实际上是在检索答案.代码被挂断了

So I think I found a solution. Turns out it wasn't having trouble with connecting to ssl://sandbox...., it was actually retrieving the answer. The code was getting hung up on the

while(!feof($fp)){
    $res=fgets($fp,1024);
}

位.我所做的只是将其替换为:

bit. All I did was replace it with:

$res=stream_get_contents($fp, 1024);

第一次就成功了!现在我可以继续我的生活了.再次感谢您对此的所有帮助.

and it worked first time! Now I can get on with my life. Thanks again for all the help on this one.

相关文章