I’ve been trying to figure out what’s wrong with my ipn script. I’ve tried many different methods and I also tried fsockopen but I didnt get it working. Now I’m using cURL and it still isn’t working. I’ve cURL installed and it should be working properly.
My script (Just a simple example, i removed some checks just for testing):
<?php
$ACTIVE_CONNECTION = mysql_connect('127.0.0.1', 'mysqluser', 'mysqlpassword') or die("Could not connect to server.");
mysql_select_db('mysqldb', $ACTIVE_CONNECTION) or die("Could not connect to database.");
$tid = $_GET['tx'];
$auth_token = "aqiopfsawdaisytrgkl";
$url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
$post_vars = "cmd=_notify-synch&tx=" . $tid . "&at=" . $auth_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'cURL/PHP');
$fetched = curl_exec($ch);
$lines = explode("\n", $fetched);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
$payment_amount = $keyarray['mc_gross'];
$payment_status = $keyarray['payment_status'];
$payment_currency = $keyarray['mc_currency'];
$txn_id = $keyarray['txn_id'];
$receiver_email = $keyarray['receiver_email'];
$payer_email = $keyarray['payer_email'];
$username = mysql_real_escape_string($keyarray['custom']);
if ($payment_status == 'Completed'){
mysql_query("UPDATE `users` SET vip = '1' WHERE `username` = '".$username."'");
}
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// manual investigation here
}
?>
Try this to test Sanbox IPN
https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session
and
replace
with