PayPal自动返回无法获取params

时间:2013-03-29 07:39:20

标签: php paypal returnurl

PayPal文档让我头晕目眩!

我已经从某个地方复制了样本,在付款完成时设置了自动返回,我创建了一个名为success.php的页面来获取返回的参数,不知何故我没有成功显示那些返回的参数,任何有Paypal专家的人都可以帮助检查以下代码有什么问题?

success.php

<?php
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "ZgTzjTg49XZGeD54WsS";
$req .= "&tx=".$tx_token."&at=".$auth_token;

foreach($_POST as $key => $value){
    $value = urlencode(stripslashes($value));
    $req .= '&'.$key.'='.$value;
}

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: http://www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($req)."\r\n\r\n";

// url for paypal sandbox
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);  


if(!$fp){

    // HTTP ERROR

}else{

    fputs($fp, $header . $req);
    // read the body data
    $res = '';
    $headerdone = false;

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

        if(strcmp($line, '\r\n') == 0){
        // read the header
            $headerdone = true;

            }else if($headerdone){
            // header has been read. now read the contents
                $res .= $line;
            }
    }

    // parse the data
    $lines = explode('\n', $res);
    $keyarray = array();

    if(strcmp($lines[0], 'SUCCESS') == 0){

        for($i=1; $i<count($lines);$i++){
            list($key,$val) = explode('=', $lines[$i]);
            $keyarray[urldecode($key)] = urldecode($val);
        }

        // check the payment_status is Completed
        // check that txn_id has not been previously processed
        // check that receiver_email is your Primary PayPal email
        // check that payment_amount/payment_currency are correct
        // process payment
        $firstname = $keyarray['first_name'];
        $lastname = $keyarray['last_name'];
        $itemname = $keyarray['item_name'];
        $amount = $keyarray['payment_gross'];

        echo '<p><h3>Thank you for your purchase!</h3></p>';
        echo '<b>Payment Details</b><br>';
        echo '<ul>';
        echo '<li>Name: '.$firstname.' '.$lastname.'</li>';
        echo '<li>Item: '.$itemname.'</li>';
        echo '<li>Amount: '.$amount.'</li>';
        echo '</ul>';

    }else if(strcmp($lines[0], 'FAIL') == 0){
        // log for manual investigation
    }

}

fclose($fp);

?>
Your transaction has been completed, and a receipt for your purchase has been emailed to you.<br>

这是自动返回的网址:

  

http://sitename.com/success.php?tx=88L2235578117773M&st=Completed&amt=318.00&cc=USD&cm=2013-23&item_number=&sig=HzS1ku7OeDrZJVZcXta6X9jrf71vct7Spstv0%2bW7lxj%2bqRtQXBDO9pqs%2bhmzaoSGs6EDjpEFCnPwOGYzlZ1FMbyw%2fIpF6%2bVMFCNDBYuLdFTYkWaiwG70IpyEMGFFNh1aJUsn3W%2fQK6ad6HOC7%2fzccQPUebhMSqp4WBw%2bRw%2f4tw4%3d

1 个答案:

答案 0 :(得分:0)

我发现使用Sandbox对最终测试不是很好,所以一旦你确定你的代码正常工作,最好上线,然后用$ 1交易进行测试。

此外,您不会返回所有客户的订单详细信息,因此最好只需检索订单号,并从向Paypal发送详细信息之前创建的数据库记录中填充收据。

检索发票编号的数据将显示为请求(“item_number”)