Payment Data Transfer (PDT) will post a token associated with the transaction, and you will need to send this token back to PayPal in order to obtain the payment details associated with the transaction. It will send this token as a $_GET value in the URL. You will need to copy the Identity Token produced by paypal on the preferences page to use in your script.
So now the customer's browser is returned to your site after a successful payment. The paypal token can be found in $_GET["tx"]. I'm using CURL to POST three values back to paypal. The first is the token it just sent me (tx), the second is my personal identity token (at), and the third is a _notify-synch value telling paypal to synch (cmd). The code looks like this.
if(isset($_GET["tx"])){ // Your id token $at = "[insert token here]"; // Setup the POST url (using sandbox for testing) $URL="https://www.sandbox.paypal.com/cgi-bin/webscr"; // Ready the curl for POST $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $URL); curl_setopt($ch, CURLOPT_POST, 1); // Set the POST values to send tx, the id token, and cmd curl_setopt($ch, CURLOPT_POSTFIELDS, "tx=".$_GET["tx"] . "&at=".$at." &cmd=_notify-synch"); // Begin the output buffer and POST the values to paypal ob_start(); curl_exec ($ch); curl_close ($ch); // Get the contents of the buffer and clean up $PDT = ob_get_contents(); ob_end_clean(); // ...}
Keep in mind that CURL will output the buffer to the page if this section of code is not within the body tags of HTML. So now we hopefully have all of the transaction values in one magnificent string. Next I needed to parse out the values so I wrote a function to do this.
// Parse the paypal string into an array with keys function parse_paypal_string_to_array($pp_string){ // Explode the cuurent string into parts divided by a line return // These parts are variable assignments ex. item_name_1=Item+Name $variable_parts = explode("\n", $pp_string); // Check to see if paypal returned FAILED if($variable_parts[0] == "FAILED") return "FAILED"; foreach ($variable_parts as $index => $val) { // Check that the variable isn't undefined if($val{strlen($val)-1} != '=') { // Get the key and value list($varkey, $varval) = explode("=", $val); // Set to final array $final_array[$varkey] = $varval; } } return $final_array;}
This returns an array with appropriate keys and values. Now I can access returned data like so...
$pp_array = parse_paypal_string_to_array($PDT);
echo($pp_array["payer_email"]);
Paypal says that if the POST fails you should try a second time in case of a network problem. Using paypal's sandbox is a great way to test this stuff out.