Guest User

Untitled

a guest
May 1st, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(0);
  4. ini_set('display_errors', 0);
  5.  
  6. if ( basename( $_SERVER['PHP_SELF'] ) == basename(__FILE__) ) { die ("403 Forbidden"); }
  7.  
  8. final class MelhorEnvio {
  9. private $_parametro = array();
  10. private $_url;
  11. private $_token;
  12. private $_verbo;
  13. private $_dados;
  14.  
  15. public function __call($name, $args) {
  16. return 'Melhor Envio: Erro 500 (Internal Server Error). A função solicitada não existe.';
  17. }
  18.  
  19. public function setParametros($dados = array()){
  20. return $this->_parametro = $dados;
  21. }
  22.  
  23. public function getCotacao() {
  24. $requisitos = $this->validar_requisitos();
  25. if ($requisitos == false) { return false; }
  26.  
  27. $campos = array('chave', 'token', 'from_postal_code', 'to_postal_code', 'insurance_value', 'receipt', 'own_hand', 'collect', 'services');
  28. $parametros = $this->validar_parametros($campos);
  29. if ($parametros == false) {
  30. $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.');
  31. return false;
  32. }
  33.  
  34. $chave = $this->validar_chave();
  35. if ($chave == false) { return false; }
  36.  
  37. $token = trim($this->_parametro['token']);
  38. if (empty($token)) {
  39. $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O token não é válido.');
  40. return false;
  41. }
  42.  
  43. $from_postal_code = preg_replace('/[^0-9]/', '', $this->_parametro['from_postal_code']);
  44. if (strlen($from_postal_code) != 8) {
  45. $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O CEP de origem não é válido.');
  46. return false;
  47. }
  48.  
  49. $to_postal_code = preg_replace('/[^0-9]/', '', $this->_parametro['to_postal_code']);
  50. if (strlen($to_postal_code) != 8) {
  51. $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O CEP de destino não é válido.');
  52. return false;
  53. }
  54.  
  55. $products = $this->_parametro['products'];
  56. if (count($products) == 0) {
  57. $this->debug('Melhor Envio: Erro 412 (Precondition Failed). A dimensão dos produtos não foi informada.');
  58. return false;
  59. }
  60.  
  61. $insurance_value = $this->_parametro['insurance_value'];
  62. if ($insurance_value < 0) {
  63. $this->debug('Melhor Envio: Erro 412 (Precondition Failed). O valor do pedido é menor que zero.');
  64. return false;
  65. }
  66.  
  67. $this->_url = 'https://www.melhorenvio.com.br/api/v2/me/shipment/calculate';
  68. $this->_token = $token;
  69. $this->_verbo = 'POST';
  70.  
  71. $this->_dados['from']['postal_code'] = $from_postal_code;
  72. $this->_dados['to']['postal_code'] = $to_postal_code;
  73. $this->_dados['products'] = $products;
  74. $this->_dados['options']['insurance_value'] = (float) $insurance_value;
  75. $this->_dados['options']['receipt'] = (bool) $this->_parametro['receipt'];
  76. $this->_dados['options']['own_hand'] = (bool) $this->_parametro['own_hand'];
  77. $this->_dados['options']['collect'] = (bool) $this->_parametro['collect'];
  78. $this->_dados['services'] = $this->_parametro['services'];
  79.  
  80. $this->debug(json_encode($this->_dados));
  81.  
  82. $resposta = $this->conectar();
  83.  
  84. return $resposta;
  85. }
  86.  
  87. public function getAgencias() {
  88. $requisitos = $this->validar_requisitos();
  89. if ($requisitos == false) { return false; }
  90.  
  91. $campos = array('chave', 'token', 'company', 'state', 'city');
  92. ................................................................................
  93. .................................................
  94. ....................
Add Comment
Please, Sign In to add comment