Error : peer certificate can not be authenticated...

HMG en Español

Moderator: Rathinagiri

Post Reply
User avatar
edufloriv
Posts: 240
Joined: Thu Nov 08, 2012 3:42 am
DBs Used: DBF, MariaDB, MySQL, MSSQL, MariaDB
Location: PERU

Error : peer certificate can not be authenticated...

Post by edufloriv »

Saludos amigos, yo de nuevo,

Estoy tratando de levantar mi primer documento electrónico al webservice de Sunat, este es mi código de prueba:

Code: Select all

* ------------------------------------------------------ *
* SISTEMA     :                                          *
* PRG         :                                          *
* CREADO      :                                          *
* ACTUALIZADO :                                          *
* AUTOR       :                                          *
* COMENTARIOS :                                          *
* ------------------------------------------------------ *

#include <minigui.ch>
#include <hbcurl.ch>

FUNCTION MAIN

   DEFINE WINDOW Win_1;
      AT 0,0 ;
      WIDTH 640 HEIGHT 460 ;
      TITLE 'Consulta CPE' ;
      ON INIT INISETS() ;
      MAIN

      DEFINE MAIN MENU
         POPUP "&Operaciones"
            ITEM '&1. Buscar CPE'                  ACTION CPE_Cons()
            ITEM '&2. Enviar CPE a Sunat'          ACTION CPE_Send()
         END POPUP
      END MENU

//
// BARRA DE ESTADO
//
      DEFINE STATUSBAR
         STATUSITEM "..."
         CLOCK WIDTH 70
         DATE  WIDTH 90
      END STATUSBAR

   END WINDOW

   CENTER WINDOW Win_1
   ACTIVATE WINDOW Win_1

RETURN


*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
* FUNCION...: INISETS
* COMENTARIO: INICIALIZA LOS SETEOS DEL SISTEMA.
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------

PROC INISETS

SET BELL       Off
SET CONSOLE    Off
SET CURSOR     Off
SET DATE TO BRIT
SET DELETED    On
SET DELIMITERS Off
SET ECHO       Off
SET EPOCH TO 1990
SET MESSAGE TO 22 CENTER
SET SAFETY     Off
SET SCOREBOARD Off
SET TALK       Off
SET WRAP       On
ALTD(0)

RETURN


*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------

Proc CPE_Cons

LOCAL cxml

cxml := '<soapenv:Envelope xmlns:ser="http://service.sunat.gob.pe" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">'+chr(13)+chr(10)
cxml += '<soapenv:Header>'+chr(13)+chr(10)
cxml += '<wsse:Security>'+chr(13)+chr(10)
cxml += '<wsse:UsernameToken>'+chr(13)+chr(10)
cxml += '<wsse:Username>10074902419MODDATOS</wsse:Username>'+chr(13)+chr(10)
cxml += '<wsse:Password>moddatos</wsse:Password>'+chr(13)+chr(10)
cxml += '</wsse:UsernameToken>'+chr(13)+chr(10)
cxml += '</wsse:Security>'+chr(13)+chr(10)
cxml += '</soapenv:Header>'+chr(13)+chr(10)
cxml += '<soapenv:Body>'+chr(13)+chr(10)
cxml += '<ser:getStatus>'+chr(13)+chr(10)
cxml += '<rucComprobante>20101088881</rucComprobante>'+chr(13)+chr(10)
cxml += '<tipoComprobante>01</tipoComprobante>'+chr(13)+chr(10)
cxml += '<serieComprobante>F001</serieComprobante>'+chr(13)+chr(10)
cxml += '<numeroComprobante>00002869</numeroComprobante>'+chr(13)+chr(10)
cxml += '</ser:getStatus>'+chr(13)+chr(10)
cxml += '</soapenv:Body>'+chr(13)+chr(10)
cxml += '</soapenv:Envelope>'+chr(13)+chr(10)

