Hey all,
I’m working on my own PayPal IPN Listener, its nowhere near finish but i have got a weird problem
Here is the class:
<?php
class IPNListener {
const PAYPAL_REFUNDED = 0;
const PAYPAL_COMPLETED = 1;
const PAYPAL_REVERSED = 2;
const PAYPAL_OTHER = 9;
private $req;
private $errorString;
private $IsValid;
private $receiver_email;
private $receiver_id;
private $txn_id;
private $payer_email;
private $payer_id;
private $first_name;
private $last_name;
private $address_country;
private $address_country_code;
private $address_status;
private $address_street;
private $address_zip;
private $custom;
private $mc_gross;
private $payment_date;
private $payment_status;
private $mc_currency;
private $item_name;
private $item_number;
private $parent_txn_id;
private $expectedReceiver_email = "";
private $expectedCurrency = "";
public $developerMode = false;
public $debugInfoEnabled = false;
private $debugData = "";
public function __construct() {
}
public function Listen(){
$this->Debug("Listening...");
$this->errorString = NULL;
$header = "";
$this->req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$this->req .= "&$key=$value";
}
$this->Debug("Posting back to paypal");
// post back to PayPal system to validate
if($this->developerMode){
$this->Debug("We're in developer mode");
$header .= "POST /cgi-bin/webscr HTTP/1.0\\r\
";
$header .= "Content-Type: application/x-www-form-urlencoded\\r\
";
$header .= "Content-Length: " . strlen($this->req) . "\\r\
\\r\
";
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
}else{
$header .= "POST /cgi-bin/webscr HTTP/1.0\\r\
";
$header .= "Content-Type: application/x-www-form-urlencoded\\r\
";
$header .= "Content-Length: " . strlen($this->req) . "\\r\
\\r\
";
$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
}
if (!$fp) {
$this->Debug("Got some weird HTTPS error, unable to open socket with paypal for valiation");
$this->NewError("HTTPS Error, could not open socket back to paypal");
return;
}
fputs($fp, $header . $this->req);
while (!feof($fp)) {
$res = fgets($fp, 1024);
if (strcmp($res, "VERIFIED") == 0) {
$this->Debug("Is valid");
$this->IsValid = true;
}else if (strcmp ($res, "INVALID") == 0) {
$this->IsValid = false;
$this->Debug("Its not valid");
$this->NewError("Fake IPN Notifiction");
return;
}
}
$this->Debug("Closing Socket.");
fclose($fp);
//Information about Heliosbots
$this->Debug("Getting all the info....");
$this->receiver_email = mysql_real_escape_string($_POST['receiver_email']); //Heliosbots Email address. This should match whats register on paypal
$this->receiver_id = mysql_real_escape_string($_POST['receiver_id']); // Helios PP id.
$this->txn_id = mysql_real_escape_string($_POST['txn_id']); //PayPal Transaction ID
echo $this->txn_id;
//Information about the buyer
$this->payer_email = mysql_real_escape_string($_POST['payer_email']); // payers paypal email address.
$this->payer_id = mysql_real_escape_string($_POST['payer_id']); // payers id.
$this->payer_status = mysql_real_escape_string($_POST['payer_status']); // payer status - have they been verified??
$this->first_name = mysql_real_escape_string($_POST['first_name']); // buyers first name
$this->last_name = mysql_real_escape_string($_POST['last_name']); // buyers last name
$this->address_country = mysql_real_escape_string($_POST['address_country']); // buyers country
$this->address_country_code = mysql_real_escape_string($_POST['address_country']); // buyers country CODE, EG: US,UK
$this->address_status = mysql_real_escape_string($_POST['address_status']); // buyers address status? verified??
$this->address_street = mysql_real_escape_string($_POST['address_street']); // buyers address street
$this->address_zip = mysql_real_escape_string($_POST['address_zip']); // zip / postcode of buyer
$this->custom = mysql_real_escape_string($_POST['custom']); // Should be the order number we send to paypal.
//
//Information about the payment
$this->mc_gross = mysql_real_escape_string($_POST['mc_gross']); //HOW MUCH!?!?!!
$this->payment_date = mysql_real_escape_string($_POST['payment_date']); // payment date - 0:12:59 Jan 13, 2009 PST
$this->payment_status = mysql_real_escape_string($_POST['payment_status']); // payment status - "Completed"
$this->mc_currency = mysql_real_escape_string($_POST['mc_currency']); // GBP USD EUR etc etc
$this->item_name = mysql_real_escape_string($_POST['item_name']); //
$this->item_number = mysql_real_escape_string($_POST['item_number']); // our product ID.
$this->parent_txn_id = mysql_real_escape_string($_POST['parent_txn_id']);
$this->Debug("Done getting info");
}
public function HasErrors() {
return $this->errorString != NULL;
}
private function NewError($errMsg){
$this->errorString = $errMsg;
}
public function DumpToFile(){
$file = ".html";
$fp = fopen($file, 'w');
fwrite($fp, "<br /><br /><p>Error String:".$this->GetErrorString(false)."</p>");
fwrite($fp, $this->debugData);
fclose($fp);
}
public function GetStatus(){
if(strcmp(strtoupper($this->payment_status), "COMPLETED") == 0){
return self::PAYPAL_COMPLETED;
}else if(strcmp(strtoupper($this->payment_status), "REFUNDED") == 0){
return self::PAYPAL_REFUND;
}else if(strcmp(strtoupper($this->payment_status), "REVERSED") == 0){
return self::PAYPAL_REVERSED;
}else{
return self::PAYPAL_OTHER;
}
}
public function GetReq(){
return $this->req;
}
public function GetErrorString($realEscapeIt = false){
if($realEscapeIt)
return mysql_real_escape_string ($this->errorString);
return $this->errorString;
}
public function GetIsValid(){
return $this->IsValid;
}
public function GetReceiverEmail(){
return $this->receiver_email;
}
public function GetReceiverId(){
return $this->receiver_id;
}
public function GetTxnId(){
return $this->txn_id;
}
public function GetPayerEmail(){
return $this->payer_email;
}
public function GetPayerId(){
return $this->payer_id;
}
public function GetFirstName(){
return $this->first_name;
}
public function GetLastName(){
return $this->last_name;
}
public function GetAddressCountry(){
return $this->address_country;
}
public function GetAddressCountryCode(){
return $this->address_country_code;
}
public function GetAddress_Status(){
return $this->address_status;
}
public function GetAddressStreet(){
return $this->address_street;
}
public function GetAddressZip(){
return $this->address_zip;
}
public function GetCustom(){
return $this->custom;
}
public function GetMcGross(){
return $this->mc_gross;
}
public function GetPaymentDate(){
return $this->payment_date;
}
public function GetPaymentStatus(){
return $this->payment_status;
}
public function GetMcCurrency(){
return $this->mc_currency;
}
public function GetItemName(){
return $this->item_name;
}
public function GetItemNumber(){
return $this->item_number;
}
public function GetParentTxnId(){
return $this->parent_txn_id;
}
public function ReceiverEmailIsValid(){
return strcmp(strtoupper($this->receiver_email), strtoupper($this->expectedReceiver_email)) == 0;
}
public function IsExpectedCurrency(){
return strcmp(strtoupper($this->mc_currency), strtoupper($this->expectedCurrency)) == 0;
}
public function SetError($string){
$this->errorString = $string;
}
public function Debug($txt){
if($this->debugInfoEnabled){
$this->debugData .= "<p>".$txt."</p>";
}
}
}
?>
Initialize it like so.
$ipn = new IPNListener();
$ipn->developerMode = true;
$ipn->debugInfoEnabled = true;
$ipn->Listen();//Will grab all the post stuff, send it back to paypal
$ipn->DumpToFile();// temporary dump to file, skipping database stuff.
Now the problem i have is simple, yet confusing.
I have the following line of code inside the Listen() function
$this->txn_id = mysql_real_escape_string($_POST['txn_id']); //PayPal Transaction ID
echo $this->txn_id."<br />";//Doesn't display anything
echo mysql_real_escape_string($_POST['txn_id']);//Do display the txn id.
If i try and echo the $this-> part of above it does nothing even though i just set it? If i echo directly from $_POST it displays just fine.
I don’t understand how i can set a variable/field/property and then not be able to access it directly after?
Hope that make sense?
Thanks