Freight in my shopping cart is different than expected

Asked

Viewed 128 times

1

I’m going through a strange problem, I’m doing the integration of my site with the PagSeguro, I am calculating the freight on my side and the values are correct according to a conference of a page that is on my hosting, but when sending to the PagSeguro the freight value there is different from mine. The requested parameters are all being filled in correctly and I am not understanding this difference. I don’t even know how to get past what I’m going through, so I’ll try not to go too far.

What I’m doing is this, page Calculates.php

if(!isset($_SESSION)) {    
  session_start();
}

header("Content-Type: text/html; charset=ISO-8859-1",true);
require_once("Connections/conexao.php");

// validacao por banco de dados
function validar($conexao, $cep, $valor){
    $c = (int)preg_replace("/\D+/", "", $cep);
    $sql = "SELECT cep_inicial, cep_final, valor, status FROM frete_gratis WHERE status = 1";
    $sel = mysql_query($sql);

    while($row = mysql_fetch_array($sel)):
    if ($c >= $row['cep_inicial'] && $c <= $row['cep_final'] && $row['valor'] < $valor) return true;
    endwhile;       
}

mysql_select_db($database_conexao, $conexao);
$query_cepOrigem = "SELECT cep_1, cep_2 FROM cep_origem";
$cepOrigem = mysql_query($query_cepOrigem, $conexao) or die(mysql_error());
$row_cepOrigem = mysql_fetch_assoc($cepOrigem);

$CEP_ORIGEM = $row_cepOrigem['cep_1']."-".$row_cepOrigem['cep_2'];

// CEP
if ( isset($_POST['cep1']) && isset($_POST['cep2']) ){
    $cep1 = $_POST['cep1'];
    $cep2 = $_POST['cep2'];

    // CEP DE DESTINO
    // não pode ser inteiro - retira zero do inicio
    $CEP_DESTINO = $cep1."-".$cep2;
    $_SESSION["cepS"] = $CEP_DESTINO;
}
// VALOR
if ( isset($_POST['SUBTotal']) ){
    $VALOR = $_POST['SUBTotal'];
    if ($VALOR > 10000) {
        $VALOR = 10000;
    }
    $VALOR = str_replace(".", ",", $VALOR);
}

if ( isset($_POST['peso']) ){
    $PESOGR = $_POST['peso'];
    $PESOGR = $PESOGR / 1000;
    //$iteracoes = 1;
    if ($PESOGR > 30) {
        //$iteracoes = ceil($PESOGR / 30);
        $PESOGR = 30;
    }
}

 echo $VALOR;  echo "<br>";
 echo $PESOGR; echo "<br>";
 echo $CEP_ORIGEM; echo "<br>";
 echo $CEP_DESTINO;echo "<br>";

// CHAMADA DO ARQUIVO QUE CONTEM A CLASSE PgsFrete()
require_once('Frete.php');
// INSTANCIANDO A CLASSE
$frete = new PgsFrete;
// ZERANDO VALORES
$valorFrete = 0.0;
// CALCULANDO
$valorFrete = $frete->gerar($CEP_ORIGEM, $PESOGR, $VALOR, $CEP_DESTINO);

$vlrFreteSedex = str_replace(".","",$valorFrete["Sedex"]);
$vlrFretePac = str_replace(".","",$valorFrete["PAC"]);

// $valorFrete["Sedex"] = $valorFrete["Sedex"] - 4.60;
// $valorFrete["PAC"] = $valorFrete["PAC"] - 2;


