como implementar el nuevo servicio SOAP de la API de AFIP en PHP
?
Está pregunta nace ya que se que a muchos les ha ocurrido lo mismo y espero que logremos llegar a una solución.
Hasta hace nada mas que una semana venia utilizando la api rest de AFIP la cual consultaba pasandole un parametro cuit
por medio de la url
y recibia un Json
de los datos.
Sin embargo, por alguna razón decidieron darla de baja, dejando todo mi sistema obsoleto. Y ademas la inexistente documentacion que encontré se encuentra en python
Ahora debo ingeniarmelas para hacer un refactor de mi app actualizando el metodo con el que obtengo los datos y usando la nueva version de la API
en SOAP
API REST:
"https://soa.afip.gov.ar/sr-padron/v2/persona/30500010912"
API SOAP:
"https://awshomo.afip.gov.ar/sr-padron/webservices/personaServiceA10"
Lamentablemente mi conocimiento del WS
SOAP es casi nulo. Por eso estoy preguntando por este medio.
Segun este manual (a mi parecer inentendible):
"https://www.afip.gob.ar/ws/ws_sr_padron_a10/manual_ws_sr_padron_a10_v1.1.pdf"
Debo usar el metodo getPersona()
Aqui el codigo de ejemplo que me brinda la pagina oficial:
!/usr/bin/php
<?php
# Author: Gerardo Fisanotti - DvSHyS/DiOPIN/AFIP - 13-apr-07
# Function: Get an authorization ticket (TA) from AFIP WSAA
# Input:
# WSDL, CERT, PRIVATEKEY, PASSPHRASE, SERVICE, URL
# Check below for its definitions
# Output:
# TA.xml: the authorization ticket as granted by WSAA.
#==============================================================================
define ("WSDL", "wsaa.wsdl"); # The WSDL corresponding to WSAA
define ("CERT", "ghf.crt"); # The X.509 certificate in PEM format
define ("PRIVATEKEY", "ghf.key"); # The private key correspoding to CERT (PEM)
define ("PASSPHRASE", "xxxxx"); # The passphrase (if any) to sign
define ("PROXY_HOST", "10.20.152.112"); # Proxy IP, to reach the Internet
define ("PROXY_PORT", "80"); # Proxy TCP port
define ("URL", "https://wsaahomo.afip.gov.ar/ws/services/LoginCms");
#define ("URL", "https://wsaa.afip.gov.ar/ws/services/LoginCms");
#------------------------------------------------------------------------------
# You shouldn't have to change anything below this line!!!
#==============================================================================
function CreateTRA($SERVICE)
{
$TRA = new SimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?>' .
'<loginTicketRequest version="1.0">'.
'</loginTicketRequest>');
$TRA->addChild('header');
$TRA->header->addChild('uniqueId',date('U'));
$TRA->header->addChild('generationTime',date('c',date('U')-60));
$TRA->header->addChild('expirationTime',date('c',date('U')+60));
$TRA->addChild('service',$SERVICE);
$TRA->asXML('TRA.xml');
}
#==============================================================================
# This functions makes the PKCS#7 signature using TRA as input file, CERT and
# PRIVATEKEY to sign. Generates an intermediate file and finally trims the
# MIME heading leaving the final CMS required by WSAA.
function SignTRA()
{
$STATUS=openssl_pkcs7_sign("TRA.xml", "TRA.tmp", "file://".CERT,
array("file://".PRIVATEKEY, PASSPHRASE),
array(),
!PKCS7_DETACHED
);
if (!$STATUS) {exit("ERROR generating PKCS#7 signature\n");}
$inf=fopen("TRA.tmp", "r");
$i=0;
$CMS="";
while (!feof($inf))
{
$buffer=fgets($inf);
if ( $i++ >= 4 ) {$CMS.=$buffer;}
}
fclose($inf);
# unlink("TRA.xml");
unlink("TRA.tmp");
return $CMS;
}
#==============================================================================
function CallWSAA($CMS)
{
$client=new SoapClient(WSDL, array(
'proxy_host' => PROXY_HOST,
'proxy_port' => PROXY_PORT,
'soap_version' => SOAP_1_2,
'location' => URL,
'trace' => 1,
'exceptions' => 0
));
$results=$client->loginCms(array('in0'=>$CMS));
file_put_contents("request-loginCms.xml",$client->__getLastRequest());
file_put_contents("response-loginCms.xml",$client->__getLastResponse());
if (is_soap_fault($results))
{exit("SOAP Fault: ".$results->faultcode."\n".$results->faultstring."\n");}
return $results->loginCmsReturn;
}
#==============================================================================
function ShowUsage($MyPath)
{
printf("Uso : %s Arg#1 Arg#2\n", $MyPath);
printf("donde: Arg#1 debe ser el service name del WS de negocio.\n");
printf(" Ej.: %s wsfe\n", $MyPath);
}
#==============================================================================
ini_set("soap.wsdl_cache_enabled", "0");
if (!file_exists(CERT)) {exit("Failed to open ".CERT."\n");}
if (!file_exists(PRIVATEKEY)) {exit("Failed to open ".PRIVATEKEY."\n");}
if (!file_exists(WSDL)) {exit("Failed to open ".WSDL."\n");}
if ( $argc < 2 ) {ShowUsage($argv[0]); exit();}
$SERVICE=$argv[1];
CreateTRA($SERVICE);
$CMS=SignTRA();
$TA=CallWSAA($CMS);
if (!file_put_contents("TA.xml", $TA)) {exit();}
?>
https://www.sistemasagiles.com.ar/padron/consulta/persona/:dni/
Chequea tambien [groups.google.com/forum/#!msg/pyafipws/S65ziE5-_5k/…