<?php
function V($StrVar){return ($StrVar==null) ? "" : $StrVar;}
$CMD=V($_GET['CMD']);
//
function CONSULTAR_RUC($RUC){
	$JSON='{"rucEmisor":"10482095521","rucReceptor":"'.$RUC.'","razonSocial":null}';
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "https://dev.invoice2u.pe/apiemisor/invoice2u/emisor/buscar",
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "POST",
	  CURLOPT_POSTFIELDS => $JSON,
	  CURLOPT_HTTPHEADER => array(
		"Authorization: Basic dXN1YXJpbzFAYWd1aWxhci5jb206QWd1aWxhcjA4MTEk",
		"Content-Type: application/json",
		"Postman-Token: 797603fd-1820-4480-8837-7bbc812a1a66",
		"X-Auth-Token: MTA0ODIwOTU1MjE=",
		"cache-control: no-cache"
	  ),
	));	
	$response = curl_exec($curl);
	$err = curl_error($curl);	
	curl_close($curl);	
	if ($err) {
	  return "|MS|".$err;
	} else {
		//
		$response=str_replace("[","",$response);
		$response=str_replace("]","",$response);
		$obj = json_decode($response);
		$_RUC=trim($obj->{'ruc'});
		if(strlen($_RUC)==0){
			return "|MS|Error : RUC/DNI no recupeado|";
		}
		$_NOM=trim($obj->{'razonSocial'});
		//$_NOM="FUERA DE SERVICIO";
		if(strlen($_NOM)==0){
			return "|MS|Error : NOMBRE o RAZON SOCIAL no recupeado|";
		}
		$_EMA=trim($obj->{'correo'});
		$_DIR=trim($obj->{'direccion'});
		$_UBI=trim($obj->{'ubigeo'});
		//
		$_NOM=str_replace("'"," ",$_NOM);
		$_DIR=str_replace("'"," ",$_DIR);
		//
	  return "|OK|".$_RUC."|".$_NOM."|".$_DIR."|".$_UBI."|".$_EMA."|";
	}
}
//
if($CMD=="RUC"){
	$RUC=V($_GET['RUC']);
	echo CONSULTAR_RUC($RUC);
		
}
?>