PHP and PayPal IPN (x-posted)
Date: 06/11/05
(PHP Development) Keywords: php, web
You'll have to excuse my relative noobishness. I'm not great at PHP, but unfortunately I have a job to do.
Has anyone used PHP do do something with PayPal's Instant Payment Notification? I have to run a script that sends different messages to the buyer based on what product they buy. I have a script (mostly copied from PayPalDev, but I'd say I pretty much know what it's doing) that gets the variables from $_POST with fsockopen(). The script already redefines these array variables (i.e., what you would do if register_globals were off), and they seem like they're all there since it sends me mail when $payer_email is used with mail(). The problem I have is that $item_number prints blank every time.
//set variables needed for email.
$from = "wegottatalkbaby@optonline.net";
$subject = "Responsible Choices Publishing Co. - File Location and Password";
$msg_02 = "second message";
$msg_03 = "third message";
$msg_04 = "fourth message";
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system 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 ('www.paypal.com', 80, $errno, $errstr, 30);
// assign posted variables to local variables
$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'];
$payment_date = $_POST['payment_date'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$payment_type = $_POST['payment_type'];
$payment_status = $_POST['payment_status'];
$payment_gross = $_POST['payment_gross'];
$payment_fee = $_POST['payment_fee'];
$settle_amount = $_POST['settle_amount'];
$memo = $_POST['memo'];
$payer_email = $_POST['payer_email'];
$receiver_email = $_POST['receiver_email'];
$txn_id = $_POST['txn_id'];
$txn_type = $_POST['txn_type'];
$payer_status = $_POST['payer_status'];
$address_street = $_POST['address_street'];
$address_city = $_POST['address_city'];
$address_state = $_POST['address_state'];
$address_zip = $_POST['address_zip'];
$address_country = $_POST['address_country'];
$address_status = $_POST['address_status'];
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$tax = $_POST['tax'];
$option_name1 = $_POST['option_name1'];
$option_selection1 = $_POST['option_selection1'];
$option_name2 = $_POST['option_name2'];
$option_selection2 = $_POST['option_selection2'];
$for_auction = $_POST['for_auction'];
$invoice = $_POST['invoice'];
$subscr_id = $_POST['subscr_id'];
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// 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 email
if ($item_number == 'RCP1101')
{mail($payer_email, $subject, "You ordered the book", "From: $from");}
else if ($item_number == 'RCP1102')
{mail($payer_email, $subject, $msg_02, "From: $from");}
else if ($item_number == 'RCP1103')
{mail($payer_email, $subject, $msg_03, "From: $from");}
else if ($item_number == 'RCP1104')
{mail($payer_email, $subject, $msg_04, "From: $from");}
else
{mail("remix.sakura@gmail.com", "Not sent", "You ordered item $item_number", "From: $from");}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
I've tried a number of things, and now I'm up against a deadline. So I am humbly grateful for assistance from better programmers.
Source: http://community.livejournal.com/php_dev/58444.html