Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(0);
- ini_set('display_errors', 0);
- if ( basename( $_SERVER['PHP_SELF'] ) == basename(__FILE__) ) { die ("403 Forbidden"); }
- final class MelhorEnvio {
- private $_parametro = array();
- private $_url;
- private $_token;
- private $_verbo;
- private $_dados;
- public function __call($name, $args) {
- return 'Melhor Envio: Erro 500 (Internal Server Error). A função solicitada não existe.';
- }
- public function setParametros($dados = array()){
- return $this->_parametro = $dados;
- }
- public function getCotacao() {
- $requisitos = $this->validar_requisitos();
- if ($requisitos == false) { return false; }
- $campos = array('chave', 'token', 'from_postal_code', 'to_postal_code', 'insurance_value', 'receipt', 'own_hand', 'collect', 'services');
- $parametros = $this->validar_parametros($campos);
- if ($parametros == false) {
- $this->debug('Erro 412 (Precondition Failed). Não foram enviados todos os dados necessários para realizar a cotação de frete no Melhor Envio.');
- return false;
- }
- $chave = $this->validar_chave();
- if ($chave == false) { return false; }
- $token = trim($this->_parametro['token']);
- if (empty($token)) {
- $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O token não é válido.');
- return false;
- }
- $from_postal_code = preg_replace('/[^0-9]/', '', $this->_parametro['from_postal_code']);
- if (strlen($from_postal_code) != 8) {
- $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O CEP de origem não é válido.');
- return false;
- }
- $to_postal_code = preg_replace('/[^0-9]/', '', $this->_parametro['to_postal_code']);
- if (strlen($to_postal_code) != 8) {
- $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O CEP de destino não é válido.');
- return false;
- }
- $products = $this->_parametro['products'];
- if (count($products) == 0) {
- $this->debug('Melhor Envio: Erro 412 (Precondition Failed). A dimensão dos produtos não foi informada.');
- return false;
- }
- $insurance_value = $this->_parametro['insurance_value'];
- if ($insurance_value < 0) {
- $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O valor do pedido é menor que zero.');
- return false;
- }
- $this->_url = 'https://www.melhorenvio.com.br/api/v2/me/shipment/calculate';
- $this->_token = $token;
- $this->_verbo = 'POST';
- $this->_dados['from']['postal_code'] = $from_postal_code;
- $this->_dados['to']['postal_code'] = $to_postal_code;
- $this->_dados['products'] = $products;
- $this->_dados['options']['insurance_value'] = (float) $insurance_value;
- $this->_dados['options']['receipt'] = (bool) $this->_parametro['receipt'];
- $this->_dados['options']['own_hand'] = (bool) $this->_parametro['own_hand'];
- $this->_dados['options']['collect'] = (bool) $this->_parametro['collect'];
- $this->_dados['services'] = $this->_parametro['services'];
- $this->debug(json_encode($this->_dados));
- $resposta = $this->conectar();
- return $resposta;
- }
- public function getAgencias() {
- $requisitos = $this->validar_requisitos();
- if ($requisitos == false) { return false; }
- $campos = array('chave', 'token', 'company', 'state', 'city');
- ................................................................................
- .................................................
- ....................
Add Comment
Please, Sign In to add comment