/**
  * AJAX - Traite la commande / Process checkout
  */
 function wps_pos_process_checkout()
 {
     $status = false;
     $output = $message = '';
     $order_id = !empty($_POST['order_id']) ? wpshop_tools::varSanitizer($_POST['order_id']) : null;
     $new_order = empty($_POST['order_id']);
     $payment_method = !empty($_POST['wpspos-payment-method']) ? wpshop_tools::varSanitizer($_POST['wpspos-payment-method']) : null;
     $customer_id = !empty($_POST['customer_id']) ? wpshop_tools::varSanitizer($_POST['customer_id']) : !empty($_SESSION['cart']['customer_id']) ? wpshop_tools::varSanitizer($_SESSION['cart']['customer_id']) : null;
     $payment_amount = !empty($_POST['wps-pos-total-order-amount']) ? wpshop_tools::varSanitizer($_POST['wps-pos-total-order-amount']) : null;
     $received_payment_amount = !empty($_POST['wpspos-order-received-amount']) ? wpshop_tools::varSanitizer($_POST['wpspos-order-received-amount']) : $payment_amount;
     if (!empty($customer_id)) {
         if (empty($order_id) && !empty($payment_method)) {
             $_SESSION['shipping_method'] = 'default_shipping_mode_for_pos';
             $order_id = wpshop_checkout::process_checkout($payment_method, '', $customer_id, $_SESSION['billing_address'], $_SESSION['shipping_address']);
             wp_update_post(array('ID' => $order_id, 'post_parent' => get_current_user_id()));
         }
         if (!empty($order_id)) {
             $status = true;
             if (!empty($received_payment_amount)) {
                 $params_array = array('method' => $payment_method, 'waited_amount' => $payment_amount, 'status' => 'payment_received', 'author' => $customer_id, 'payment_reference' => '', 'date' => current_time('mysql', 0), 'received_amount' => 'money' == $payment_method && number_format((double) $received_payment_amount, 2, '.', '') > number_format((double) $payment_amount, 2, '.', '') ? $payment_amount : $received_payment_amount);
                 wpshop_payment::check_order_payment_total_amount($order_id, $params_array, 'completed');
             }
             /**	Get order content	*/
             $order_postmeta = get_post_meta($order_id, '_order_postmeta', true);
             ob_start();
             require_once wpshop_tools::get_template_part(WPSPOS_DIR, WPSPOS_TEMPLATES_MAIN_DIR, 'backend/order', 'order', 'complete');
             $output = ob_get_contents();
             ob_end_clean();
             /**	Empty the cart	*/
             /*if ( !empty( $order_postmeta ) && !empty( $order_postmeta['order_status'] ) && ( 'completed' ==  $order_postmeta['order_status'] ) ) {
             			$wps_cart = new wps_cart();
             			$wps_cart->empty_cart();
             		}*/
             $message = __('Order have been saved', 'wps-pos-i18n');
         } else {
             $message = __('No order have been found', 'wps-pos-i18n');
         }
         /*}
         		else {
         			$message = __( 'Please choose a payment method for order', 'wps-pos-i18n' );
         		}*/
     } else {
         $message = __('No customer has been selected for current order', 'wps-pos-i18n');
     }
     wp_die(json_encode(array('status' => $status, 'output' => $output, 'message' => $message)));
 }
 /**
  * AJAX - Valid Checkout step four
  */
 function wps_checkout_valid_step_five()
 {
     $status = false;
     $response = '';
     $payment_method = !empty($_POST['wps-payment-method']) ? wpshop_tools::varSanitizer($_POST['wps-payment-method']) : null;
     $order_id = !empty($_SESSION['cart']['order_id']) ? wpshop_tools::varSanitizer($_SESSION['cart']['order_id']) : 0;
     $customer_comment = !empty($_POST['wps-customer-comment']) ? wpshop_tools::varSanitizer($_POST['wps-customer-comment']) : null;
     $terms_of_sale_checking = isset($_POST['terms_of_sale_indicator']) && !empty($_POST['terms_of_sale']) || !empty($_POST['terms_of_sale']) || !isset($_POST['terms_of_sale_indicator']) && empty($_POST['terms_of_sale']) ? true : false;
     if ($terms_of_sale_checking) {
         if (!empty($payment_method)) {
             /** Check if the payment method exist for the shop **/
             $payment_option = get_option('wps_payment_mode');
             if (!empty($payment_option) && !empty($payment_option['mode']) && array_key_exists($payment_method, $payment_option['mode']) && !empty($payment_option['mode'][$payment_method]['active'])) {
                 $order_id = wpshop_checkout::process_checkout($payment_method, $order_id, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address']);
                 if (!empty($order_id) && !empty($customer_comment)) {
                     wp_update_post(array('ID' => $order_id, 'post_excerpt' => $customer_comment));
                 }
                 $permalink_option = get_option('permalink_structure');
                 $checkout_page_id = wpshop_tools::get_page_id(get_option('wpshop_checkout_page_id'));
                 $response = get_permalink($checkout_page_id) . (!empty($permalink_option) ? '?' : '&') . 'order_step=6';
                 $_SESSION['payment_method'] = $payment_method;
                 $status = true;
                 //Add an action to extra actions on order save
                 $args = array('order_id' => $order_id, 'posted_data' => $_REQUEST);
                 wpshop_tools::create_custom_hook('wps_order_extra_save_action', $args);
             } else {
                 $response = '<div class="wps-alert-error">' . __('This payment method is unavailable', 'wpshop') . '</div>';
             }
         } else {
             $response = '<div class="wps-alert-error">' . __('You must choose a payment method', 'wpshop') . '</div>';
         }
     } else {
         $response = '<div class="wps-alert-error">' . __('You must accept the terms of sale to order', 'wpshop') . '</div>';
     }
     echo json_encode(array('status' => $status, 'response' => $response));
     die;
 }