function trav_tour_booking_before() { global $trav_options, $def_currency; // init booking_data fields $booking_fields = array('tour_id', 'st_id', 'tour_date', 'adults'); $booking_data = array(); foreach ($booking_fields as $field) { if (!isset($_REQUEST[$field])) { do_action('trav_tour_booking_wrong_data'); exit; } else { $booking_data[$field] = $_REQUEST[$field]; } } if (isset($_REQUEST['kids'])) { $booking_data['kids'] = $_REQUEST['kids']; } //verify nonce if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'post-' . $_REQUEST['tour_id'])) { do_action('trav_tour_booking_wrong_data'); exit; } $schedule_data = trav_tour_get_price_data($booking_data); $tour_url = get_permalink($booking_data['tour_id']); // redirect if $schedule_data is not valid if (empty($schedule_data) || empty($schedule_data['success'])) { wp_redirect(add_query_arg(array('error' => 1), $tour_url)); } if (!isset($_SESSION['exchange_rate'])) { trav_init_currency(); } $deposit_rate = get_post_meta($booking_data['tour_id'], 'trav_tour_security_deposit', true); $booking_data['total_price'] = $schedule_data['price']; // if woocommerce enabled change currency_code and exchange rate as default if (!empty($deposit_rate) && trav_is_woo_enabled()) { $booking_data['currency_code'] = $def_currency; $booking_data['exchange_rate'] = 1; } else { $booking_data['currency_code'] = trav_get_user_currency(); $booking_data['exchange_rate'] = $_SESSION['exchange_rate']; } // if payment enabled set deposit price field $is_payment_enabled = !empty($deposit_rate) && trav_is_payment_enabled(); if ($is_payment_enabled) { $booking_data['deposit_price'] = $deposit_rate / 100 * $booking_data['total_price'] * $booking_data['exchange_rate']; } $price_data = $schedule_data['price_data']; // initialize session values $transaction_id = mt_rand(100000, 999999); $_SESSION['booking_data'][$transaction_id] = $booking_data; //'tour_id', 'st_id', 'date_from', 'date_to', 'rooms', 'adults', 'kids', price, currency_code, exchange_rate, deposit_price $multi_book = get_post_meta($booking_data['tour_id'], 'trav_tour_multi_book', true); // thank you page url $tour_book_conf_url = ''; if (!empty($trav_options['tour_booking_confirmation_page'])) { $tour_book_conf_url = trav_get_permalink_clang($trav_options['tour_booking_confirmation_page']); } else { // thank you page is not set } global $trav_booking_page_data; $trav_booking_page_data['transaction_id'] = $transaction_id; $trav_booking_page_data['tour_url'] = $tour_url; $trav_booking_page_data['booking_data'] = $booking_data; $trav_booking_page_data['price_data'] = $price_data; $trav_booking_page_data['multi_book'] = $multi_book; $trav_booking_page_data['is_payment_enabled'] = $is_payment_enabled; $trav_booking_page_data['tour_book_conf_url'] = $tour_book_conf_url; }
function trav_ajax_tour_submit_booking() { global $wpdb, $trav_options; // validation $result_json = array('success' => 0, 'result' => ''); if (!isset($_POST['transaction_id']) || !isset($_SESSION['booking_data'][$_POST['transaction_id']])) { $result_json['success'] = 0; $result_json['result'] = __('Sorry, some error occurred on input data validation.', 'trav'); wp_send_json($result_json); } $raw_booking_data = $_SESSION['booking_data'][$_POST['transaction_id']]; $booking_fields = array('tour_id', 'st_id', 'tour_date', 'adults', 'kids', 'total_price', 'currency_code', 'exchange_rate', 'deposit_price'); $booking_data = array(); foreach ($booking_fields as $booking_field) { if (!empty($raw_booking_data[$booking_field])) { $booking_data[$booking_field] = $raw_booking_data[$booking_field]; } } $is_payment_enabled = trav_is_payment_enabled() && !empty($booking_data['deposit_price']); if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'post-' . $booking_data['tour_id'])) { $result_json['success'] = 0; $result_json['result'] = __('Sorry, your nonce did not verify.', 'trav'); wp_send_json($result_json); } if (isset($trav_options['vld_captcha']) && !empty($trav_options['vld_captcha'])) { if (!isset($_POST['security_code']) || $_POST['security_code'] != $_SESSION['security_code']) { $result_json['success'] = 0; $result_json['result'] = __('Captcha error. Please check your security code again.', 'trav'); wp_send_json($result_json); } } if (isset($trav_options['vld_credit_card']) && !empty($trav_options['vld_credit_card'])) { if (!isset($_POST['cc_type']) || !isset($_POST['cc_holder_name']) || !isset($_POST['cc_number']) || !isset($_POST['cc_exp_month']) || !isset($_POST['cc_exp_year']) || !trav_cc_validation($_POST['cc_type'], $_POST['cc_holder_name'], $_POST['cc_number'], $_POST['cc_exp_month'], $_POST['cc_exp_year'])) { $result_json['success'] = 0; $result_json['result'] = __('Vcc validation An error.', 'trav'); wp_send_json($result_json); } } // init variables $post_fields = array('first_name', 'last_name', 'email', 'country_code', 'phone', 'address', 'city', 'zip', 'country', 'special_requirements'); $customer_info = array(); foreach ($post_fields as $post_field) { if (!empty($_POST[$post_field])) { $customer_info[$post_field] = sanitize_text_field($_POST[$post_field]); } } $data = array_merge($customer_info, $booking_data); $data['tour_date'] = date('Y-m-d', trav_strtotime($data['tour_date'])); if (is_user_logged_in()) { $data['user_id'] = get_current_user_id(); } $latest_booking_id = $wpdb->get_var('SELECT id FROM ' . TRAV_TOUR_BOOKINGS_TABLE . ' ORDER BY id DESC LIMIT 1'); $booking_no = mt_rand(1000, 9999); $booking_no .= $latest_booking_id; $pin_code = mt_rand(1000, 9999); if (!isset($_SESSION['exchange_rate'])) { trav_init_currency(); } $default_booking_data = array('first_name' => '', 'last_name' => '', 'email' => '', 'country_code' => '', 'phone' => '', 'address' => '', 'city' => '', 'zip' => '', 'country' => '', 'special_requirements' => '', 'tour_id' => '', 'st_id' => 0, 'tour_date' => '', 'adults' => '', 'kids' => '', 'total_price' => '', 'currency_code' => 'usd', 'exchange_rate' => 1, 'deposit_price' => 0, 'deposit_paid' => $is_payment_enabled ? 0 : 1, 'created' => date('Y-m-d H:i:s'), 'booking_no' => $booking_no, 'pin_code' => $pin_code, 'status' => 1); $data = array_replace($default_booking_data, $data); // credit card offline charge if (!empty($trav_options['vld_credit_card']) && !empty($trav_options['cc_off_charge'])) { $cc_fields = array('cc_type', 'cc_holder_name', 'cc_number', 'cc_cid', 'cc_exp_year', 'cc_exp_month'); $cc_infos = array(); foreach ($cc_fields as $cc_field) { $cc_infos[$cc_field] = empty($_POST[$cc_field]) ? '' : $_POST[$cc_field]; } $data['other'] = serialize($cc_infos); } // recheck availability $room_price_data = trav_tour_get_price_data(array('tour_id' => $data['tour_id'], 'st_id' => $data['st_id'], 'tour_date' => $booking_data['tour_date'], 'adults' => $data['adults'], 'kids' => $data['kids'])); if (empty($room_price_data) || !is_array($room_price_data)) { $result_json['success'] = -1; $result_json['result'] = __('Sorry, The tour you are booking now is just taken by another customer. Please have another look.', 'trav'); wp_send_json($result_json); } do_action('trav_tour_add_booking_before', $data); // save default language tour and room type $data['tour_id'] = trav_tour_org_id($data['tour_id']); // add to db if ($wpdb->insert(TRAV_TOUR_BOOKINGS_TABLE, $data)) { $booking_id = $wpdb->insert_id; $data['booking_id'] = $booking_id; $_SESSION['booking_data'][$_POST['transaction_id']] = $data; $result_json['success'] = 1; $result_json['result']['booking_no'] = $booking_no; $result_json['result']['pin_code'] = $pin_code; $result_json['result']['transaction_id'] = $_POST['transaction_id']; if ($is_payment_enabled) { if (trav_is_woo_enabled()) { // woocommerce do_action('trav_woo_add_tour_booking', $data); $result_json['result']['payment'] = 'woocommerce'; } elseif (trav_is_paypal_enabled()) { // paypal direct $result_json['result']['payment'] = 'paypal'; } } else { $result_json['result']['payment'] = 'no'; } do_action('trav_tour_add_booking_after', $data); } else { $result_json['success'] = 0; $result_json['result'] = __('Sorry, An error occurred while add booking.', 'trav'); } wp_send_json($result_json); }