PayPal Payment Data Transfer (PDT) – Getting PayPal Information Returned to Your Site

Screen Shot 2013-10-27 at 4.23.10 PM My client has digital products that need to be downloaded after a user clicks on a PayPal Buy Now button. If I set Payment Data Transfer up in her PayPal account, PayPal will return back to my site to a page that I specify in her PayPal account. It will return all the information about the customer, their payment and their product.

I had to go to the Client’s PayPal profile->website payment preferences and turn on Auto Return, then set the page where I want it to return with the information. Then I had to turn on Payment Data Transfer on the same page. It then gives me a Identity Token to use in my code.

The page I entered on that page should have my PDT script which looks something like this:

(Note: See this post for an explanation of testing with PayPal)

(Note2: I got most of this script from the PayPal Developer site.)

//
global $testing;
if ($testing) {
    $pp_hostname = "www.sandbox.paypal.com";
    $auth_token = "ifju592873478cpAgqwxeqmgulIHf0tfrtPNhsoTR7h3VgEPciBP-fAyRCXZ4";
} else {
    $pp_hostname = "www.paypal.com";
    $auth_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
}

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$req .= "&tx=$tx_token&at=$auth_token";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
$res = curl_exec($ch);
curl_close($ch);

if(!$res){
    //HTTP ERROR
    echo 'HTTP ERROR';die;
}else{
     // 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
        // Add the customer to the mailing list
        // show downloads

        // print_r($keyarray) // to see other variables passed from PayPal

        $firstname = $keyarray['first_name'];
        $lastname = $keyarray['last_name'];
        $itemname = $keyarray['item_name'];
        $amount = $keyarray['payment_gross'];

        echo ("<h1><span style="color: #171989;">Thank You for Your Purchase</span></h1>");

        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");

    else if (strcmp ($lines[0], "FAIL") == 0) {
        // log for manual investigation

    }
}
//

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top