Okay, here is the code I'm using. The original author hasn't been seen in three years and I haven't been able to get a response for a month so I'm asking here. The people here seem to be abel to help me more than anyone else.
This is the ipn.php:
<?php
// === Include the scripts to make this IPN works === //
include('core/class.config.php');
include('core/class.database.php');
include('core/lib/class.paypal.php');
// =====================================//
// Initiate the classes, and establish a DB conection
$Config = new Config;
$Paypal = new Paypal;
$DB = new Database(
$Config->getDbInfo('db_host'),
$Config->getDbInfo('db_port'),
$Config->getDbInfo('db_username'),
$Config->getDbInfo('db_password'),
$Config->getDbInfo('db_name')
);
// Set test mode features (TRUE or FALSE)
$Paypal->testMode(FALSE);
// Lets check to see if we are valid or not
$Paypal->setLogFile('core/logs/ipn_log.txt');
// Check the payment status
$check = $Paypal->checkPayment($_POST);
if($check == TRUE)
{
// We must break down all the fancy stuff to get the account ID
// Format: Item info --- Account: (Account name) (# (account number))
// ex: Item: 5 Shortswords --- Account: wilson212(#6)
$account = explode(" --- ", $_POST['item_name']);
$pre_accountid = $account['1'];
$pre_accountid = str_replace("Account: ", "", $pre_accountid);
$pre_accountid = explode("(#", $pre_accountid);
$accountid = str_replace(")", "", $pre_accountid['1']);
if(isset($_POST['pending_reason']))
{
$pending_reason = $_POST['pending_reason'];
}
else
{
$pending_reason = NULL;
}
if(isset($_POST['reason_code']))
{
$reason_code = $_POST['reason_code'];
}
else
{
$reason_code = NULL;
}
// Do the DB injection here
$DB->query("INSERT INTO `mw_donate_transactions`(
`trans_id`,
`account`,
`item_number`,
`buyer_email`,
`payment_type`,
`payment_status`,
`pending_reason`,
`reason_code`,
`amount`,
`item_given`)
VALUES(
'".$_POST['txn_id']."',
'".$accountid."',
'".$_POST['item_number']."',
'".$_POST['payer_email']."',
'".$_POST['payment_type']."',
'".$_POST['payment_status']."',
'".$pending_reason."',
'".$reason_code."',
'".$_POST['mc_gross']."',
'0'
)
");
}
?>
The line $Paypal->testMode(FALSE); doesn't seem to care one way or the other if it's TRUE or FALSE for writing to the log file.
And here is the class.paypal.php
<?php
class Paypal
{
var $isTest = FALSE;
// ************************************************************
// Adds variables to the form
function addVar($var,$value)
{
$this->vars[$var][0] = $var;
$this->vars[$var][1] = $value;
}
// ************************************************************
// Sets the button image. 1 = Donate, 2 = Buy now, 3 = custom
function setButtonType($type, $button_image = "")
{
switch($type)
{
// Donate
case 1:
$this->button = '<input type="image" src="images/donate.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">';
break;
// Buy now
case 2:
$this->button = '<input type="image" src="images/paynow.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">';
break;
// Custom
case 3:
$this->button = '<input type="image" src="'.$button_image.'" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">';
}
$this->button .= "\n";
}
// ************************************************************
// Prints the form with all the hidden posts, and displays the button
function showForm()
{
$url = $this->getAddress();
$form = '<form action="https://'.$url.'/cgi-bin/webscr" method="post" target="_blank" style="display:inline;">'."\n";
foreach($this->vars as $key => $value)
{
$form .= '<input type="hidden" name="'.$value[0].'" value="'.$value[1].'">'."\n";
}
$form .= $this->button;
$form .= '</form>';
echo $form;
}
// ************************************************************
// Setup the log file
function setLogFile($logFile)
{
$this->logFile = $logFile;
}
// ************************************************************
// Writes into the log file, a message
private function writeLog($msg)
{
$outmsg = date('Y-m-d H:i:s')." : ".$msg."<br />\n";
$file = fopen($this->logFile,'a');
fwrite($file,$outmsg);
fclose($file);
}
// ************************************************************
// For the IPN. use to check if payment is valid, and the status
function checkPayment($_POST)
{
$req = 'cmd=_notify-validate';
foreach($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$url = $this->getAddress();
// Headers to post back to paypal
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen('ssl://'.$url, 443, $errno, $errstr, 30);
// $fp = fsockopen ($url, 80, $errno, $errstr, 30);
if (!$fp)
{
return FALSE;
}
else
{
fputs($fp, $header . $req);
// Common mistake by people is ending the loop on the first run
// around, which only gives the header line. There for we are going
// to wait until the loop is ended before returning anything.
$loop = FALSE; # Start by saying the loop is false on a verified return
while(!feof($fp))
{
$res = fgets($fp, 1024);
if(strcmp($res, "VERIFIED") == 0) # If line result length matches VERIFIED
{
$loop = TRUE; # Define the loop contained VERIFIED
}
}
if($loop == TRUE)
{
return TRUE;
}
else
{
if($this->logFile != NULL) # If user defined a log file
{
$err = array();
$err[] = '--- Start Transaction ---';
foreach($_POST as $var)
{
$err[] = $var;
}
$err[] = '--- End Transaction ---';
$err[] = '';
foreach($err as $logerror)
{
$this->writeLog($logerror); # Log for error checking
}
}
return FALSE;
}
fclose ($fp);
}
return false;
}
// ************************************************************
// For use of sandbox
function testMode($value)
{
$this->isTest = $value;
}
// ************************************************************
// Return the address based on if its
function getAddress()
{
if($this->isTest == TRUE)
{
return 'www.sandbox.paypal.com';
}
else
{
return 'www.paypal.com';
}
}
}
?>
I have even tried forcing the logfile to automatically return as TRUE to the ipn.php. I have placed $Paypal->setLogFile('core/logs/ipn_log.txt'); within the check that is supposed to write to the database just to make sure it returns as TRUE, this is even after returning the class.paypal.php to its original state. It does return as true, so why won't it write to the database? I've checked the fields, the database access is the same code I've been using.
PHP Version: 5.3.28
MySQL Version: 5.1.73-community