canswer := SunatConsultar(cxml)
canswer := strtran(canswer,"><",">"+chr(10)+"<")
memowrit("soapanswer.txt",canswer)

RETURN


*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------

FUNC SunatConsultar(cxml)

local endpointUrl,curlHandle,curlErr
local aHeader,cc1

cc1 := ""

endpointUrl := "https://www.sunat.gob.pe/ol-it-wsconscpegem/billConsultService"

aHeader := {}
AADD(aHeader,'Content-Type: text/xml' )
AADD(aHeader,'Content-Type: text/xml;charset=ISO-8859-1' )
AADD(aHeader,'Content-Length: '+STR(len(cxml)) )
AADD(aHeader,'SOAPAction: getStatus' )

curlHandle := curl_easy_init()

if ! empty(curlHandle)

   /* Specify the Header  data */
   curl_easy_setopt(curlHandle,HB_CURLOPT_HTTPHEADER,aHeader)

   /* Set the endpoint to send the POST to */
   curl_easy_setopt(curlHandle, HB_CURLOPT_URL, endpointUrl)

   /* Setup response data */
   curl_easy_setopt( curlHandle, HB_CURLOPT_DOWNLOAD )
   curl_easy_setopt( curlHandle, HB_CURLOPT_DL_BUFF_SETUP )

   /* Specify the POST data */
   curl_easy_setopt(curlHandle, HB_CURLOPT_POST, 1)
   curl_easy_setopt(curlHandle, HB_CURLOPT_POSTFIELDS, cxml)

   /* Do everything */
   curlErr := curl_easy_perform(curlHandle)

   /* Report any errors */
   if empty(curlErr)
   /* store response in variable */
   cc1 := curl_easy_dl_buff_get( curlHandle )
   else
      msginfo( curl_easy_strerror(curlErr) )
   endif

else

   msginfo( "No handle" )

endif

if ! empty(curlHandle)
   /* Clean-up libcurl */
   curl_global_cleanup( curlHandle )
else
   msginfo( "Error" )
endif

if empty(cc1)
   msginfo( "Error" )
endif

return cc1


*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------

PROC CPE_Send

LOCAL cxml

cxml := '<soapenv:Envelope xmlns:ser="http://service.sunat.gob.pe" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">'+chr(13)+chr(10)
cxml += '   <soapenv:Header>'+chr(13)+chr(10)
cxml += '      <wsse:Security>'+chr(13)+chr(10)
cxml += '         <wsse:UsernameToken>'+chr(13)+chr(10)
cxml += '            <wsse:Username>20600992571MODDATOS</wsse:Username>'+chr(13)+chr(10)
cxml += '            <wsse:Password>moddatos</wsse:Password>'+chr(13)+chr(10)
cxml += '         </wsse:UsernameToken>'+chr(13)+chr(10)
cxml += '      </wsse:Security>'+chr(13)+chr(10)
cxml += '   </soapenv:Header>'+chr(13)+chr(10)
cxml += '   <soapenv:Body>'+chr(13)+chr(10)
cxml += '      <ser:sendBill>'+chr(13)+chr(10)
cxml += '         <fileName>20600992571-01-F001-00000001.zip</fileName>'+chr(13)+chr(10)
cxml += '         <contentFile>cid:20600992571-01-F001-00000001.zip</contentFile>'+chr(13)+chr(10)
cxml += '      </ser:sendBill>'+chr(13)+chr(10)
cxml += '   </soapenv:Body>'+chr(13)+chr(10)
cxml += '</soapenv:Envelope>'+chr(13)+chr(10)

canswer := SunatEnviar(cxml)
canswer := strtran(canswer,"><",">"+chr(10)+"<")
memowrit( "sunat_cpe_respta.txt" , canswer )

RETURN


*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------

FUNC SunatEnviar(cxml)

local endpointUrl,curlHandle,curlErr
local aHeader,cc1

cc1 := ""

endpointUrl := "https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService"

