Hi,
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="
https://www.paypal.com/cgi-bin/webscr"
method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="
https://www.paypal.com/en_US/i/btn/btn_buynow_SM.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<img alt="" border="0" src="
https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
<input type="hidden" name="custom" id="custom" value="<?echo"$contact_id"
;?>">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHPwYJKoZIhvcN
AQcEoIIHMD
CCBywCAQEx
ggEwMIIBLA
IBADCBlDCB
jjELMAkGA1
UEBh
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:
<?php
// 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_to
ken";
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-url
encoded\r\
n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$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) {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_cur
rency 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);
echo"
Thank you $firstname $lastname. Your purchase of $itemname in the amount of $amount has been completed. Custom: $customreturn $custom
<p>
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>
";
?>
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,
Alan