// Read the post from PayPal system and add "cmd"
$req = "cmd=_notify-validate";
foreach($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// 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("ssl://www.paypal.com", 443, $errno, $errstr, 30);
// $fp = fsockopen("ssl://sandbox.paypal.com", 443, $errno, $errstr, 30); // Testing only
if (!$fp) {
// HTTP ERROR
echo "Connection Error<br/>Error Number: $errno<br/>Error: $errstr";
}
else {
// Connection with Paypal made
fputs($fp, $header . $req);
while(!feof($fp)) {
$res = fgets($fp, 1024);
if(strcmp($res, "VERIFIED") == 0) {
// Creates storage for buyer
$buyer = new Buyer();
// Ensure transaction of at least minimum amount
if($_POST["mc_gross"] < 1.50) {
echo "Minimum amount not exceeded";
}
else {
// Compiles data to be stored
foreach(array_keys($_POST) as $type) {
$buyer->addDetail($type, $_POST[$type]);
}
// Sends the data to be stored
processSale($buyer);
}
}
else if(strcmp($res, "INVALID") == 0) {
// Invalid transaction
echo "Invalid Transaction";
}
}
}
fclose($fp);
Bookmarks