Example #1
0
 function trav_ajax_acc_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('accommodation_id', 'room_type_id', 'rooms', 'adults', 'kids', 'child_ages', 'total_price', 'room_price', 'tax', 'currency_code', 'exchange_rate', 'deposit_price', 'date_from', 'date_to', 'created', 'booking_no', 'pin_code', 'status');
     $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['room_type_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['child_ages'] = serialize($data['child_ages']);
     $data['date_from'] = date('Y-m-d', trav_strtotime($data['date_from']));
     $data['date_to'] = date('Y-m-d', trav_strtotime($data['date_to']));
     if (is_user_logged_in()) {
         $data['user_id'] = get_current_user_id();
     }
     $latest_booking_id = $wpdb->get_var('SELECT id FROM ' . TRAV_ACCOMMODATION_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' => '', 'accommodation_id' => '', 'room_type_id' => '', 'rooms' => '', 'adults' => '', 'kids' => '', 'child_ages' => '', 'total_price' => '', 'room_price' => '', 'tax' => '', 'currency_code' => 'usd', 'exchange_rate' => 1, 'deposit_price' => 0, 'deposit_paid' => $is_payment_enabled ? 0 : 1, 'date_from' => '', 'date_to' => '', '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_acc_get_room_price_data($data['accommodation_id'], $data['room_type_id'], $booking_data['date_from'], $booking_data['date_to'], $data['rooms'], $data['adults'], $data['kids'], $data['child_ages']);
     if (!$room_price_data || !is_array($room_price_data)) {
         $result_json['success'] = -1;
         $result_json['result'] = __('Sorry, The room you are booking now is just taken by another customer. Please have another look.', 'trav');
         wp_send_json($result_json);
     }
     do_action('trav_acc_add_booking_before', $data);
     // save default language accommodation and room type
     $data['accommodation_id'] = trav_acc_org_id($data['accommodation_id']);
     $data['room_type_id'] = trav_room_org_id($data['room_type_id']);
     // add to db
     if ($wpdb->insert(TRAV_ACCOMMODATION_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_acc_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_acc_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);
 }
Example #2
0
 function trav_acc_booking_before()
 {
     global $trav_options, $def_currency;
     // prevent direct access
     if (!isset($_REQUEST['booking_data'])) {
         do_action('trav_acc_booking_wrong_data');
         exit;
     }
     // init booking data : array( 'accommodation_id', 'room_type_id', 'date_from', 'date_to', 'rooms', 'adults', 'kids', 'child_ages' );
     $raw_booking_data = '';
     parse_str($_REQUEST['booking_data'], $raw_booking_data);
     //verify nonce
     if (!isset($raw_booking_data['_wpnonce']) || !wp_verify_nonce($raw_booking_data['_wpnonce'], 'post-' . $raw_booking_data['accommodation_id'])) {
         do_action('trav_acc_booking_wrong_data');
         exit;
     }
     // init booking_data fields
     $booking_fields = array('accommodation_id', 'room_type_id', 'date_from', 'date_to', 'rooms', 'adults', 'kids', 'child_ages');
     $booking_data = array();
     foreach ($booking_fields as $field) {
         if (!isset($raw_booking_data[$field])) {
             do_action('trav_acc_booking_wrong_data');
             exit;
         } else {
             $booking_data[$field] = $raw_booking_data[$field];
         }
     }
     // date validation
     if (trav_strtotime($booking_data['date_from']) >= trav_strtotime($booking_data['date_to'])) {
         do_action('trav_acc_booking_wrong_data');
         exit;
     }
     // make an array for redirect url generation
     $query_args = array('date_from' => $booking_data['date_from'], 'date_to' => $booking_data['date_to'], 'rooms' => $booking_data['rooms'], 'adults' => $booking_data['adults'], 'kids' => $booking_data['kids'], 'child_ages' => $booking_data['child_ages']);
     // get price data
     $room_price_data = trav_acc_get_room_price_data($booking_data['accommodation_id'], $booking_data['room_type_id'], $booking_data['date_from'], $booking_data['date_to'], $booking_data['rooms'], $booking_data['adults'], $booking_data['kids'], $booking_data['child_ages']);
     $acc_url = get_permalink($booking_data['accommodation_id']);
     $edit_url = add_query_arg($query_args, $acc_url);
     // redirect if $room_price_data is not valid
     if (!$room_price_data || !is_array($room_price_data)) {
         $query_args['error'] = 1;
         wp_redirect($edit_url);
     }
     // calculate tax and total price
     $tax_rate = get_post_meta($booking_data['accommodation_id'], 'trav_accommodation_tax_rate', true);
     $tax = 0;
     if (!empty($tax_rate)) {
         $tax = $tax_rate * $room_price_data['total_price'] / 100;
     }
     $total_price_incl_tax = $room_price_data['total_price'] + $tax;
     $booking_data['room_price'] = $room_price_data['total_price'];
     $booking_data['tax'] = $tax;
     $booking_data['total_price'] = $booking_data['room_price'] + $booking_data['tax'];
     // calculate deposit payment
     $deposit_rate = get_post_meta($booking_data['accommodation_id'], 'trav_accommodation_security_deposit', true);
     // 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 {
         if (!isset($_SESSION['exchange_rate'])) {
             trav_init_currency();
         }
         $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'];
     }
     // initialize session values
     $transaction_id = mt_rand(100000, 999999);
     $_SESSION['booking_data'][$transaction_id] = $booking_data;
     //'accommodation_id', 'room_type_id', 'date_from', 'date_to', 'rooms', 'adults', 'kids', 'child_ages', room_price, tax, total_price, currency_code, exchange_rate, deposit_price
     $review = get_post_meta(trav_acc_org_id($booking_data['accommodation_id']), 'review', true);
     $review = !empty($review) ? round($review, 1) : 0;
     // thank you page url
     $acc_book_conf_url = '';
     if (!empty($trav_options['acc_booking_confirmation_page'])) {
         $acc_book_conf_url = trav_get_permalink_clang($trav_options['acc_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['review'] = $review;
     $trav_booking_page_data['acc_url'] = $acc_url;
     $trav_booking_page_data['edit_url'] = $edit_url;
     $trav_booking_page_data['booking_data'] = $booking_data;
     $trav_booking_page_data['room_price_data'] = $room_price_data;
     $trav_booking_page_data['is_payment_enabled'] = $is_payment_enabled;
     $trav_booking_page_data['acc_book_conf_url'] = $acc_book_conf_url;
     $trav_booking_page_data['tax'] = $tax;
     $trav_booking_page_data['tax_rate'] = $tax_rate;
 }
Example #3
0
 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;
 }
Example #4
0
 function trav_booking_button_text($button_text = '')
 {
     global $is_payment_enabled;
     if ($is_payment_enabled) {
         if (trav_is_woo_enabled()) {
             $button_text = __('SUBMIT BOOKING', 'trav');
         } elseif (trav_is_paypal_enabled()) {
             $button_text = __('CONFIRM AND DEPOSIT VIA PAYPAL', 'trav');
         }
     } else {
         $button_text = __('CONFIRM BOOKING', 'trav');
     }
     return $button_text;
 }