giansalex / sufel

SUFEL - Libreria para llevar acabo la Consulta de comprobantes electrónicos exigido por Sunat
6 stars 5 forks source link

ayuda #8

Closed abelnexus closed 6 years ago

abelnexus commented 6 years ago

amigo, esto tambien es aceptado en vez de enviar email no ? o son diferentes consulta y envio de representacion impresa

abelnexus commented 6 years ago

la verdad no entiendo nada de sufel-angular :c

abelnexus commented 6 years ago

hola amigo, por alguna razón ahora me vuelve a decir "401 (Unauthorized)" envie otro documento y copie el token y nada :( (las api rest son muy complicados :'()

giansalex commented 6 years ago

el tema es la seguirdad, el token expira en pocas horas, por ello te devuelve un token y expire (esta en formato), alli verificas que el token ya vencio y tienes que volver a solicitarlo como la primera vez.

var expire = new Date(data.expire * 1000);

console.log(expire);

con eso puedes saber la fecha y hora exacta de expiracion

abelnexus commented 6 years ago

este es el ultimo paso que me falta y es lo mas complicado :( como programo para que se suba a la api automaticamente al recibir el cdr, si nisiquiera logro mostrar la info,pdf o xml al hacer la consulta al api peor aun teniendo que cambiar los tokens :(

giansalex commented 6 years ago

subir los comprobantes eso se puede hacer desde PHP, no te recomiendo que lo hagas por js, por que tendrías que colocar el token. y entonces cualquiera podria subir los archivos. En php todo se realizaria desde el servidor.

abelnexus commented 6 years ago

y como subiria y consultaria sinajax y jquery ? como se hace en php

abelnexus commented 6 years ago

lo que quiero es eso que desde mi servidor mismo se suba y tambien se consulte

giansalex commented 6 years ago

para ello se usa curl (php-extension), tambien hay librerias como Guzzle, php-curl-class http://php.net/manual/es/book.curl.php

abelnexus commented 6 years ago

me das un codigo basico amigo para subir el archivo ?

abelnexus commented 6 years ago

estoy probando con este codigo y nisiquiera habre solo se queda cargando

`<?php $url = 'http://localhost:82/sufel/api/company/auth'; $data = array("ruc" => "10486113532", "password" => "fer123");
$data = json_encode($data);

echo CurlSendPostJson($url,$data);

function CurlSendPostJson($url='http://localhost:82/sufel/api/company/auth',$datajson){
$ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $datajson); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($datajson)));

return $result = curl_exec($ch); } ?>`

abelnexus commented 6 years ago

$url = 'http://localhost:82/sufel/api/client/document/info'; $data = array("emisor" => "10486113532", "tipo" => "01", "documento" => "F001-000100", "fecha" => "2018-03-15", "total" => 120.00 );
$data = json_encode($data);
// Send post data Json format echo CurlSendPostJson($url,$data); // send curl post
function CurlSendPostJson($url,$datajson){
$ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $datajson); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($datajson))); //curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers return $result = curl_exec($ch); }

abelnexus commented 6 years ago

LO ENVIE TAMBIEN CON GET Y NO RETORNA NADA NI BOTA ERROR NADA DE NADA

abelnexus commented 6 years ago

con este codigo me retorna el token y expire] $url = 'http://localhost:82/sufel/api/company/auth'; // Curl get method $data = array("ruc" => "10486113532", "password" => "fer123"

        );   

$data = json_encode($data); echo CurlSendPostJson($url,$data); // send curl post
function CurlSendPostJson($url,$datajson){
$ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $datajson); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($datajson))); //curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers return $result = curl_exec($ch); }

abelnexus commented 6 years ago

captura

abelnexus commented 6 years ago

pero para hacer consulta para el pdf,info,xml nada no encuentro referencia

abelnexus commented 6 years ago

tambien se tiene que enviar token ? $xml = base64_encode(file_get_contents("10486113532-01-F001-000100.xml")); $pdf= base64_encode(file_get_contents("10486113532-01-F001-000100.pdf")); $url = 'http://localhost:82/sufel/api/company/add-document'; // Curl get method $data = array("xml" => $xml, "pdf" => $pdf
);
$data = json_encode($data); echo CurlSendPostJson($url,$data); // send curl post
function CurlSendPostJson($url,$datajson){
$ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $datajson); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($datajson))); //curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers return $result = curl_exec($ch); }

giansalex commented 6 years ago

y el token de seguridad para subir el documento. array('Content-Type: application/json','Content-Length: ' . strlen($datajson), 'Authorization: Bearer TOKEN');

abelnexus commented 6 years ago

ya me sale amigo, lo que no entiendo es como hago para enviarlo directamente desde mi sistema al momento de recibir el cdr ya que generar primero el token luego configurar para enviar el xml y pdf me resulta impensable tiene que ser automatico todo

abelnexus commented 6 years ago

iluminame giansalex tu eres el unico que puede ayudarme, ya tengo alg cerebral tratando de encontrar una solucion

abelnexus commented 6 years ago

como haria amigo para decodificar(base64_decode($pdf)) un pdf que guarde en mi bd y mostrarlo

abelnexus commented 6 years ago

probe con este codigo y manda error

$xml = base64_encode(file_get_contents("10486113532-01-F001-000052.xml")); $pdf= base64_encode(file_get_contents("10486113532-01-F001-000052.pdf"));

