function testPreparaValidar() { $this->assertPattern('@Token=123456@', RetornoPagSeguro::_preparaDados('', true), 'O Token foi inserido'); $this->assertPattern('@Comando=validar@', RetornoPagSeguro::_preparaDados('', true), 'Adicinado o Comando'); }
/** * verifica * * Verifica o tipo de conexão aberta e envia os dados vindos * do post * * @access public * * @use RetornoPagSeguro::_tipoenvio() * @global string $_retPagSeguroErrNo Numero de erro do pagseguro * @global string $_retPagSeguroErrStr Texto descritivo do erro do pagseguro * @param array $post Array contendo os posts do pagseguro * @param bool $tipoEnvio (opcional) Verifica o tipo de envio do post * @return bool */ function verifica($post, $tipoEnvio = false) { global $_retPagSeguroErrNo, $_retPagSeguroErrStr; if ('array' !== gettype($tipoEnvio)) { $tipoEnvio = RetornoPagSeguro::_tipoEnvio(); } $spost = RetornoPagSeguro::_preparaDados($post); if (!in_array($tipoEnvio[0], array('curl', 'fsocket'))) { return false; } $confirma = false; if ($tipoEnvio[0] === 'curl') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $tipoEnvio[1]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $spost); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $resp = curl_exec($ch); if (!RetornoPagSeguro::not_null($resp)) { curl_setopt($ch, CURLOPT_URL, $tipoEnvio[1]); $resp = curl_exec($ch); } curl_close($ch); $confirma = strcmp($resp, 'VERIFICADO') == 0; } elseif ($tipoEnvio[0] === 'fsocket') { if (!$tipoEnvio[2]) { die("{$_retPagSeguroErrStr} ({$_retPagSeguroErrNo})"); } else { $cabecalho = "POST {$tipoEnvio[1]} HTTP/1.0\r\n"; $cabecalho .= "Content-Type: application/x-www-form-urlencoded\r\n"; $cabecalho .= "Content-Length: " . strlen($spost) . "\r\n\r\n"; $resp = ''; fwrite($tipoEnvio[2], "{$cabecalho}{$spost}"); while (!feof($tipoEnvio[2])) { $resp = fgets($tipoEnvio[2], 1024); if (strcmp($resp, 'VERIFICADO') == 0) { $confirma = strcmp($resp, 'VERIFICADO') == 0; $confirma = true; break; } } fclose($tipoEnvio[2]); } } if ($confirma && function_exists('retorno_automatico')) { $itens = array('VendedorEmail', 'TransacaoID', 'Referencia', 'TipoFrete', 'ValorFrete', 'Anotacao', 'DataTransacao', 'TipoPagamento', 'StatusTransacao', 'CliNome', 'CliEmail', 'CliEndereco', 'CliNumero', 'CliComplemento', 'CliBairro', 'CliCidade', 'CliEstado', 'CliCEP', 'CliTelefone', 'NumItens'); foreach ($itens as $item) { if (!isset($post[$item])) { $post[$item] = ''; } if ($item == 'ValorFrete') { $post[$item] = str_replace(',', '.', $post[$item]); } } $produtos = array(); for ($i = 1; isset($post["ProdID_{$i}"]); $i++) { $produtos[] = array('ProdID' => $post["ProdID_{$i}"], 'ProdDescricao' => $post["ProdDescricao_{$i}"], 'ProdValor' => (double) str_replace(',', '.', $post["ProdValor_{$i}"]), 'ProdQuantidade' => $post["ProdQuantidade_{$i}"], 'ProdFrete' => (double) str_replace(',', '.', $post["ProdFrete_{$i}"]), 'ProdExtras' => (double) str_replace(',', '.', $post["ProdExtras_{$i}"])); } retorno_automatico($post['VendedorEmail'], $post['TransacaoID'], $post['Referencia'], $post['TipoFrete'], $post['ValorFrete'], $post['Anotacao'], $post['DataTransacao'], $post['TipoPagamento'], $post['StatusTransacao'], $post['CliNome'], $post['CliEmail'], $post['CliEndereco'], $post['CliNumero'], $post['CliComplemento'], $post['CliBairro'], $post['CliCidade'], $post['CliEstado'], $post['CliCEP'], $post['CliTelefone'], $produtos, $post['NumItens']); } return $confirma; }
/** * _confirma * * Faz a parte Server-Side, verificando os dados junto ao PagSeguro * * @param array $tipoEnvio Array com a configuração gerada * por {@see Retorno::_tipoEnvio()} * @param array $post Dados vindos no POST do PagSeguro para serem * verificados * * @return bool * @global string $_retPagSeguroErrNo Numero de erro do pagseguro * @global string $_retPagSeguroErrStr Texto descritivo do erro do pagseguro */ static function _confirma($tipoEnvio, $post) { global $_retPagSeguroErrNo, $_retPagSeguroErrStr; $spost = RetornoPagSeguro::_preparaDados($post); $confirma = false; // Usando a biblioteca cURL if ($tipoEnvio[0] === 'curl') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $tipoEnvio[1]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $spost); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Deve funcionar apenas em teste if (defined('PAGSEGURO_AMBIENTE_DE_TESTE')) { curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $resp = curl_exec($ch); if (!RetornoPagSeguro::notNull($resp)) { curl_setopt($ch, CURLOPT_URL, $tipoEnvio[1]); $resp = curl_exec($ch); } curl_close($ch); $confirma = strcmp($resp, 'VERIFICADO') == 0; // Usando fsocket } elseif ($tipoEnvio[0] === 'fsocket') { if (!$tipoEnvio[2]) { die("{$_retPagSeguroErrStr} ({$_retPagSeguroErrNo})"); } else { $cabecalho = "POST {$tipoEnvio[1]} HTTP/1.0\r\n"; $cabecalho .= "Content-Type: application/x-www-form-urlencoded\r\n"; $cabecalho .= "Content-Length: " . strlen($spost) . "\r\n\r\n"; $resp = ''; fwrite($tipoEnvio[2], "{$cabecalho}{$spost}"); while (!feof($tipoEnvio[2])) { $resp = fgets($tipoEnvio[2], 1024); if (strcmp($resp, 'VERIFICADO') == 0) { $confirma = true; break; } } fclose($tipoEnvio[2]); } } return $confirma; }