I've got this PayPal IPN script, tested it in sandbox mode and it works perfectly but when I use it in live mode it doesn't work. No errors or other. What should I do to fix this issue?
<?php
define("DEBUG", 0);
define("USE_SANDBOX", 0);
define("LOG_FILE", "./ipn.log");
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
if(USE_SANDBOX == true) {
$paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
$paypal_url = "https://www.paypal.com/cgi-bin/webscr";
}
$ch = curl_init($paypal_url);
if ($ch == FALSE) {
return FALSE;
}
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if(DEBUG == true) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$cert = __DIR__ . "./cacert.pem";
curl_setopt($ch, CURLOPT_CAINFO, $cert);
$res = curl_exec($ch);
if (curl_errno($ch) != 0)
{
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
}
curl_close($ch);
exit;
} else {
// Log the entire HTTP response if debug is switched on.
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
}
curl_close($ch);
}
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp ($res, "VERIFIED") == 0) {
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$payment_status = $_POST['payment_status'];
$item_number = $_POST['item_number'];
if($payment_status == 'Completed') {
require_once("../assets/includes/core.php");
require_once("../assets/includes/settings.php");
if(USE_SANDBOX == true) {
$rec_email = "[email protected]";
} else {
$rec_email = "[email protected]";
}
if($receiver_email == $rec_email) {
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db('website');
function generateString($length)
{
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
for($i=0; $i<$length; $i++)
$key .= $charset[(mt_rand(0,(strlen($charset)-1)))];
return $key;
}
$code = generateString("30");
$insert_code = mysql_query("INSERT INTO `$dbtable_wsc` (`code`,`payer_email`,`item_number`,`used`,`gen_date`) VALUES('$code','$payer_email','$item_number','no', NOW())") or die("MYSQL INSERT CODE QUERY ERROR: " . mysql_error());
mysql_close($con);
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "[email protected]";
$mail->Password = "******";
$mail->FromName = "mydomain";
$mail->Subject = "mydomain | Redeem Code";
$mail->Body = "
Your redeem code is ".$code."
To use your code click here: $root_link/webshop/red.php?code=".$code."&step=step2
Thank you for have given your support with a donation to mydomain, we really appreciate it!
mydomain";
$mail->AddAddress($payer_email);
if(!$mail->Send()){
echo "Mailer Error. Contact our Support Team giving them this error: " . $mail->ErrorInfo;
}
echo "Email sent to $payer_email with the redeem code. If you didn't received your email go <a href='./red.php'>there</a> to redeem you code.";
} else {
die("You are trying to pay to the wrong email!");
}
}
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
}
} else if (strcmp ($res, "INVALID") == 0) {
if(DEBUG == true) {
error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
}
}
?>
Tested this several times in sandbox mode but each time I set this live and my customers try to buy something all works exept the IPN confirmation so they don't get anything. I also would like some suggestions on how I could make better checks for ensure the payment has not been already processed or is not a fake one. Thanks!
P.S. I'm aware of the mysql extension of php being deprecated but currently that's not my main problem.
Didn't read the code, but I know for me I looked and there was two different ITNs: one for live, and one for sandbox.
Had to set them both
Hope that helps