Hello everyone,
I am creating a test page with a test paypal buy now button. After the user has paid and is being redirected back to my site I want to print a message like “Thank you for paying, continue to the next step >>”
I failed to understand how to realize (by code) that the payment was made or not.
So, here’s my code:
paypal.php (IPN listener)
[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);
$fh = fopen('result.txt', 'w');
fwrite($fh, $result . ' -- ' . $postFields);
fclose($fh);
}
}[/php]
When I use the IPN Simulator the file result.txt is created and the IPN is stored inside.
test.php is the page the user visits and clicks on the “Buy Now” button.
test.php
[php]

// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
// IPN message values depend upon the type of notification sent.
// To loop through the &_POST array and print the NV pairs to the screen:
foreach($_POST as $key => $value) {
echo $key." = ". $value."<br>";
}
echo 'YES! PAYMENT WAS MADE!';
} else if (strcmp ($result, “INVALID”) == 0) {
// IPN invalid, log for manual investigation
echo “The response from IPN was: " .$result .”";
}[/php]
Everything is currently being done a a Paypal sandbox account. Any suggestions?