Last active
March 23, 2020 22:58
-
-
Save davidchc/5326321 to your computer and use it in GitHub Desktop.
Classe Para Retornar o valor do Frete do Simulador do PagSeguro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FretePagSeguro{ | |
private $cepOrigem; | |
private $cepDestino; | |
private $peso; | |
private $preco; | |
private $url = 'https://pagseguro.uol.com.br/desenvolvedor/simulador_de_frete_calcular.jhtml?postalCodeFrom=%s&weight=%s&value=%s&postalCodeTo=%s'; | |
private $sedex; | |
private $pac; | |
public function __construct($cepOrigem){ | |
$this->cepOrigem = $cepOrigem; | |
} | |
public function setCepDestino($cep){ | |
$this->cepDestino = $cep; | |
return $this; | |
} | |
public function setPeso($peso){ | |
$this->peso = $peso; | |
return $this; | |
} | |
public function setPreco($preco){ | |
$this->preco = $preco; | |
return $this; | |
} | |
private function acessarCURL($url){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
public function executar(){ | |
$url = sprintf($this->url, $this->cepOrigem, $this->peso, $this->preco, $this->cepDestino); | |
$result = $this->acessarCURL($url); | |
list($resposta, $uf, $item, $sedex, $pac) = explode('|', $result); | |
if($resposta !== 'ok'){ | |
return false; | |
} | |
$this->sedex = $sedex; | |
$this->pac = $pac; | |
return true; | |
} | |
public function getSedex(){ | |
return number_format($this->sedex, 2, ',', '.'); | |
} | |
public function getPac(){ | |
return number_format($this->pac, 2, ',', '.'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once "FretePagSeguro.php"; | |
$frete = new FretePagSeguro('27963-762'); | |
$frete->setCepDestino('26255-170')->setPeso('0,300')->setPreco('200,00'); | |
if($frete->executar()){ | |
echo "SEDEX ".$frete->getSedex(); | |
echo '<br />'; | |
echo "PAC ".$frete->getPac(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start() | |
include_once "FretePagSeguro.php"; | |
//Instancia a classe PDO | |
$pdo = new PDO('mysql:host=localhost;dbname=lojavirtual', 'root', ''); | |
//Pega apenas os ID dos produtos | |
$ids = array_keys($_SESSION['carrinho']); | |
//Select retornando, id, preco, peso, filtrando pelos IDs dos produtos | |
$strSQL = "SELECT id, preco, peso FROM produtos WHERE id IN ('".implode($ids, ',')."')"; | |
//inicia o valor total | |
$valorTotal = 0; | |
//inicia o peso total | |
$pesoTotal = 0; | |
foreach($pdo->query($strSQL) as $produto){ | |
$id = $produto['id']; | |
$qtd = $_SESSION['carrinho'][$id]; | |
$pesoTotal += $produto['peso'] * $qtd; | |
$valorTotal += $produto['preco'] * $qtd; | |
} | |
//troca o ponto por , pra enviar para o pagseguro | |
$valorTotalFormado = str_replace('.', ',', $valorTotal); | |
$pesoTotalFormado = str_replace('.', ',', $pesoTotal); | |
//instancia a classe para calcular o frete | |
$frete = new FretePagSeguro('27963-762'); | |
$frete->setCepDestino('26255-170') | |
->setPeso($pesoTotalFormado) | |
->setPreco($valorTotalFormado); | |
if($frete->executar()){ | |
echo "SEDEX ".$frete->getSedex(); | |
echo '<br>'; | |
echo "PAC ".$frete->getPac(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment