/**
  * Access API Correios.
  *
  * @param  string $tracking_code.
  *
  * @return SimpleXMLElement|stdClass History Tracking code.
  */
 protected function get_tracking_history($tracking_code)
 {
     $user_data = $this->get_user_data();
     $args = apply_filters('woocommerce_correios_tracking_args', array('Usuario' => $user_data['login'], 'Senha' => $user_data['password'], 'Tipo' => 'L', 'Resultado' => 'T', 'Objetos' => $tracking_code));
     $api_url = $this->get_tracking_history_api_url();
     $request_url = add_query_arg($args, $api_url);
     $params = array('timeout' => 30);
     $this->logger('Requesting tracking history in: ' . print_r($request_url, true));
     $response = wp_safe_remote_get($request_url, $params);
     if (!is_wp_error($response) && $response['response']['code'] >= 200 && $response['response']['code'] < 300) {
         try {
             $tracking_history = WC_Correios_Connect::safe_load_xml($response['body'], LIBXML_NOCDATA);
         } catch (Exception $e) {
             $this->logger('Tracking history invalid XML: ' . $e->getMessage());
         }
     } else {
         $tracking_history = new stdClass();
         $tracking_history->error = true;
     }
     $this->logger('Tracking history response: ' . print_r($tracking_history, true));
     return $tracking_history;
 }
 /**
  * Calculates the shipping rate.
  *
  * @param array $package Order package.
  *
  * @return void
  */
 public function calculate_shipping($package = array())
 {
     global $woocommerce;
     $rates = array();
     $errors = array();
     $shipping_values = $this->correios_calculate($package);
     if (!empty($shipping_values)) {
         foreach ($shipping_values as $code => $shipping) {
             if (!isset($shipping->Erro)) {
                 continue;
             }
             $name = WC_Correios_Connect::get_service_name($code);
             $error_number = (string) $shipping->Erro;
             $error_message = WC_Correios_Error::get_message($shipping->Erro);
             $errors[$error_number] = array('error' => $error_message, 'number' => $error_number);
             // Set the shipping rates.
             if (in_array($error_number, array('0', '010'))) {
                 $label = 'yes' == $this->display_date ? WC_Correios_Connect::estimating_delivery($name, $shipping->PrazoEntrega, $this->additional_time) : $name;
                 $cost = $this->fix_format(esc_attr($shipping->Valor));
                 $fee = $this->get_fee($this->fix_format($this->fee), $cost);
                 array_push($rates, array('id' => $name, 'label' => $label, 'cost' => $cost + $fee));
             }
         }
         // Display correios errors.
         if (!empty($errors)) {
             foreach ($errors as $error) {
                 if ('' != $error['error']) {
                     $type = '010' == $error['number'] ? 'notice' : 'error';
                     $message = '<strong>' . __('Correios', 'woocommerce-correios') . ':</strong> ' . esc_attr($error['error']);
                     if (function_exists('wc_add_notice')) {
                         wc_add_notice($message, $type);
                     } else {
                         if ('error' == $type) {
                             $woocommerce->add_error($message);
                         } else {
                             $woocommerce->add_message($message);
                         }
                     }
                 }
             }
         }
         $rates = apply_filters('woocommerce_correios_shipping_methods', $rates, $package);
         // Add rates.
         foreach ($rates as $rate) {
             $this->add_rate($rate);
         }
     }
 }
 /**
  * Simulator ajax response.
  *
  * @return string
  */
 public static function ajax_simulator()
 {
     // Validate the data.
     if (!isset($_GET['product_id']) || empty($_GET['product_id'])) {
         wp_send_json(array('error' => __('Error to identify the product.', 'woocommerce-correios'), 'rates' => ''));
     }
     if (!isset($_GET['zipcode']) || empty($_GET['zipcode'])) {
         wp_send_json(array('error' => __('Please enter with your zipcode.', 'woocommerce-correios'), 'rates' => ''));
     }
     // Get the product data.
     $id = isset($_GET['variation_id']) && !empty($_GET['variation_id']) ? $_GET['variation_id'] : $_GET['product_id'];
     $product_id = absint($id);
     $product = get_product($product_id);
     $quantity = isset($_GET['quantity']) && !empty($_GET['quantity']) ? $_GET['quantity'] : 1;
     // Test with the product exist.
     if (!$product) {
         wp_send_json(array('error' => __('Invalid product!', 'woocommerce-correios'), 'rates' => ''));
     }
     // Set the shipping params.
     $product_price = $product->get_price();
     $zip_destination = $_GET['zipcode'];
     $options = get_option('woocommerce_correios_settings');
     $minimum_height = isset($options['minimum_height']) ? $options['minimum_height'] : '';
     $minimum_width = isset($options['minimum_width']) ? $options['minimum_width'] : '';
     $minimum_length = isset($options['minimum_length']) ? $options['minimum_length'] : '';
     $zip_origin = isset($options['zip_origin']) ? $options['zip_origin'] : '';
     $display_date = isset($options['display_date']) ? $options['display_date'] : '';
     $additional_time = isset($options['additional_time']) ? $options['additional_time'] : '';
     $declare_value = isset($options['declare_value']) ? $options['declare_value'] : '';
     $corporate_service = isset($options['corporate_service']) ? $options['corporate_service'] : '';
     $login = isset($options['login']) ? $options['login'] : '';
     $password = isset($options['password']) ? $options['password'] : '';
     $fee = isset($options['fee']) ? $options['fee'] : '';
     $debug = isset($options['debug']) ? $options['debug'] : '';
     $package = array('contents' => array(array('data' => $product, 'quantity' => $quantity)));
     // Get the shipping.
     $services = array_values(self::get_correios_services($options));
     $connect = new WC_Correios_Connect();
     $connect->set_services($services);
     $_package = $connect->set_package($package);
     $_package->set_minimum_height($minimum_height);
     $_package->set_minimum_width($minimum_width);
     $_package->set_minimum_width($minimum_length);
     $connect->set_zip_origin($zip_origin);
     $connect->set_zip_destination($zip_destination);
     $connect->set_debug($debug);
     if ('declare' == $declare_value) {
         $connect->set_declared_value($product_price);
     }
     if ('corporate' == $corporate_service) {
         $connect->set_login($login);
         $connect->set_password($password);
     }
     $shipping = $connect->get_shipping();
     // Send shipping rates.
     if (!empty($shipping)) {
         $_shipping = self::get_the_shipping($shipping, $options, $package);
         wp_send_json(array('error' => '', 'rates' => $_shipping));
     }
     // Error.
     wp_send_json(array('error' => __('It was not possible to simulate the shipping, please try adding the product to cart and proceed to try to get the value.', 'woocommerce-correios'), 'rates' => ''));
 }