Hi, I hope that someone can help me. I need to use Paypal IPN to verify payments in case the buyer does not return to the website after payment.
I have followed the instructions in the Paypal IPN guide using their demo code as listed below
<?php
error_reporting(E_ALL ^ E_NOTICE);
$email = $_GET['ipn_email'];
$header = "";
$emailtext = "";
// Read the post from PayPal and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc'))
{
$get_magic_quotes_exists = true;
}
foreach ($_POST as $key => $value)
// Handle escape characters, which depends on setting of magic quotes
{
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1){
$value = urlencode(stripslashes($value));
} else {
$value = urlencode($value);
}
$req .= "&$key=$value";
}
// Post back to PayPal to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
// Process validation from PayPal
// TODO: This sample does not test the HTTP response code. All
// HTTP response codes must be handles or you should use an HTTP
// library, such as cUrl
if (!$fp) { // HTTP ERROR
} else {
// NO HTTP ERROR
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// TODO:
// 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
// If 'VERIFIED', send an email of IPN variables and values to the
// specified email address
foreach ($_POST as $key => $value){
$emailtext .= $key . " = " .$value ."\n\n";
}
mail($email, "Live-VERIFIED IPN", $emailtext . "\n\n" . $req);
} else if (strcmp ($res, "INVALID") == 0) {
// If 'INVALID', send an email. TODO: Log for manual investigation.
foreach ($_POST as $key => $value){
$emailtext .= $key . " = " .$value ."\n\n";
}
mail($email, "Live-INVALID IPN", $emailtext . "\n\n" . $req);
}
}
fclose ($fp);
?>
I do not receive any email acknowledgement although everything seems to have worked if I check the IPN history on my Paypal account.
Can anyone help.
Listener code for paypal IPN
Moderator: General Moderators
Re: Listener code for paypal IPN
Have you tested that mail function alone to make sure it works? Is this really where you want the email to go $_GET['ipn_email'] for testing? Are you setting that via the paypal sandbox to point to you?
(PS please use
(PS please use
Code: Select all
tags when you post code so we can read it).-
Granny1984
- Forum Newbie
- Posts: 3
- Joined: Sun Jan 29, 2012 1:08 am
Re: Listener code for paypal IPN
Thank you.
Yes the email address is ok. I have tried putting other actions in code but still nothing happens. I am sure that it must be something quite simple.
I'll keep trying.
Sorry about code format. Its my first posting.
Yes the email address is ok. I have tried putting other actions in code but still nothing happens. I am sure that it must be something quite simple.
I'll keep trying.
Sorry about code format. Its my first posting.
Re: Listener code for paypal IPN
If you are testing in the PayPal sand box you have to repost to http://www.sandbox.paypal.com not http://www.paypal.com
Also for testing I suggest you write to a debug file in your IPN code near the beginning. This snippet will show you what PayPal is posting to your script. You can expand the debugging from there if you want more debug data.
Also for testing I suggest you write to a debug file in your IPN code near the beginning. This snippet will show you what PayPal is posting to your script. You can expand the debugging from there if you want more debug data.
Code: Select all
$log = fopen("ipn_debug.txt", "a");
fwrite($log, "\nIPN - " . gmstrftime("%b %d %Y %H:%M:%S", time()) . "\n");
foreach ($_POST as $key => $value) {
fwrite($log,"$key: $value\n");
}
fclose($log);
-
Granny1984
- Forum Newbie
- Posts: 3
- Joined: Sun Jan 29, 2012 1:08 am
Re: Listener code for paypal IPN
Thanks Eric.
I have managed to sort out the problem.
There were two errors.
The first was an ommission of a '}' in the code that I had copied from the Paypal IPN guide.
The second was that my server requires
ini_set("sendmail_from", " email@mydomainname ");
in order for the mail function to work properly.
I have managed to sort out the problem.
There were two errors.
The first was an ommission of a '}' in the code that I had copied from the Paypal IPN guide.
The second was that my server requires
ini_set("sendmail_from", " email@mydomainname ");
in order for the mail function to work properly.