$name = base64_decode($pdf); //file_get_contents is standard function $content = $name; header('Content-Type: application/pdf'); header('Content-Length: '.strlen( $content )); header('Content-disposition: inline; filename="'.$name.'"'); header('Cache-Control: public, must-revalidate, max-age=0'); echo $content;

abelnexus commented 6 years ago

y si uso las consultas de sunat no sirve ? :'( no puedo usar la api rest no lo entiendo

giansalex commented 6 years ago

la consulta de SUNAT solo te permite saber si el comprobante ha sido informado, pero no tienes acceso al XML ni PDF

abelnexus commented 6 years ago

existe alguna manera de usar sufel sin el token ? solo enviar y consutlar

abelnexus commented 6 years ago

o como podria hacer otro sistema o codigo y alojarlo en un hosting donde solo se suba y consulte archivos ayudame amigo

giansalex commented 6 years ago

de la misma forma, si ya los tienes guardado los archivos, solo creas un formulario con los datos a consultar, luego en el servidor consultas en la base de datos, si existe la venta devuelves los archivos

abelnexus commented 6 years ago

esta bien amigo lo hare siguiendo tu modelo, es recomendable subir los archivos en base64 o puedo hacerlo como archivos .pdf y .xml ?

abelnexus commented 6 years ago

lo intentare hoy mas con sufel solo amigo ayudame a capturar el token env ariables al recibirlos por curl

abelnexus commented 6 years ago

este codigo me regresa el token y expire y me los muestra en pantalla y quiero ponerlos en variables como seria ? $url = 'http://localhost:82/sufel/api/company/auth'; // Curl get method $data = array("ruc" => "10000000002", "password" => "fer123" $data = json_encode($data); echo CurlSendPostJson($url,$data); // send curl post function CurlSendPostJson($url,$datajson){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $datajson); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($datajson))); //curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers return $result = curl_exec($ch);

abelnexus commented 6 years ago

captura

giansalex commented 6 years ago

en el result te devuelve un cadena json:

$result = curl_exec($ch);
$obj = json_decode($result);
return $obj->token;
abelnexus commented 6 years ago

quiero ponerlo $token = "tokenrecibido" , $expire = "expirerecibido"

abelnexus commented 6 years ago

Notice: Trying to get property 'token' of non-object in C:\xampp\htdocs\sufel\auth.php on line 20 si no le pongo return no funciona ?

$result = curl_exec($ch); $obj = json_decode($result); echo $obj->token;

giansalex commented 6 years ago

probablemente no recibiste respuesta, json_decode retorna false si no pudo decodificar:

$result = curl_exec($ch);
$obj = json_decode($result);
if ($obj === false) {
    return;
}
$token = $obj->token;
$expire =$obj->expire; 
abelnexus commented 6 years ago

me bota error cuando no tiene return, y cuando pongo return se imprime solo en la pantalla en un array

Notice: Trying to get property 'token' of non-object in C:\xampp\htdocs\sufel\auth.php on line 24

Notice: Trying to get property 'expire' of non-object in C:\xampp\htdocs\sufel\auth.php on line 25

abelnexus commented 6 years ago

pero al parecer no decodifica el array y lo pone en las variables

abelnexus commented 6 years ago

no reconoce las variables cuando quiero usar fuera de la funcion CurlSendPostJson

giansalex commented 6 years ago

es por que solo existen dentro de la funcion, por ello debes retornarlos, o retornar $obj y afuera acceder a token y expire

abelnexus commented 6 years ago

$url = 'http://localhost:82/sufel/api/company/auth'; // Curl get method $data = array("ruc" => "10000000002", "password" => "fer123"

    );   

$data = json_encode($data); echo CurlSendPostJson($url,$data); function CurlSendPostJson($url,$datajson){ // send curl post $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $datajson); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($datajson))); //curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers $result = curl_exec($ch); $obj = json_decode($result); return $obj; } echo $token = $obj->token;

abelnexus commented 6 years ago

Notice: Undefined variable: obj in C:\xampp\htdocs\sufel\auth.php on line 22

Notice: Trying to get property 'token' of non-object in C:\xampp\htdocs\sufel\auth.php on line 22

giansalex commented 6 years ago

$obj solo existe dentro de la funcion tienes que capturarlo al retornar el valor de la funcion.

$obj = function_to_request();
abelnexus commented 6 years ago

no se si soy un bruto de m... XD pero no sale

abelnexus commented 6 years ago

maldita sea tan complicado es obtener una maldita variable de una función ?

abelnexus commented 6 years ago

lo hice ahora sin funcion

$url = 'http://localhost:82/sufel/api/company/auth'; // Curl get method $data = array("ruc" => "10000000002", "password" => "fer123" );
$data = json_encode($data); // send curl post $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data))); //curl_setopt($ch,CURLOPT_HEADER, true); //if you want headers $result = curl_exec($ch);

$obj = json_decode($result);

abelnexus commented 6 years ago

al darle echo a $result me muestra el array pero al poenr este codigo echo $token=$obj->token; me dice: Notice: Trying to get property 'token' of non-object in

abelnexus commented 6 years ago

y al usar esto echo $token=$obj['token']; echo $expire=$obj['expire']; no muestra nada :(

giansalex commented 6 years ago

tal vez sea la version de PHP, pero el json_decode debe devolver un object, prueba viendo que tipo de datos salen:

var_dump($obj);
abelnexus commented 6 years ago

null

abelnexus commented 6 years ago

mi php es 7.2.0

giansalex commented 6 years ago

y en result: $result = curl_exec($ch); var_dump($result);