aHeader := {}
AADD(aHeader,'Content-Type: text/xml' )
AADD(aHeader,'Content-Type: text/xml;charset=ISO-8859-1' )
AADD(aHeader,'Content-Length: '+STR(len(cxml)) )
AADD(aHeader,'SOAPAction: sendBill' )

curlHandle := curl_easy_init()

if ! empty(curlHandle)

   /* Specify the Header  data */
   curl_easy_setopt(curlHandle,HB_CURLOPT_HTTPHEADER,aHeader)

   /* Set the endpoint to send the POST to */
   curl_easy_setopt(curlHandle, HB_CURLOPT_URL, endpointUrl)

   /* Setup response data */
   curl_easy_setopt( curlHandle, HB_CURLOPT_DOWNLOAD )
   curl_easy_setopt( curlHandle, HB_CURLOPT_DL_BUFF_SETUP )

   /* Specify the POST data */
   curl_easy_setopt(curlHandle, HB_CURLOPT_POST, 1)
   curl_easy_setopt(curlHandle, HB_CURLOPT_POSTFIELDS, cxml)

   /* Do everything */
   curlErr := curl_easy_perform(curlHandle)

   /* Report any errors */
   if empty(curlErr)
   /* store response in variable */
   cc1 := curl_easy_dl_buff_get( curlHandle )
   else
      msginfo( curl_easy_strerror(curlErr) )
   endif

else

   msginfo( "No handle" )

endif

if ! empty(curlHandle)
   /* Clean-up libcurl */
   curl_global_cleanup( curlHandle )
else
   msginfo( "Error" )
endif

if empty(cc1)
   msginfo( "Error" )
endif

return cc1

Pero al usar cualquiera de las dos opciones me lanza el error:
peer certi.png
peer certi.png (10.76 KiB) Viewed 1813 times
Ya probé instalando varios certificados, 2 de google y uno más de una empresa Llama.pe sin resultado.

Alguién que me eche una mano por fa.


Cordiales saludos.

Eduardo Flores Rivas


LIMA - PERU
User avatar
edufloriv
Posts: 240
Joined: Thu Nov 08, 2012 3:42 am
DBs Used: DBF, MariaDB, MySQL, MSSQL, MariaDB
Location: PERU

Re: Error : peer certificate can not be authenticated...

Post by edufloriv »

Amigos,

Lo solucioné, encontré por alli en este mismo foro otro post. La función me quedó así:

Code: Select all

FUNC SunatEnviar(cxml)

local endpointUrl,curlHandle,curlErr
local aHeader,cc1

cc1 := ""

endpointUrl := "https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService"

aHeader := {}
AADD(aHeader,'Content-Type: text/xml' )
AADD(aHeader,'Content-Type: text/xml;charset=ISO-8859-1' )
AADD(aHeader,'Content-Length: '+STR(len(cxml)) )
AADD(aHeader,'SOAPAction: sendBill' )

curlHandle := curl_easy_init()

if ! empty(curlHandle)

   /* Specify the Header  data */
   curl_easy_setopt(curlHandle,HB_CURLOPT_HTTPHEADER,aHeader)

   /* Set the endpoint to send the POST to */
   curl_easy_setopt(curlHandle, HB_CURLOPT_URL, endpointUrl)

   /* Setup response data */
   curl_easy_setopt( curlHandle, HB_CURLOPT_DOWNLOAD )
   curl_easy_setopt( curlHandle, HB_CURLOPT_DL_BUFF_SETUP )

   /* Specify the POST data */
   curl_easy_setopt(curlHandle, HB_CURLOPT_POST, 1)
   curl_easy_setopt(curlHandle, HB_CURLOPT_POSTFIELDS, cxml)

   curl_easy_setopt(curlHandle, HB_CURLOPT_SSL_VERIFYPEER, 0)  // <-- Agregué esto

   /* Do everything */
   curlErr := curl_easy_perform(curlHandle)

   /* Report any errors */
   if empty(curlErr)
   /* store response in variable */
   cc1 := curl_easy_dl_buff_get( curlHandle )
   else
      msginfo( curl_easy_strerror(curlErr) )
   endif

