这两天一直在写paypal, 现在就快把ipn.php整完了,但遇到一个问题让我很费解啊!
session在ipn.php里面只有一部分有效,看代码<?php
//ipn.php全部代码
session_start();
include('ipnlistener.php');
include_once 'lib/functions.php';
ini_set('display_errors', 'Off');
$db = new Connection();
$connection = $db->connect();$listener = new IpnListener();
$listener->use_sandbox = true;$order_time=$_SESSION['order_time_org'];
$username=$_SESSION['user'];
echo "$order_time, $username";//到这里位置以上,SESSION是可以用的 上面输出两个都能显示出来
//下面就显示不出来了!这是为什么?try {
$verified = $listener->processIpn();
} catch (Exception $e) {
// fatal error trying to process IPN.
exit(0);
}if ($verified)
{
// IPN response was "VERIFIED"
//send email form
$invoice_id=$_POST['invoice'];
$payment_status = $_POST['payment_status'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
//$order_time = $_POST['custom'];
//$whimwin_user=$_SESSION['user'];
//这里的$_SESSION['user']显示不出来啊!
//if($payment_status=="Completed")
//{
$query="UPDATE CART SET payment_status='paid' WHERE username='$username' AND order_time='$order_time' AND invoice_id='$invoice_id'";
$result=queryMysql($query);
if($result)
{
$to="[email protected]";//payer_email
//subject
$subject="Whim Win paid successful!";
//from
$header="from: test <[email protected]>";
//message body
$message="Dear Member, \n\n";
$message.="this is you invoice id $invoice_id \n";
$message.="$payment_status, $order_time, $payer_email, $receiver_email, $username";
//send email
$sendmail=mail($to,$subject,$message,$header); }
//}} else {
// IPN response was "INVALID"
}
$db->close();
?>
session在ipn.php里面只有一部分有效,看代码<?php
//ipn.php全部代码
session_start();
include('ipnlistener.php');
include_once 'lib/functions.php';
ini_set('display_errors', 'Off');
$db = new Connection();
$connection = $db->connect();$listener = new IpnListener();
$listener->use_sandbox = true;$order_time=$_SESSION['order_time_org'];
$username=$_SESSION['user'];
echo "$order_time, $username";//到这里位置以上,SESSION是可以用的 上面输出两个都能显示出来
//下面就显示不出来了!这是为什么?try {
$verified = $listener->processIpn();
} catch (Exception $e) {
// fatal error trying to process IPN.
exit(0);
}if ($verified)
{
// IPN response was "VERIFIED"
//send email form
$invoice_id=$_POST['invoice'];
$payment_status = $_POST['payment_status'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
//$order_time = $_POST['custom'];
//$whimwin_user=$_SESSION['user'];
//这里的$_SESSION['user']显示不出来啊!
//if($payment_status=="Completed")
//{
$query="UPDATE CART SET payment_status='paid' WHERE username='$username' AND order_time='$order_time' AND invoice_id='$invoice_id'";
$result=queryMysql($query);
if($result)
{
$to="[email protected]";//payer_email
//subject
$subject="Whim Win paid successful!";
//from
$header="from: test <[email protected]>";
//message body
$message="Dear Member, \n\n";
$message.="this is you invoice id $invoice_id \n";
$message.="$payment_status, $order_time, $payer_email, $receiver_email, $username";
//send email
$sendmail=mail($to,$subject,$message,$header); }
//}} else {
// IPN response was "INVALID"
}
$db->close();
?>
$whimwin_user=$_SESSION['user'];
我头发都快愁白了
为什么不行呢啊!
其他页面都能使用session,应该不是php.ini设置问题吧?
{
你能确认进入这个分支了吗?
直接写在session下面,echo一下能输出,但是在if($verified)里面echo就什么也显示不出来,很奇怪啊
确定进入了,我if($verified)里面有发邮件到我邮箱,我收到了,
邮箱信息是 $message.="$payment_status, $order_time, $payer_email, $receiver_email, $username";
//只有$username 是空白的,其他都有内容
$verified = $listener->processIpn();
} 这个方法里面什么什么?
try {
print_r($_SESSION);
$verified = $listener->processIpn();
print_r($_SESSION);
} catch (Exception $e) {
// fatal error trying to process IPN.
exit(0);
}
print_r($_SESSION);
$verified = $listener->processIpn();
这样是有显示的,
$verified = $listener->processIpn();
print_r($_SESSION);
这样就是空白的,看来是调用出问题了,我把代码贴上来,替我看看呗?<?php
/**
* PayPal IPN Listener
*
* A class to listen for and handle Instant Payment Notifications (IPN) from
* the PayPal server.
*
* https://github.com/Quixotix/PHP-PayPal-IPN
*
* @package PHP-PayPal-IPN
* @author Micah Carrick
* @copyright (c) 2011 - Micah Carrick
* @version 2.0.5
* @license http://opensource.org/licenses/gpl-3.0.html
*/
class IpnListener {
public $use_curl = true;
public $force_ssl_v3 = true; public $follow_location = false;
public $use_ssl = true;
public $use_sandbox = false;
public $timeout = 30;
private $post_data = array();
private $post_uri = '';
private $response_status = '';
private $response = ''; const PAYPAL_HOST = 'www.paypal.com';
const SANDBOX_HOST = 'www.sandbox.paypal.com';
/**
* Post Back Using cURL
*
* Sends the post back to PayPal using the cURL library. Called by
* the processIpn() method if the use_curl property is true. Throws an
* exception if the post fails. Populates the response, response_status,
* and post_uri properties on success.
*
* @param string The post data as a URL encoded string
*/
protected function curlPost($encoded_data) { if ($this->use_ssl) {
$uri = 'https://'.$this->getPaypalHost().'/cgi-bin/webscr';
$this->post_uri = $uri;
} else {
$uri = 'http://'.$this->getPaypalHost().'/cgi-bin/webscr';
$this->post_uri = $uri;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
if ($this->force_ssl_v3) {
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
}
$this->response = curl_exec($ch);
$this->response_status = strval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
if ($this->response === false || $this->response_status == '0') {
$errno = curl_errno($ch);
$errstr = curl_error($ch);
throw new Exception("cURL error: [$errno] $errstr");
}
}
protected function fsockPost($encoded_data) {
if ($this->use_ssl) {
$uri = 'ssl://'.$this->getPaypalHost();
$port = '443';
$this->post_uri = $uri.'/cgi-bin/webscr';
} else {
$uri = $this->getPaypalHost(); // no "http://" in call to fsockopen()
$port = '80';
$this->post_uri = 'http://'.$uri.'/cgi-bin/webscr';
} $fp = fsockopen($uri, $port, $errno, $errstr, $this->timeout);
if (!$fp) {
// fsockopen error
throw new Exception("fsockopen error: [$errno] $errstr");
} $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($encoded_data)."\r\n";
$header .= "Connection: Close\r\n\r\n";
fputs($fp, $header.$encoded_data."\r\n\r\n");
while(!feof($fp)) {
if (empty($this->response)) {
// extract HTTP status from first line
$this->response .= $status = fgets($fp, 1024);
$this->response_status = trim(substr($status, 9, 4));
} else {
$this->response .= fgets($fp, 1024);
}
}
fclose($fp);
}
private function getPaypalHost() {
if ($this->use_sandbox) return IpnListener::SANDBOX_HOST;
else return IpnListener::PAYPAL_HOST;
}
public function getPostUri() {
return $this->post_uri;
}
public function getResponse() {
return $this->response;
}
public function getResponseStatus() {
return $this->response_status;
}
public function getTextReport() {
$r = '';
// date and POST url
for ($i=0; $i<80; $i++) { $r .= '-'; }
$r .= "\n[".date('m/d/Y g:i A').'] - '.$this->getPostUri();
if ($this->use_curl) $r .= " (curl)\n";
else $r .= " (fsockopen)\n";
// HTTP Response
for ($i=0; $i<80; $i++) { $r .= '-'; }
$r .= "\n{$this->getResponse()}\n";
// POST vars
for ($i=0; $i<80; $i++) { $r .= '-'; }
$r .= "\n";
foreach ($this->post_data as $key => $value) {
$r .= str_pad($key, 25)."$value\n";
}
$r .= "\n\n";
return $r;
}
public function processIpn($post_data=null) { $encoded_data = 'cmd=_notify-validate';
if ($post_data === null) {
// use raw POST data
if (!empty($_POST)) {
$this->post_data = $_POST;
$encoded_data .= '&'.file_get_contents('php://input');
} else {
throw new Exception("No POST data found.");
}
} else {
// use provided data array
$this->post_data = $post_data;
foreach ($this->post_data as $key => $value) {
$encoded_data .= "&$key=".urlencode($value);
}
} if ($this->use_curl) $this->curlPost($encoded_data);
else $this->fsockPost($encoded_data);
if (strpos($this->response_status, '200') === false) {
throw new Exception("Invalid response status: ".$this->response_status);
}
if (strpos($this->response, "VERIFIED") !== false) {
return true;
} elseif (strpos($this->response, "INVALID") !== false) {
return false;
} else {
throw new Exception("Unexpected response from PayPal.");
}
}
public function requirePostMethod() {
// require POST requests
if ($_SERVER['REQUEST_METHOD'] && $_SERVER['REQUEST_METHOD'] != 'POST') {
header('Allow: POST', true, 405);
throw new Exception("Invalid HTTP request method.");
}
}
}
?>
//print_r($_SESSION);
$mysession=$_SESSION;
$verified = $listener->processIpn();
$_SESSION=$mysession;
print_r($_SESSION);
还是空白的....
但 $verified = $listener->processIpn(); if(verified) 这块是成功了 我已经收到确定邮件了 只是邮件里面所有关于$_SESSION的都显示不出来
我在try{}catch{}外面 echo “hello world”; 都没显示print_f也没用
try {
$verified = $listener->processIpn();
} catch (Exception $e) {
// fatal error trying to process IPN.
exit(0);
}
[/PHP]
我感觉你的代码执行的是catch历代内容,在catch中加一行输出:
[code=PHP]
catch (Exception $e) {
echo "this false<br>";
// fatal error trying to process IPN.
exit(0);
}
[/PHP]
$listener->processIpn();执行后不见得就返回FALSE,而且你的processIpn()中也没有哪里调用session的。
如果我猜的没错,那么你要搞清楚的一点是,客户支付的过程是走浏览器,所以cookie头可以续传,但是paypal请求你这个页面通告你支付结果这个流程,是通过palpay服务端完成的,比如我们php常用的curl。所以如果你不是显式的传给paypal当前的cookies,然后paypal请求的header头里带上这个cookies,怎么可能读到session呢。
哦 我大概知道是什么意思了。
弱弱的问一句,那我在ipn.php里面写sql语句会执行么?
$db = new Connection();要保证Connection类正确引入。
你把这个文件当做普通的页面,只不过这个页面不是接收来自浏览器的请求,其它的没什么区别。