Closed abelnexus closed 6 years ago
la verdad no entiendo nada de sufel-angular :c
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 :'()
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
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 :(
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.
y como subiria y consultaria sinajax y jquery ? como se hace en php
lo que quiero es eso que desde mi servidor mismo se suba y tambien se consulte
para ello se usa curl (php-extension), tambien hay librerias como Guzzle, php-curl-class http://php.net/manual/es/book.curl.php
me das un codigo basico amigo para subir el archivo ?
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); } ?>`
$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);
}
LO ENVIE TAMBIEN CON GET Y NO RETORNA NADA NI BOTA ERROR NADA DE NADA
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);
}
pero para hacer consulta para el pdf,info,xml nada no encuentro referencia
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);
}
y el token de seguridad para subir el documento.
array('Content-Type: application/json','Content-Length: ' . strlen($datajson), 'Authorization: Bearer TOKEN');
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
iluminame giansalex tu eres el unico que puede ayudarme, ya tengo alg cerebral tratando de encontrar una solucion
como haria amigo para decodificar(base64_decode($pdf)) un pdf que guarde en mi bd y mostrarlo
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;
y si uso las consultas de sunat no sirve ? :'( no puedo usar la api rest no lo entiendo
la consulta de SUNAT solo te permite saber si el comprobante ha sido informado, pero no tienes acceso al XML ni PDF
existe alguna manera de usar sufel sin el token ? solo enviar y consutlar
o como podria hacer otro sistema o codigo y alojarlo en un hosting donde solo se suba y consulte archivos ayudame amigo
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
esta bien amigo lo hare siguiendo tu modelo, es recomendable subir los archivos en base64 o puedo hacerlo como archivos .pdf y .xml ?
lo intentare hoy mas con sufel solo amigo ayudame a capturar el token env ariables al recibirlos por curl
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);
en el result te devuelve un cadena json:
$result = curl_exec($ch);
$obj = json_decode($result);
return $obj->token;
quiero ponerlo $token = "tokenrecibido" , $expire = "expirerecibido"
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;
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;
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
pero al parecer no decodifica el array y lo pone en las variables
no reconoce las variables cuando quiero usar fuera de la funcion CurlSendPostJson
es por que solo existen dentro de la funcion, por ello debes retornarlos, o retornar $obj
y afuera acceder a token y expire
$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;
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
$obj solo existe dentro de la funcion tienes que capturarlo al retornar el valor de la funcion.
$obj = function_to_request();
no se si soy un bruto de m... XD pero no sale
maldita sea tan complicado es obtener una maldita variable de una función ?
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);
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
y al usar esto echo $token=$obj['token']; echo $expire=$obj['expire']; no muestra nada :(
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);
null
mi php es 7.2.0
y en result: $result = curl_exec($ch); var_dump($result);
amigo, esto tambien es aceptado en vez de enviar email no ? o son diferentes consulta y envio de representacion impresa