PayPal Payment Data Transfer (PDT) provides a simple way for gathering the PayPal transaction details after the payment. You can even make use of it to trigger any post-transaction action. This article guide you how to test your PDT flow in PayPal Sandbox
First of all you have to enable the PDT in your PayPal account. After you have logged in, go to Profile > More Options and the click Website Payment Preferences.

Turn on the Auto Return and Payment Data Transfer as depicted in the following picture. Fill in the Return URL with the .php which can talk with PayPal with PDT. Please note that you need to create the thank.php in your web folder before it can be validated by PayPal – Remarks by yousouf @ 2011-06-03.

Copy the identity token for authorization in the thank.php
The next thing is to create the thank.php which is the destination of the Return URL.
thank.php
<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';
$tx_token = $_GET['tx'];
$auth_token = "<your identity token>";
$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 .= “Host: http://www.sandbox.paypal.com\r\n”;
//$header .= “Host: http://www.paypal.com\r\n”;
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
// url for paypal sandbox
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// url for payal
// $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
// If possible, securely post back to paypal using HTTPS
// Your PHP server will need to be SSL enabled
// $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'];
$itemname = $keyarray['item_name'];
$amount = $keyarray['payment_gross'];
echo ("<p><h3>Thank you for your purchase!</h3></p>");
echo ("<b>Payment Details</b><br>\n");
echo ("<li>Name: $firstname $lastname</li>\n");
echo ("<li>Item: $itemname</li>\n");
echo ("<li>Amount: $amount</li>\n");
echo ("");
}
else if (strcmp ($lines[0], "FAIL") == 0) {
// log for manual investigation
}
}
fclose ($fp);
?>
Your transaction has been completed, and a receipt for your purchase has been emailed to you.<br>
You may log into your account at <a href='https://www.paypal.com'>www.paypal.com</a> to view details of this transaction.<br>
Replace the identity token in the above code and u can test the PayPal transaction flow with PDT.
Apart from PHP, u can work with PDT using
- ASP/VBScript
- PERL
- Cold Fusion
You can find the sample code in the reference page.
Reference: Payment Data Transfer Code Samples
Done =)
This just helped me a bunch. Thanks!
you are welcome =)
Hi the returning array is empty. please help me.
do you have any HTTP authentication settings in apache or .htaccess?
for paypal to feed back the data to you, you have to disable the HTTP authentication.
Where does the data for the $keydata array come from?
sorry the $keyarray
the $keyarray is return from paypal.com/sandbox.paypal.com which contains the payment details =)
ah sorry, for some reason i thought this script was to accept recurring payments from your site. would you happen to have a script for that?
Hi Jose,
Sorry for the late reply. i haven’t worked on the recurring payment before. so i have no idea about that.
are you using Express Checkout or Direct Payment (Website Payments Pro)? if yes, i guess the following API Documentation could help.
Thanks a ton! Helped me a lot.
hi,
how to get the values ‘tx’ and identity token in paypal sandbox after transaction completed.
is any possible to get url values in paypal sandbox?
plz anybody can help me.
you don’t need to have the value of tx, it is assigned from paypal/paypal sandbox. when user completed the transaction in the paypal/paypal sandbox page, it will redirect to the Return URL (thank.php in the above example) as you set in the paypal/paypal sandbox account.
you can get the identity token from the paypal/paypal sandbox. as i remember, it is shown after you have enabled the PDT feature.
Hi,
I just came across this and have been trying to figure it out but I keep getting this error:
Notice: Undefined variable: header in /home/user/public_html/domain/thank.php on line 12
Which refers to the following line:
$header .= “POST /cgi-bin/webscr HTTP/1.0\r\n”;
Going crazy trying to figure out what it could be. Any insight would be appreciated. Thanks for all the info so far!
oh, i think there is a typo there.
could you try replacing that by
I fixed it last night. The issue was that I was working on a scratch file and had not defined $header anywhere else yet. Your solution fixes it (would not have been a problem for folks who had already defined $header I don’t think) but I just added another line above it which also fixes it.
$header = “”;
Thanks for the reply!
Good to know that u have solved the problem. =)
Just what I needed, thanks!
Implemented it though and no matter what I try nothing shows next to price on the return url page, I want to use it with a multi item drop down, get shipping, name, item name but price is blank. Changed to a single product buy now button and still get a blank price, I’m guessing the payment_gross line is wrong for me, any ideas?
are u able to get the $keyarray from the paypal server?
Hi
Thanks for the tutorial. Works like a charm.
I had some difficulty at first. When I gave the return url I got an error message telling me that the url could not be validated. It was only after I posted the thank.php on my website that I could finish the configuration and got the token. But then, I am a newbie. I hope that my remarks help other newbies like me.
Thanks for ur remark. =)
Hi ! This is by far the best tutorial on how to use PDT ! Thank you very much mate !
Hi Pastro, thanks for your words and really glad to know it could help. =D
Thanks for this tutorial. This works perfectly in sandbox test site. Does this code need any changes when integrated with paypal site.
What you need to change are the $fp and $auth_token when you switch to live.
... $auth_token = "<your identity token>"; ... $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); ...Hi! and thanks for this wonderful tutorial. I tried it and it worked perfectly with basic sample buttons, however I’m trying to get it to work with other information … like when you have a paypal pay now button with a form … like how paypal, when it generates the buttons it gives you certain options to add text fields, etc. How do you get information that is passed from those optional fields to display in thank.php? Otherwise it works just fine!
i guess other form data is stored in the $keyarray too, you can print_r($keyarray) and see if it contains those form data.
Hi! I have a question, hope you could help. Your PDT is displaying only one item name, what if I have to display a multiple Item name in my return url? thanks in advance.
i haven’t tried this before but according to IPN and PDT Variables
it should be item_name x. i.e. $keyarray['item_name1'], $keyarray['item_name2']… etc.
yeah, your right, I figured it out by displaying the $keyarray. Thanks!
Good to know that you have solved the problem. =)
hi can I use it with Java?
There is no sample code of PDT in Java. Maybe you can find more information in the following link on how to implement paypal in Java.
Sample Code – IPN and APIs
after the script runs i only get:
“Your transaction has been completed, and a receipt for your purchase has been emailed to you.You may log into your account at http://www.paypal.com to view details of this transaction.”
without the transaction details..
any ideas?
the script doesnt output any data like firstname, lastname etc.
after inspecting the $res variable it is empty only the headers are there.
any idea?
I guess maybe paypal has made some changes on the implementation. I suggest you could follow the example from the official page.
Code Samples – Payment Data Transfer
Do you still get the same problem?
Hi,
Great tutorial, but I’m not able to get any response.
First off when I run the code the reply is “Invalid Host header” in the result. Adding
fixes that, but now I get no reply at all. Any thoughts would be greatly appreciated.
Not very sure what causes the problem. I have checked there is a new sample code provided by PayPal. Let me know if it could solve the problem.
it should be:
$header .= “Host: http://www.sandbox.paypal.com\r\n”;
or
$header .= “Host: http://www.paypal.com\r\n”;
I have updated the post. Thanks. =D
I’m running PHP 5.2 on my server, and can’t upgrade until I upgrade PHP code for everything on my site. Paypal’s code above is written on PHP 5.3. Would you know the specific changes I would need to make in the code for it to work with PHP 5.2. I get nothing back from Paypal using the above code — no errors, nothing.
The above comment is the latest source code from Paypal
And the code sample in my post was written long time ago and properly i was using PHP5.2.
It is hard to say what causes the problem. But i think you could check the following.
1. Make sure you are working on the correct server. PayPal or Sandbox?
2. Have you enabled PDT on your PayPal account and is your $auth_token correct?
3. Try to print some debug msgs in the code and check which part causes the problem.
Hope this help. =)