The transaction data have to come form PayPal, because you don't know whether or not an order resulted in a transaction until you know that the customer has paid for the order.
PayPal's PDT is something you could use for your Thank You page. The code below displays the transaction data on a page, and sends you an e-mail. PDT, however, does not always work. It depends on certain ccount settings, and with certain settings the mechanism is triggered by a customer clicking the 'Return to My Site' button, which doesn't always happen.
PayPal's IPN is normally used to update the inventory and transactions database. It is often too slow to be used in real time.
PayPal's PDT is something you could use for your Thank You page. The code below displays the transaction data on a page, and sends you an e-mail. PDT, however, does not always work. It depends on certain ccount settings, and with certain settings the mechanism is triggered by a customer clicking the 'Return to My Site' button, which doesn't always happen.
PayPal's IPN is normally used to update the inventory and transactions database. It is often too slow to be used in real time.
PHP Code:
<?php
//PDT
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "token goes here";
$req .= "&tx=$tx_token&at=$auth_token";
// 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);
// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
// read the body data
$res = '';
$headerdone = false;
while (!feof($fp)) {
$line = fgets ($fp, 1024);
if (strcmp($line, "\r\n") == 0) {
// read the header
$headerdone = true;
}
else if ($headerdone)
{
// header has been read. now read the contents
$res .= $line;
}
}
// parse the data
$lines = explode("\n", $res);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
// 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
$firstname = $keyarray['first_name'];
$lastname = $keyarray['last_name'];
$payer_email = $keyarray['payer_email'];
$amount = $keyarray['mc_gross'];
$tax = $keyarray['tax'];
$discount = $keyarray['discount'];
$payment_date = $keyarray['payment_date'];
$payment_status = $keyarray['payment_status'];
$payment_type = $keyarray['payment_type'];
$pending_reason = $keyarray['pending_reason'];
$mc_currency = $keyarray['mc_currency'];
$transactionid = $keyarray['txn_id'];
echo ("<p><strong>Payment Details</strong></p>\n");
echo ("<ul>\n");
echo ("<li><em>Name</em>: $firstname $lastname ($payer_email)</li>\n");
echo ("<li><em>Amount</em>: $amount</li>\n");
echo ("<li><em>Tax</em>: $tax</li>\n");
if ($discount <> '0' && $discount <> "") {
echo ("<li><em>Discount</em>: $discount</li>\n");
}
echo ("<li><em>Date</em>: $payment_date</li>\n");
echo ("<li><em>Payment status</em>: $payment_status</li>\n");
echo ("<li><em>Transaction ID</em>: $transactionid</li>\n");
echo ("</ul>\n");
// send e-mail
$today = date("F j, Y, g:i a");
mail("me@myemail.com", "PayPal Payment Data Transfer", "PDT successfully executed on $today \n Payment Details \n Name: $firstname $lastname \n Amount: $amount \n payer_email: $payer_email \n tax: $tax \n discount: $discount \n payment_date: $payment_date \n payment_status: $payment_status \n payment_type: $payment_type \n pending_reason: $pending_reason \n mc_currency: $mc_currency \n Transaction ID: $transactionid \n", "From: My Company Payment Data Transfer <info@mycompany.com>");
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// log for manual investigation
}
}
fclose ($fp);
?>