index.php
<?php
//Paypal IPN Tutorial
// http://jream.com
/**
How it Works
1: A user buys something from your BUY button
2: The BUY button is configured with a URL for PayPal to go to when complete
3: The CALLBACK is on our site (This Page). It queries Paypal to see the result of the transaction just made.
4: If it is good, we update some kind of record.
**/
require 'Paypal_IPN.php';
echo "done";
$paypal = new Paypal_IPN('sandbox');
echo "1";
$paypal->run();
echo "2"; // does not echo 2
?>
Paypal_IPN.php
<?php
class Paypal_IPN
{
/** @var string $_url The paypal url to go to through cURL*/
private $_url;
/**
* @param string $mode 'live' or 'sandbox'
*/
public function __construct($mode = 'live')
{
if ($mode == 'live')
$this->_url = 'https://www.paypal.com/cgi-bin/webscr';
else
$this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
public function run()
{
$postFields = 'cmd=_notify-validate';
foreach($_POST as $key => $value)
{
$postFields .= "&$key=".urlencode($value);
}
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $this->_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postFields
));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
}
?>
It doesn't echo "2" at index.php. I'm running this on my own computer with apache and an actual site pointing to my IP address.