
April 21st, 2006, 10:01 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 264
Time spent in forums: 1 Day 23 h 22 m 48 sec
Reputation Power: 0
|
|
|
PayPal IPN Grrrrr!!
Hi,
I finally found a IPN script that actually works and I can almost understand. One problem I currently have is it doens't echo anything what ever the result. Here is the code.
PHP Code:
<?
#1 = Live on PayPal Network
#2 = Testing with www.BelaHost.com/pp
$verifymode = "2"; # be sure to change value for testing/live!
# Send notifications to here
$send_mail_to = "info@designanet.co.uk";
# subject of messages
$sysname = "Paypal IPN Transaction";
# Your primary PayPal e-mail address
$paypal_email = "selleraccount@testdomain.com";
# MySQL Database Information set by Default to insert into paypal_ipn table.
# This can be changed after the IPN is VERIFIED.
$MyUser = "root";
$MyPass = "";
$MyHost = "localhost";
$MyDatabase = "paypal_database";
# Now we Read the Posted IPN
$postvars = array();
foreach ($_POST as $ipnvars => $ipnvalue) $postvars[] = $ipnvars;
# Now we ADD "cmd=_notify-validate" for Post back Validation
$postipn = 'cmd=_notify-validate';
$orgipn = '<b>Posted IPN variables in order received:</b><br><br>';
# Prepare for validation
for ($x=0; $x < count($postvars); $x++) { $y=$x+1; $postkey = $postvars[$x]; $postval = $$postvars[$x]; $postipn.= "&" . $postkey . "=" . urlencode($postval); $orgipn.= "<b>#" . $y . "</b> Key: " . $postkey . " <b>=</b> " . $postval . "<br>"; }
## Verify Mode 1: This will post the IPN variables to the Paypal Network for Validation
if ($verifymode == 1) {
$port = fsockopen ("paypal.com", 80, $errno, $errstr, 30);
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n".
"Host: www.paypal.com\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: " . strlen($postipn) . "\r\n\r\n";
} elseif ($verifymode == 2) {
$port = fsockopen ("www.sandbox.paypal.com", 80, $errno, $errstr, 30);
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n".
"Host: www.sandbox.paypal.com\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: " . strlen($postipn) . "\r\n\r\n";
} else {
$error=1; echo "CheckMode: " . $verifymode . " is invalid!";
exit;
}
# Error at this point: If at this point you need to check your Firewall or your Port restrictions?
if (!$port && !$error) {
echo "Problem: Error Number: " . $errno . " Error String: " . $errstr;
mail("$send_mail_to", "$sysname", "\nYour Paypal System failed due to $errno and string $errstr \n");
exit;
# If No Errors to this point then we proceed with the processing.
# Open port to paypal or test site and post Varibles.
} else {
fputs ($port, $header . $postipn);
while (!feof($port)) {
$reply = fgets ($port, 1024);
$reply = trim ($reply);
}
# Prepare a Debug Report
$ipnreport = $orgipn . "<br><b>" . "IPN Reply: " . $reply . "</b>";
# Buyer Information
// taken these out to fit
#There are certain variables that we will not store for cart since they are dynamic
#such as mc_gross_x as they will be forever changing/increasing your script must check these
# IPN was Confirmed as both Genuine and VERIFIED
if (!strcmp ($reply, "VERIFIED"))
{
/* Now that IPN was VERIFIED below are a few things which you may want to do at this point.
1. Check that the "payment_status" variable is: "Completed"
2. If it is Pending you may want to wait or inform your customer?
3. You should Check your datebase to ensure this "txn_id" or "subscr_id" is not a duplicate. txn_id is not sent with subscriptions!
4. Check "payment_gross" or "mc_gross" matches match your prices!
5. You definately want to check the "receiver_email" or "business" is yours.
6. We have included an insert to database for table paypal_ipn
*/
# Remove Echo below when live
echo $ipnreport;
echo "VALID";
mail("$send_mail_to", "$sysname", "\n Verified IPN Transaction\n \n$ipnreport\n");
}
# IPN was Not Validated as Genuine and is INVALID
elseif (!strcmp ($reply, "INVALID"))
{
/* Now that IPN was INVALID below are a few things which you may want to do at this point.
1. Check your code for any post back Validation problems!
2. Investigate the Fact that this Could be an attack on your script IPN!
3. If updating your DB, Ensure this "txn_id" is Not a Duplicate!
*/
# Remove Echo line below when live
echo $ipnreport;
echo "INVALID";
mail("$send_mail_to", "$sysname", "\n IN Valid IPN Transaction\n \n$ipnreport\n");
}
else
{
# If your script reaches this point there is a problem. Communication from your script to test/paypal pages could be 1 reason.
echo $ipnreport;
echo "ERROR";
mail("$send_mail_to", "$sysname", "\n FATAL ERROR No Reply at all\n \n$ipnreport\n");
}
# Terminate the Socket connection and Exit
fclose ($port); exit;
}
?>
Could you help me out? or has anyone got a IPN script they could share with me. I don't understand classes so please dont waste your time trying to explain them to me.
Thanks
|