<?php 
session_start(); 
require_once('../conexion.php');
require_once('../include/sesiones.php');
require_once('../php_api/api.php');

$api_tipo_doc=$_POST["api_tipo_doc"]; 
$api_documento=$_POST["api_documento"]; 
$api_dv=$_POST["api_dv"]; 
$api_nombre=$_POST["api_nombre"]; 
$api_telefono=$_POST["api_telefono"]; 
$api_direccion=$_POST["api_direccion"]; 
$api_ciudad=$_POST["api_ciudad"]; 
$api_correo=$_POST["api_correo"]; 
$api_registro=$_POST["api_registro"]; 
$api_contribuyente=$_POST["api_contribuyente"]; 
$api_regimen=$_POST["api_regimen"]; 
$api_liability=$_POST["api_liability"]; 
$correo_envio=$_POST["correo_envio"]; 
$clave_envio=$_POST["clave_envio"]; 

/*"imap_server": "imap.gmail.com",
"imap_user": "alexanderobandolondono@gmail.com",
"imap_password": "sstghufkwdatgjnsnqthxw",
"imap_encryption": "ssl",
"imap_port": "993"*/

$json = array(
    "type_document_identification_id" => $api_tipo_doc,
    "type_organization_id" => $api_contribuyente,
    "type_regime_id" => $api_regimen,
    "type_liability_id" =>$api_liability,
    "business_name" => $api_nombre,
    "merchant_registration" => $api_registro,
    "municipality_id" => $api_ciudad,
    "address" => $api_direccion,
    "phone" => $api_telefono,
    "email" => $api_correo,
    "mail_host"=> "smtp.gmail.com",
    "mail_port"=> "587",
    "mail_username"=> $correo_envio,
    "mail_password"=> $clave_envio,
    "mail_encryption"=> "tls",
    "mail_from_address"=> $correo_envio,
    "mail_from_name"=> "API DE FACTURACION ELECTRONICA"
);


$curl = curl_init();
  curl_setopt_array($curl, array(
  CURLOPT_URL => "{$configuration->base_url}ubl2.1/config/".$api_documento."/".$api_dv,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>json_encode($json),

  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Accept: application/json',
    'Authorization: ••••••'
  ),
));

$response = curl_exec($curl);
curl_close($curl);
$book=json_decode($response, true);
$message=$book['message'];
echo '<div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>'.$message.'</strong> 
    </div></br>'; 


if($message='Empresa creada')
{
    $token=$book['token'];
    $sql=mysqli_query($con,"UPDATE configuracion_electronica Set ele_token ='$token' Where empresa='$empresa'") or die(mysqli_error($con)); 
}
?>