PayPal 文档让我头晕目眩!
我已经从某个地方复制了示例以在付款完成后设置自动退货,我创建了一个页面success.php
来获取返回的参数,不知何故我没有成功显示这些返回的参数,Paypal 专家可以帮忙检查下面的代码有什么问题吗?
成功.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>
这是自动返回的 URL: