Skip to content

Commit

Permalink
Fixed correios call
Browse files Browse the repository at this point in the history
  • Loading branch information
cagartner committed Oct 25, 2019
1 parent 2e1f556 commit 912acc3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
68 changes: 38 additions & 30 deletions src/Carriers/Correios.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 10 additions & 4 deletions src/Helpers/Consult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);

Expand Down Expand Up @@ -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'])) {
Expand All @@ -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),
Expand Down

0 comments on commit 912acc3

Please sign in to comment.