0
votes

I have used exactly same code from paypal and try to get response from IPN simulator from Payapl Sandbox. I have set up ipn.php file on my website where I will get IPN response and it has below code. Still I am not getting any mail for either VERIFIED or INVALID status as I stated in below code. So please let me know what is missing or what is wrong in my code so I can correct it.

<?php
$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"; 
}

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
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);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

if( !($res = curl_exec($ch)) ) {
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);

if (strcmp ($res, "VERIFIED") == 0) {
$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'];

mail('[email protected]', 'PayPal IPN', 'Working...');
} 
else if (strcmp ($res, "INVALID") == 0) 
{
// log for manual investigation
mail('[email protected]', 'PayPal IPN', 'NOT Working...');  
}
?>

I have tried to test using test merchant and buyer accounts in paypal sandbox. I can see IPN history under test merchant account. There I can see HTTP response code = 200 so I guess my ipn script is accessed by Paypal. $0.25 amount is debited from buyer account and deposited into merchant account so I guess that part is also working fine.

Now I have passed 2 values in single variable like.

<input type="hidden" name="custom" value="mem001::mempass001">

Then I am trying to receive values of "custom" variable on my ipn.php (ipn script is on this page) page. Using php functions I have separated both values and stored in different variables.

For testing purpose, I am trying to store above values in database but values are not stored into database.

So finally what wrong I am doing so far? And how can i make sure that I am really getting "VERIFIED" status from IPN after all above processes? Please help.

1

1 Answers

2
votes

I would first start by checking your access logs and error logs on your server. Make sure your access logs show that PayPal is actually attempting to POST the data to your script. If the access logs show PayPal posting to your script, then check your error logs to see if there are any errors being generated.

Make sure your server, is configured to all inbound and out bound connections and that you don't have anything that would be blocking access to your system. Another option, would be to set this up on a test sandbox seller account and go through and make a purchase with a test sandbox seller account instead of using the simulator. The one plus side to this is that, when you go through the account and make a payment you will have a record of the IPN in your IPN history. You can then check to verify that PayPal was sending it out. It will also tell you if your server responded back with a 200ok response, or if your server returned a different status other than 200ok.

I have used the same script example that you are which is based on the script at https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623 and this has worked for me in the past. If this doesn't help, let me know and I can try testing it with your script and pulling up one of my PHP scripts that send out an email.