diff --git a/src/Carriers/Correios.php b/src/Carriers/Correios.php index 608e5b2..2a63244 100755 --- a/src/Carriers/Correios.php +++ b/src/Carriers/Correios.php @@ -50,41 +50,49 @@ public function calculate() /** @var \Webkul\Checkout\Models\Cart $cart */ $cart = Cart::getCart(); $total_weight = $cart->items->sum('total_weight'); + $rates = []; + $tax_handling = (int)core()->convertPrice($this->getConfigData('tax_handling')) ?: 0; + + $methods = explode(',', $this->getConfigData('methods')); - $data = [ - 'tipo' => $this->getConfigData('methods'), - 'formato' => $this->getConfigData('package_type'), // opções: `caixa`, `rolo`, `envelope` - 'cep_destino' => $cart->shipping_address->postcode, - 'cep_origem' => core()->getConfigData('sales.shipping.origin.zipcode'), - 'peso' => $total_weight, // Peso em kilos - 'comprimento' => $this->getConfigData('package_length'), // Em centímetros - 'altura' => $this->getConfigData('package_height'), // Em centímetros - 'largura' => $this->getConfigData('package_width'), // Em centímetros - 'diametro' => $this->getConfigData('roll_diameter'), // Em centímetros, no caso de rolo - ]; - - if ($this->getConfigData('cod_company') && $this->getConfigData('password')) { - $data['empresa'] = $this->getConfigData('cod_company'); - $data['senha'] = $this->getConfigData('password'); + if (!$methods) { + throw new \Exception('Select one shipping method of correios'); } - $consult = new Consult(); - /** @var Collection $result */ - $result = $consult->carriers($data); - $rates = []; - $tax_handling = (int)core()->convertPrice($this->getConfigData('tax_handling')) ?: 0; + foreach ($methods as $method) { + $data = [ + 'tipo' => $method, + 'formato' => $this->getConfigData('package_type'), // opções: `caixa`, `rolo`, `envelope` + 'cep_destino' => $cart->shipping_address->postcode, + 'cep_origem' => core()->getConfigData('sales.shipping.origin.zipcode'), + 'peso' => $total_weight, // Peso em kilos + 'comprimento' => $this->getConfigData('package_length'), // Em centímetros + 'altura' => $this->getConfigData('package_height'), // Em centímetros + 'largura' => $this->getConfigData('package_width'), // Em centímetros + 'diametro' => $this->getConfigData('roll_diameter'), // Em centímetros, no caso de rolo + ]; - foreach ($result as $item) { - $object = new CartShippingRate; - $object->carrier = 'correios'; - $object->carrier_title = $this->getConfigData('title'); - $object->method = 'cagartner_correios_' . Consult::getTipoIndex($item['codigo']); - $object->method_title = $this->getMethodTitle($item['codigo']); - $object->method_description = $this->getMethodDescription($item['prazo']); - $object->price = core()->convertPrice($item['valor']) + $tax_handling; - $object->base_price = core()->convertPrice($item['valor']) + $tax_handling; - array_push($rates, $object); + if ($this->getConfigData('cod_company') && $this->getConfigData('password')) { + $data['empresa'] = $this->getConfigData('cod_company'); + $data['senha'] = $this->getConfigData('password'); + } + + $consult = new Consult(); + /** @var Collection $result */ + $result = $consult->carriers($data); + + $shippingRate = new CartShippingRate; + $shippingRate->carrier = 'correios'; + $shippingRate->carrier_title = $this->getConfigData('title'); + $shippingRate->method = 'cagartner_correios_' . Consult::getTipoIndex($result['codigo']); + $shippingRate->method_title = $this->getMethodTitle($result['codigo']); + $shippingRate->method_description = $this->getMethodDescription($result['prazo']); + $shippingRate->price = core()->convertPrice($result['valor']) + $tax_handling; + $shippingRate->base_price = core()->convertPrice($result['valor']) + $tax_handling; + + array_push($rates, $shippingRate); } + return $rates; } diff --git a/src/Helpers/Consult.php b/src/Helpers/Consult.php index 9a028ef..86982dd 100644 --- a/src/Helpers/Consult.php +++ b/src/Helpers/Consult.php @@ -15,13 +15,13 @@ class Consult const RASTREIO_URL = 'https://www2.correios.com.br/sistemas/rastreamento/resultado_semcontent.cfm'; private static $methods = array( - 'sedex' => '04014', + 'sedex' => '4014', 'sedex_a_cobrar' => '40045', 'sedex_10' => '40215', 'sedex_hoje' => '40290', - 'pac' => '04510', - 'pac_contrato' => '04669', - 'sedex_contrato' => '04162', + 'pac' => '4510', + 'pac_contrato' => '4669', + 'sedex_contrato' => '4162', 'esedex' => '81019', ); @@ -111,6 +111,7 @@ public function carriers($data, $options = array()) if ($result = $curl->simple($endpoint, $params)) { $result = simplexml_load_string($result); $rates = array(); + $collect = (array) $result->Servicos; if (is_object($collect['cServico'])) { @@ -120,6 +121,11 @@ public function carriers($data, $options = array()) } foreach ($rates as $rate) { + + if ((int) $rate->Erro) { + throw new \Exception($rate->MsgErro); + } + $return[] = array( 'codigo' => (int) $rate->Codigo, 'valor' => self::cleanMoney($rate->Valor),