if(is_array($valorFrete)) {
    // frete gratis para tipo de ceps
    $VALOR = str_replace(",", ".", $VALOR);
    if (validar($pdo,$CEP_DESTINO,$VALOR) ){
        echo '  
        <input type="radio" name="tipoEntrega" value="3" alt="0.00" rel="0.00" /> <strong>GRATIS (R$ 0,00)</strong><br/>
        ';
    } else {
        echo '  
        <input type="radio" name="tipoEntrega" value="2" alt="'.$vlrFreteSedex.'" rel="'.$valorFrete["Sedex"].'" /> <strong>SEDEX (R$ '.$valorFrete["Sedex"].')</strong><br/>
        <input type="radio" name="tipoEntrega" value="1" alt="'.$vlrFretePac.'" rel="'.$valorFrete["PAC"].'" /> <strong>PAC (R$ '.$valorFrete["PAC"].')</strong><br/>
        ';
    }   
}

Page Shipping.php

class PgsFrete {
private $_use     = 'curl';
private $_debug   = false;
private $_methods = array('curl');
private $_result;

public function PgsFrete()
{
    if ($this->debug()) {
        echo "\nPgsFrete started!";
    }
}

public function debug($debug=null)
{
    if (null===$debug) {
        return $this->_debug;
    }
    $this->_debug = (bool) $debug;
}

public function setUse($useMethod)
{
    if ('string'!==gettype($useMethod)) {
        throw new Exception('Method for setUse not allowed.'.
          'Method passed: '.var_export($useMethod, true));
    }
    $useMethod = strtolower($useMethod);
    if (!in_array($useMethod, $this->_methods)) {
        throw new Exception('Method for setUse not allowed.'.
          'Method passed: '.var_export($useMethod, true));
    }
    $this->_use = $useMethod;
    if ($this->debug()) {
        echo "\nMethod changed to ".strtoupper($useMethod);
    }
}

public function getUse()
{
    return $this->_use;
}

public function request($url, $post=null)
{
    $method = $this->getUse();
    if (in_array($method, $this->_methods)) {
        $method_name = '_request'.ucWords($method);
        if (!method_exists($this, $method_name)) {
          throw new Exception("Method $method_name does not exists.");
        }
        if ($this->debug()) {
            echo "\nTrying to get '$url' using ".strtoupper($method);
        }
        return call_user_func(array($this, $method_name), $url, $post);
    } else {
        throw new Exception('Method not seted.');
    }
}

private function _requestCurl($url, $post=null)
{
    $urlkey="URL:".md5("$url POST:$post");
    if(isset($_SESSION[$urlkey])){
      $this->_result = $_SESSION[$urlkey];
      return;
    }
    $parse = parse_url($url);
    $ch    = curl_init();
    if ('https'===$parse['scheme']) {
        // Nao verificar certificado
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    }
    curl_setopt($ch, CURLOPT_URL, $url); // Retornar o resultado
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Retornar o resultado
    if ($post) {
        curl_setopt($ch, CURLOPT_POST, true); // Ativa o modo POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Insere os POSTs
    }
    $result = curl_exec($ch);
    curl_close($ch);
    $this->_result = $result;
    $_SESSION[$urlkey]=$result;
}

public function gerar($CepOrigem, $Peso, $Valor, $Destino)
{
    $Peso = str_replace('.',',',$Peso);
    $url = "https://pagseguro.uol.com.br/desenvolvedor/simulador_de_frete_calcular.jhtml?postalCodeFrom={$CepOrigem}&weight={$Peso}&value={$Valor}&postalCodeTo={$Destino}";                
    $this->request($url);
    $result = explode('|',$this->_result);
    $valores=array();
    if($result[0]=='ok'){
      $valores['Sedex']=$result[3];
      $valores['PAC']=$result[4];
    }else{
      die($result[1]);
    }
    return $valores;
}

}

Sending to the Pagseguro:

if ($Tpfreight == 3){ $weight = 0; $paymentRequest->addItem($cod_prod, $prod_name, $quantidade_prod, $vlr, $weight); } Else { $paymentRequest->addItem($cod_prod, $prod_name, $quantidade_prod, $vlr, $peso, $Vlrfrete); }

Some images of the cart and console:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • I think it is a question that Pagseguro can answer more clearly. Already contacted the support?

  • Hello, yes, but so far they haven’t answered.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.