Link to home
Create AccountLog in
Avatar of centerforward

asked on

I cannot get the "custom" variable back from PayPal's Payment Data Transfer (PDT) return


I need help from someone familiar with PayPal's Payment Data Transfer (PDT) using Website Payments Standard (simple buy now buttons).

In the PayPal account under Profile -> "Website Payment Preferences" I've enabled Auto Return and PDT and used the PHP script generator to generate the return script when it returns from the PayPal gateway.  I am able to output these returning variables successfully:  

echo ("<li>Name: $firstname $lastname</li>\n");
echo ("<li>Item: $itemname</li>\n");
echo ("<li>Amount: $amount</li>\n");

But I'd like to make use of the "custom" variable which is a valid variable to pass and return according to the documentation - but I cannot get a value for "custom" to return.

Here is the beginning of the Buy Now button code:

<form action="" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="" width="1" height="1">
<input type="hidden" name="custom" id="custom" value="<?echo"$contact_id";?>">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHPwYJKoZIhvcNAQcEoIIHMDCCBywCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBh

and looking at the source, this successfully reads
<input type="hidden" name="custom" id="custom" value="36">
for example...

This is my PHP I edited to deal with the return:

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

$tx_token = $_GET['tx'];
$auth_token = "(proper token 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 ('', 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://', 443, $errno, $errstr, 30);

if (!$fp) {
} 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) {echo"success";
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'];
$customreturn = $keyarray['custom'];

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 ("<li>Custom: $customreturn</li>\n");
echo ("");
else if (strcmp ($lines[0], "FAIL") == 0) {
// log for manual investigation

fclose ($fp);


Thank you $firstname $lastname.  Your purchase of $itemname in the amount of $amount has been completed.  Custom: $customreturn $custom
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=''></a> to view details of this transaction.<br>



I added the line to pull custom from $keyarray, is that right?

If anyone has done this before I'd really appreciate some advice.  I've gone over and over the PDT posts about using the custom variable, and I cannot find any clues why $custom just doesn't come back as a normal populated variable with a value etc.

Thanks very much for your help,
Avatar of centerforward


Thank you, actually I progressed beyond the assistance of the first link but am still having problems.

(Detail:  It refers to an issue about where my PHP result script attempts to get the PDT variables from, or etc - When I was testing with sandbox last night, I originally got a FAIL because of not having the proper URL but fixed this and got SUCESS and *some* variables back.  When the "custom" variable consistently returned blank even though it's listed as an included variable, and I'm nearly certain I must be posting it, I suspected sandbox so I went back to the actual paypal environment and created a button for $0.01 and I'm doing a "penny test" now.  And I'm seeing the same thing - my PHP script on the return WORKS - I get a success and I get variables like "mc_gross" and "first_name" etc., BUT NOT "custom").

The second link has more resources - and phpcoin sounds interesting - I'll explore this.

But the goal of my question/post is to explain/answer why I get a blank value for "custom" on my return.  I am hoping that someone who has done this before might know some clues about what could cause this... it seems so simple in nature - I'm sending it from the form, I'm getting all the others back successfully via PDT EXCEPT that one. ?  What can I check/fix to get my custom variable!  I'd like to know this, regardless of whether I "upgrade" the system to phpcoin.  Thanks.

looks liek you need to make sure of the var names and the servers.

waiting for your reply
To reiterate, I am NOT using sandbox anymore, I am testing the live paypal servers by only paying with a penny.

I posted all of my code BECAUSE I believe my variable names, servers, and syntax to be correct.  If you can find a mistake in my code (above) that would be terrific.  But I am not finding a mistake in it, and I did not make the server or varname mistakes in the links that you sent

I'm looking for a specific reason the CUSTOM variable is not being returned.  Best I can tell, it is not because of my syntax.  I don't know what the other possibilities could be.  I've posted my code, so if you can find an error in my code, and point it out to me, then that'd be great and I could move forward.  Otherwise I"m looking for an expert who can identify other reasons or things to check that would explain why paypal RECEIVES the custom variable but returns it blank.

Thanks in advance
:) . maybe this helps.

but try to use a loop instead of the loop.

please let me know if i am not welcomed to contribute in this post any more.

and if it works for you. i would like a nice 50 $ , as they are offered in teh rentacoder :) .

waiting for your reply.

I sincerely appreciate your attempts to help, it's just that each post has referred me to a link that is about a problem basically "next door" to my problem, which don't have bearing on resolving my specific issue.  

As I explore each one, I'm learning more and more, but I really don't understand why this is so complicated.  Paypal says if you send the hidden "custom" variable in a form, I'll receive it in the PDT return post, and I don't.  I've posted my code.  The lack of support I can get about this makes me feel like I'm the first person who's tried this.

I'm using code from the "PHP code generator", and the loop it uses is identical to the one you suggest I try above, and that could be observed by looking at the code I originally posted (above).

Yes, I re-posted and received no replies all week.  I re-posted on and asked for help for $25 with a max bid of $50 and so far all I can are suggestions to use IPN instead of PDT, which granted is an option, but not a solution to my issue, and a comment that "custom" doesn't work with PDT - the documentation says it does.

I am about to give up and try something else, but this seems kind of ridiculous that something this simple isn't working - I feel like someone who's done this before could look at my code and identify the problem.  Yes, if you can do that, I'll accept your solution and send you $25 both!

thanks for your nice reply :)

Yes I have not did that before.
I am not looking for your money neither I can accept it.
I really appreciate your acceptance for my help.

I hope this is the last check ,

can you use 2 test lines to print out the $res , #lines. that will show if the value is sent empty from papal or if it is a coding problem.

in other words , just check the whole message coming to you before breaking it into variables.

waiting for your reply
Ha well okay, thanks, ... and that sounds like a great idea.  Problem is I'm not a very advanced programmer, I only do basic php/mysql queries and simple dynamic coding, I'm not experienced with arrays and I don't know how to write that code to test that.  

Are you able to look at my code above, and tell me exactly how to modify it to add that test?  Unfortunately until I can find some other help this is the only way I know to try your advice on this one, I don't know enough to do it myself. :/

I just took this code from the php code generator paypal supplied.
Avatar of Smart_Man
Flag of Egypt image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account