else

   msginfo( "No handle" )

endif

if ! empty(curlHandle)
   /* Clean-up libcurl */
   curl_global_cleanup( curlHandle )
else
   msginfo( "Error" )
endif

if empty(cc1)
   msginfo( "Error" )
endif

return cc1
Ya no me arroja el error, pero ahora reviso la respuesta del webservice (sunat_cpe_respta.txt) y es el mismo "envelope" que envio.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.sunat.gob.pe">
   <soapenv:Header>
      <wsse:Security>
         <wsse:UsernameToken>
            <wsse:Username>20600992571MODDATOS</wsse:Username>
            <wsse:Password>moddatos</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <ser:sendBill>
         <fileName>20600992571-01-F001-00000001.zip</fileName>
         <contentFile>cid:20600992571-01-F001-00000001.zip</contentFile>
      </ser:sendBill>
   </soapenv:Body>
</soapenv:Envelope>
En la documentación de la SUNAT indica que hay que pasar así los parametros:
sendbill.jpg
sendbill.jpg (198.25 KiB) Viewed 1805 times
¿ Me parece que el problema está en el tag <contentFile>cid:20600992571-01-F001-00000001.zip</contentFile> ? - ¿ Como paso el contenido del archivo .ZIP en el envelope ?


Atentos saludos a todos y gracias anticipadas.

Eduardo Flores Rivas


LIMA - PERU
edk
Posts: 999
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Error : peer certificate can not be authenticated...

Post by edk »

edufloriv wrote: Thu Aug 22, 2019 10:48 pm ¿ Me parece que el problema está en el tag <contentFile>cid:20600992571-01-F001-00000001.zip</contentFile> ? - ¿ Como paso el contenido del archivo .ZIP en el envelope ?
Lo siento, no sé español, uso el traductor de Google.

Por lo que entendí de la documentación, debe codificar el contenido del archivo ZIP en Base64binary.
Bez tytułu.png
Bez tytułu.png (43.39 KiB) Viewed 1770 times
Su código fuente para crear SOAP Envelope debería ser similar a este:

Code: Select all


(...)

cZipFile := ..: Su archivo ZIP, por ejemplo "20600992571-01-F001-00000001.zip" :..
cZipFileContent := FileStr( cZipFile )
cBase64ContentFile := hb_Base64Encode( cZipFileContent, Len( cZipFileContent ) )

(...)

cxml += '   <soapenv:Body>' + CRLF
cxml += '      <ser:sendBill>' + CRLF
cxml += '         <fileName>' + cZipFile + '</fileName>' + CRLF
cxml += '         <contentFile>' + cBase64ContentFile + '</contentFile>' + CRLF
cxml += '      </ser:sendBill>' + CRLF
cxml += '   </soapenv:Body>' + CRLF

(...)
En respuesta, debe recibir SOAP Envelope con la etiqueta <document>, cuyo contenido también se codificará en Base64.
Para decodificar el contenido, use la función StrFile( hb_Base64Decode( Contenido de la etiqueta <document> ), Archivo_de_prueba.zip ).


Al menos así es como entendí la traducción de la documentación. :roll:
User avatar
edufloriv
Posts: 240
Joined: Thu Nov 08, 2012 3:42 am
DBs Used: DBF, MariaDB, MySQL, MSSQL, MariaDB
Location: PERU

Re: Error : peer certificate can not be authenticated...

Post by edufloriv »

Edk,

Muchas gracias amigo, es exactamente lo que necesitaba. :D - No encontraba la función.

Saludos cordiales,


Thank you very much my friend, it is exactly what I needed. : D - I couldn't find the function.

Best regards,

Eduardo Flores Rivas


LIMA - PERU
Post Reply