I have the following IPN script which I'm using to process a payment from a Buy Now button. When I run the script in the Paypal Sandbox, it comes back as being received correctly. I receive an email which lists all of the posted variables (just using this to debug at the moment), but it posts an entry in the IPN Errors table I've created in the DB with an Error Number 0 and no Error message. No other code appears to be executed (such as the call to the process_payment function). I've run the process payment function on it's own and it works correctly, and I know that the variables being parsed into it are also correct. The script is shown below:
<?php
class Paypal extends Controller
{
public function __construct()
{
// Load the Registration Model
$this->load_model('Registration');
// Load the Error Model
$this->load_model('Ipn_Error');
}
public function process_payment($Post)
{
if (!empty($Post['custom']))
{
// This is the User ID
$User_ID = $Post['custom'];
// Check that the User Exists
if ($this->registration->check_user_by_id($User_ID))
{
if (!empty($Post['item_number']))
{
if ($Post['item_number'] == 'M') {
// Set the Package Type
$Package_Type = 'M';
// Process the Payment
$this->registration->process_initial_payment($User_ID, $Package_Type);
}
else if ($Post['item_number'] == 'Y') {
// Set the Package Type
$Package_Type = 'Y';
// Process the Payment
$this->registration->process_initial_payment($User_ID, $Package_Type);
}
}
}
}
}
public function index()
{
// First, check that we're dealing with a Posted Request
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// Create a Blank Header
$Header = '';
// Create a Request String
$Request_String = 'cmd=_notify-validate';
$eml = "";
// Loop through and add the Variables
foreach($_POST as $Key=>$Value)
{
$eml .= "$Key : $Value
";
// URL Encode the Value
$Value = urlencode($Value);
// Add it to the Request String
$Request_String .= '&'.$Key.'='.$Value;
}
mail('me@andypopsmedia.com', 'RESPONSE', $eml);
// Post back to PayPal to validate
$Header .= "POST /cgi-bin/webscr HTTP/1.0
";
$Header .= "Content-Type: application/x-www-form-urlencoded
";
$Header .= "Content-Length: ".strlen($Request_String)."
";
// Use a Socket Connect to Send Data back to Paypal
$FP = fsockopen('ssl://'.paypal_ipn, 80, $Error_No, $Error_Str, 30);
// Begin Handling the Transaction Stuff
if (!$FP)
{
// HTTP ERROR
// HANDLE WITH CARE
$this->ipn_error->log_error($Error_No, $Error_Str);
}
else
{
// Post the Data back
fwrite($FP, $Header.$Request_String);
$this->ipn_error->log_error(3, 'Response Obtained');
// Get the Response and Handle it
while (!feof($FP))
{
// Get the Result String
$Result_String = fgets($FP, 1024);
$this->ipn_error->log_error(3, $Result_String);
// Check if it was Successful
if (strcmp($Result_String, 'VERIFIED') == 0)
{
// Check the Transaction Type
switch ($_POST['txn_type'])
{
case ('web_accept'):
$this->ipn_error->log_error(123, 'Transaction done.');
$this->process_payment($_POST);
break;
}
}
else
{
// VERIFICATION ERROR
// HANDLE WITH CARE
mail('me@andypopsmedia.com', 'RESPONSE FAILED', 'it did not work');
}
}
}
}
// ELSE NOT POSTED
else
{
echo 'the file works';
//$this->registration->process_initial_payment(1, 'M');
$this->ipn_error->log_error(123, 'File Accessed');
}
}
};
?>