php - PayPal IPN working on sandbox but not live -
i've got paypal ipn script, tested in sandbox mode , works when use in live mode doesn't work. no errors or other. should fix 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 $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 $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 paypal validate ipn message: " . curl_error($ch) . php_eol, 3, log_file); } curl_close($ch); exit; } else { // log entire http response if debug 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) ." 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 = "sandboxemail@email.com"; } else { $rec_email = "mypaypalemail@email.com"; } 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 `$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 = "mydomain@email.com"; $mail->password = "******"; $mail->fromname = "mydomain"; $mail->subject = "mydomain | redeem code"; $mail->body = " redeem code ".$code." use code click here: $root_link/webshop/red.php?code=".$code."&step=step2 thank have given support donation mydomain, appreciate it! mydomain"; $mail->addaddress($payer_email); if(!$mail->send()){ echo "mailer error. contact our support team giving them error: " . $mail->errorinfo; } echo "email sent $payer_email redeem code. if didn't received email go <a href='./red.php'>there</a> redeem code."; } else { die("you trying pay 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 several times in sandbox mode each time set live , customers try buy works exept ipn confirmation don't anything. suggestions on how make better checks ensure payment has not been processed or not fake one. thanks!
p.s. i'm aware of mysql extension of php being deprecated that's not main problem.
didn't read code, know me looked , there 2 different itns: 1 live, , 1 sandbox.
had set them both
hope helps
Comments
Post a Comment