Skip to content

Instantly share code, notes, and snippets.

@omarkdev
Last active December 14, 2016 21:13
Show Gist options
  • Save omarkdev/75eb41bac00ac89f4996d6ae86e5dd18 to your computer and use it in GitHub Desktop.
Save omarkdev/75eb41bac00ac89f4996d6ae86e5dd18 to your computer and use it in GitHub Desktop.
Calcular frete dos correios com Laravel
<?php
$freightPostOffice = new FreightPostOfficeService();
$dataFreight = [
'source-zip-code' => '18780-000',
'destiny-zip-code' => '17025-060',
'weight-product' => 1,
'length-product' => 16,
'height-product' => 5,
'width-product' => 15,
'diameter-product' => 0,
'value-product' => '200.50',
'code-service' => 40010
];
$freight = $freightPostOffice->setData($dataFreight)
->calcFreight());
<?php
class FreightPostOfficeService {
/**
* Source zip code to calculate the freight
*
* @var
*/
public $sourceZipCode;
/**
* Destiny zip code to calculate the freight
*
* @var
*/
public $destinyZipCode;
/**
* Weight of the product being shipped
*
* @var
*/
public $weightProduct;
/**
* Format of the product being shipped (1 = box, 2 = package)
*
* @var int
*/
public $formatProduct = 1;
/**
* Length of the product being shipped
*
* @var
*/
public $lengthProduct;
/**
* Height of the product being shipped
*
* @var
*/
public $heightProduct;
/**
* Width of the product being shipped
*
* @var
*/
public $widthProduct;
/**
* Diameter of the product being shipped
*
* @var int
*/
public $diameterProduct = 0;
/**
* Declared value of the product being shipped
*
* @var
*/
public $valueProduct;
/**
* Only for a particular person
*
* @var bool
*/
public $onlyOwner = true;
/**
* Receive delivery notifications
*
* @var bool
*/
public $receiveAlert = false;
/**
* Type of data return
*
* @var string
*/
protected $typeReturn = 'xml';
/**
* Post office service code
*
* @var
*/
public $codeService;
/**
* Company code if have contract with the post office
*
* @var
*/
public $codeCompany;
/**
* Company password if have contract with the post office
*
* @var
*/
public $passwordCompany;
/**
* Calculate the freight
*
* @return array
*/
public function calcFreight(){
$params = $this->buildQuery($this->createParams());
$curl = curl_init('http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?'.$params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$data = $this->loadXML($response);
foreach($data->cServico as $service){
return $this->formatService($service);
}
return ['type' => 'error', 'message' => 'Ocorreu um erro ao processar!'];
}
/**
* Format the received service information
*
* @param $service
*
* @return array
*/
private function formatService($service){
if($service->Erro != 0)
return ['type' => 'error', 'message' => $service->MsgErro];
return [
'type' => 'success',
'response' => [
'code' => $service->Codigo,
'price' => $service->Valor,
'deadline' => $service->PrazoEntrega
]
];
}
/**
* Create the parameters to make the requisition
*
* @return array
*/
private function createParams(){
return [
/*
* Company code and password, if you have a contract with the post office.
*/
'nCdEmpresa' => $this->codeCompany,
'sDsSenha' => $this->passwordCompany,
/*
* Zip codes
*/
'sCepOrigem' => $this->sourceZipCode,
'sCepDestino' => $this->destinyZipCode,
/*
* Product information to be sent
*/
'nVlPeso' => $this->weightProduct,
'nCdFormato' => $this->formatProduct,
'nVlComprimento' => $this->lengthProduct,
'nVlAltura' => $this->heightProduct,
'nVlLargura' => $this->widthProduct,
'nVlDiametro' => $this->diameterProduct,
'nVlValorDeclarado' => $this->valueProduct,
/*
* If delivery is to be delivered only to a particular person
*/
'sCdMaoPropria' => $this->convertAnswersBoolean($this->onlyOwner),
/*
* If you'd like to receive delivery notifications
*/
'sCdAvisoRecebimento' => $this->convertAnswersBoolean($this->receiveAlert),
/*
* Type of information to be returned
*/
'StrRetorno' => $this->typeReturn,
/*
* Post office service code
*/
'nCdServico' => $this->codeService
];
}
/**
* Convert some bool values to Yes or No
*
* @param $value
*
* @return string
*/
private function convertAnswersBoolean($value){
if(!is_bool($value))
return $value;
return ($value === true) ? 's' : 'n';
}
/**
* Build the query
*
* @param $params
*
* @return string
*/
private function buildQuery($params){
return http_build_query($params);
}
/**
* Load the XML
*
* @param $response
*
* @return \SimpleXMLElement
*/
private function loadXML($response){
return simplexml_load_string($response);
}
/**
* Define variables in class
*
* @param $data
*
* @return $this
*/
public function setData($data){
foreach($data as $key => $val){
$attr = camel_case($key);
if(property_exists($this, $attr))
$this->$attr = $val;
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment