I have been trying to sort it out myself so what I did so far: As I want the redirect to PayPal happen once the user clicks on Book button, I copied hidden fields which are part of Pay now form to the part with Book button --> part of the last bit of code:
($deposit > 0) {
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php echo $SETTINGS_DB["paypal_address"]; ?>">
<input type="hidden" name="item_name" value="Booking confirmation - <?php echo $BookingID; ?>">
<input type="hidden" name="item_number" value="">
<input type="hidden" name="amount" value="<?php echo number_format($deposit, 2, '.', ''); ?>">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="<?php echo $Currencies[$SETTINGS_DB["currency"]]; ?>">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="hidden" name="return" value="<?php echo $SETTINGS_DB["thankyou_page"]; ?>">
<?php }
?>
<div style="float:left"><input type="submit" value="Book"></div>
<div style="clear:both"></div>
</form>
Once the user clicks on Book button, the code will check if it's been submitted with this:
elseif ($_REQUEST["ac"]=='save_booking') {
and then the Booking details (name, tel, email,..) will be inserted into the database like this:
$sql = "INSERT INTO ".$TABLES["bookings"]." SET
`from` = '".Date2MySQL(SaveToDB($_REQUEST["from"]))."',
`to` = '".Date2MySQL(SaveToDB($_REQUEST["to"]))."',
`name` = '".SaveToDB($_REQUEST["name"])."',
`email` = '".SaveToDB($_REQUEST["email"])."',
`phone` = '".SaveToDB($_REQUEST["phone"])."',
`notes` = '".SaveToDB($_REQUEST["notes"])."',
`people` = '".SaveToDB($_REQUEST["people"])."',
`payment_type` = '".SaveToDB($_REQUEST["payment_type"])."',
`dt` = now()";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql.mysql_error());
$BookingID = mysql_insert_id();
$total_price = 0;
$sql = "SELECT * FROM ".$TABLES["rooms"]." ORDER BY id DESC";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
if ($_REQUEST["room_".$row["id"]]>0) {
$sql = "INSERT INTO ".$TABLES["bookings_rooms"]." SET
`booking_id` = '".$BookingID."',
`room_id` = '".$row["id"]."',
`quantity` = '".SaveToDB($_REQUEST["room_".$row["id"]])."'";
$sql_resultT = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql.mysql_error());
$total_price = $total_price + $_REQUEST["room_price_".$row["id"]]*$_REQUEST["room_".$row["id"]];
};
};
$sql = "UPDATE ".$TABLES["bookings"]." SET
`price` = '".$total_price."'
WHERE id = '".$BookingID."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql.mysql_error());
$deposit = $SETTINGS_DB["payment_deposit"];
$sql = "SELECT * FROM ".$TABLES["bookings"]." WHERE id='".$BookingID."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$booking = mysql_fetch_assoc($sql_result);
$sql = "SELECT * FROM `".$TABLES["bookings_rooms"]."` INNER JOIN ".$TABLES["rooms"]." ON ".$TABLES["rooms"].".id = ".$TABLES["bookings_rooms"].".room_id WHERE `".$TABLES["bookings_rooms"]."`.booking_id='".$BookingID."'";
$sql_resultP = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($rooms = mysql_fetch_assoc($sql_resultP)) {
$roomsemail .= $rooms["quantity"]." x ".$rooms["room_type"].", ";
};
$message = ReadFromDB($SETTINGS_DB["email_message"]);
$search_tokens=array("{name}","{email}","{phone}","{from_date}","{to_date}","{price}","{rooms}");
$replace_tokens=array(ReadFromDB($booking["name"]),ReadFromDB($booking["email"]),ReadFromDB($booking["phone"]),MySQL2Date(ReadFromDB($booking["from"])),MySQL2Date(ReadFromDB($booking["to"])),formatCurrencyExport($booking["price"]),$roomsemail);
$message=str_replace($search_tokens,$replace_tokens,$message);
$mailheader = "From: ".$SETTINGS_DB["admin_email"]."\r\n";
$mailheader .= "Reply-To: ".$SETTINGS_DB["admin_email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=UTF-8\r\n";
mail($booking["email"], $SETTINGS_DB["email_subject"], $message, $mailheader);
if ($SETTINGS_DB["admin_email"]) {
mail($SETTINGS_DB["admin_email"], $SETTINGS_DB["email_subject"], $message, $mailheader);
}
After the data has been inserted into the database I want the user to go automatically to PayPal payment page, so I inserted this code after the above code:
header ("https://www.paypal.com/cgi-bin/webscr");
exit;
?>
Now when I click on Book button nothing happens, well the form disappears, so I just have some instruction text on the page, but we are not redirected to the PayPal page. Could somebody please point me to the right direction?