/api/contrib/procesos_optimizados/Enviar_XML_Hacienda/envio_hacienda.php

https://gitlab.com/D-apos-software/api-hacienda · PHP · 266 lines · 105 code · 30 blank · 131 comment · 10 complexity · 76c7a6e14102c0e42d6803189becfcb3 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (C) 2017-2020 CRLibre <https://crlibre.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as published
  7. * by the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. function send($Token_session,$XmlClave,$R_XmlFirmado)
  19. {
  20. $datos = null;
  21. $apiTo = params_get("client_id");
  22. $url = ($apiTo == 'api-stag' ? "https://api.comprobanteselectronicos.go.cr/recepcion-sandbox/v1/recepcion/" : ($apiTo == 'api-prod' ? "https://api.comprobanteselectronicos.go.cr/recepcion/v1/recepcion/" : null));
  23. if (params_get("receptor_tipo_identif") == "" or params_get("receptor_num_identif") == "")
  24. {
  25. if (params_get('callbackUrl') == "")
  26. {
  27. $datos = array(
  28. 'clave' => $XmlClave, //params_get('clave'),
  29. 'fecha' => params_get("fecha_emision"),
  30. 'emisor' => array(
  31. 'tipoIdentificacion' => params_get("emisor_tipo_identif"),
  32. 'numeroIdentificacion' => params_get("emisor_num_identif")
  33. ),
  34. 'comprobanteXml' => $R_XmlFirmado //params_get("comprobanteXml")
  35. );
  36. }
  37. else
  38. {
  39. $datos = array(
  40. 'clave' => $XmlClave, //params_get('clave'),
  41. 'fecha' => params_get("fecha_emision"),
  42. 'emisor' => array(
  43. 'tipoIdentificacion' => params_get("emisor_tipo_identif"),
  44. 'numeroIdentificacion' => params_get("emisor_num_identif")
  45. ),
  46. 'comprobanteXml' => $R_XmlFirmado, //params_get("comprobanteXml"),
  47. 'callbackUrl' => params_get('callbackUrl')
  48. );
  49. }
  50. }
  51. else
  52. {
  53. if (params_get('callbackUrl') == "")
  54. {
  55. $datos = array(
  56. 'clave' => $XmlClave, //params_get('clave'),
  57. 'fecha' => params_get("fecha_emision"),
  58. 'emisor' => array(
  59. 'tipoIdentificacion' => params_get("emisor_tipo_identif"),
  60. 'numeroIdentificacion' => params_get("emisor_num_identif")
  61. ),
  62. 'receptor' => array(
  63. 'tipoIdentificacion' => params_get("receptor_tipo_identif"),
  64. 'numeroIdentificacion' => params_get("receptor_num_identif")
  65. ),
  66. 'comprobanteXml' => $R_XmlFirmado //params_get("comprobanteXml")
  67. );
  68. } else {
  69. $datos = array(
  70. 'clave' => $XmlClave, //params_get('clave'),
  71. 'fecha' => params_get("fecha_emision"),
  72. 'emisor' => array(
  73. 'tipoIdentificacion' => params_get("emisor_tipo_identif"),
  74. 'numeroIdentificacion' => params_get("emi_numeroIdentificacion")
  75. ),
  76. 'receptor' => array(
  77. 'tipoIdentificacion' => params_get("receptor_tipo_identif"),
  78. 'numeroIdentificacion' => params_get("receptor_num_identif")
  79. ),
  80. 'comprobanteXml' => $R_XmlFirmado, //params_get("comprobanteXml"),
  81. 'callbackUrl' => params_get('callbackUrl')
  82. );
  83. }
  84. }
  85. //$datosJ= http_build_query($datos);
  86. $mensaje = json_encode($datos);
  87. grace_debug("JSON:" . $mensaje);
  88. $header = array(
  89. 'Authorization: bearer ' . $Token_session, //params_get('token'),
  90. 'Content-Type: application/json'
  91. );
  92. $curl = curl_init($url);
  93. curl_setopt($curl, CURLOPT_HEADER, true);
  94. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  95. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  96. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  97. curl_setopt($curl, CURLOPT_POSTFIELDS, $mensaje);
  98. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  99. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  100. $respuesta = curl_exec($curl);
  101. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  102. $err = curl_error($curl);
  103. curl_close($curl);
  104. if ($err)
  105. {
  106. $arrayResp = array(
  107. "Status" => $status,
  108. "to" => $apiTo,
  109. "text" => $err
  110. );
  111. return $arrayResp;
  112. }
  113. else
  114. {
  115. $arrayResp = array(
  116. "Status" => $status,
  117. "text" => explode("\n", $respuesta)
  118. );
  119. return $arrayResp;
  120. }
  121. }
  122. /* function sendMensaje()
  123. {
  124. $apiTo = params_get("client_id");
  125. $url = ($apiTo == 'api-stag' ? "https://api.comprobanteselectronicos.go.cr/recepcion-sandbox/v1/recepcion/" : ($apiTo == 'api-prod' ? "https://api.comprobanteselectronicos.go.cr/recepcion/v1/recepcion/" : null));
  126. $datos = array(
  127. 'clave' => params_get('clave'),
  128. 'fecha' => params_get("fecha"),
  129. 'emisor' => array(
  130. 'tipoIdentificacion' => params_get("emi_tipoIdentificacion"),
  131. 'numeroIdentificacion' => params_get("emi_numeroIdentificacion")
  132. ),
  133. 'receptor' => array(
  134. 'tipoIdentificacion' => params_get("receptor_tipo_identif"),
  135. 'numeroIdentificacion' => params_get("receptor_num_identif")
  136. ),
  137. 'consecutivoReceptor' => str_pad(params_get("consecutivoReceptor"), 20, "0", STR_PAD_LEFT),
  138. 'comprobanteXml' => params_get("comprobanteXml")
  139. );
  140. //$datosJ= http_build_query($datos);
  141. $mensaje = json_encode($datos);
  142. $header = array(
  143. 'Authorization: bearer ' . params_get('token'),
  144. 'Content-Type: application/json'
  145. );
  146. $curl = curl_init($url);
  147. curl_setopt($curl, CURLOPT_HEADER, true);
  148. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  149. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  150. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  151. curl_setopt($curl, CURLOPT_POSTFIELDS, $mensaje);
  152. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  153. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  154. $respuesta = curl_exec($curl);
  155. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  156. $err = curl_error($curl);
  157. curl_close($curl);
  158. if ($err)
  159. {
  160. $arrayResp = array(
  161. "Status" => $status,
  162. "to" => $apiTo,
  163. "text" => $err
  164. );
  165. return $arrayResp;
  166. }
  167. else
  168. {
  169. $arrayResp = array(
  170. "Status" => $status,
  171. "text" => explode("\n", $respuesta)
  172. );
  173. return $arrayResp;
  174. }
  175. }
  176. function sendTE()
  177. {
  178. $apiTo = params_get("client_id");
  179. $url = ($apiTo == 'api-stag' ? "https://api.comprobanteselectronicos.go.cr/recepcion-sandbox/v1/recepcion/" : ($apiTo == 'api-prod' ? "https://api.comprobanteselectronicos.go.cr/recepcion/v1/recepcion/" : null));
  180. $datos = array(
  181. 'clave' => params_get('clave'),
  182. 'fecha' => params_get("fecha"),
  183. 'emisor' => array(
  184. 'tipoIdentificacion' => params_get("emi_tipoIdentificacion"),
  185. 'numeroIdentificacion' => params_get("emi_numeroIdentificacion")
  186. ),
  187. 'comprobanteXml' => params_get("comprobanteXml")
  188. );
  189. //$datosJ= http_build_query($datos);
  190. $mensaje = json_encode($datos);
  191. $header = array(
  192. 'Authorization: bearer ' . params_get('token'),
  193. 'Content-Type: application/json'
  194. );
  195. $curl = curl_init($url);
  196. curl_setopt($curl, CURLOPT_HEADER, true);
  197. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  198. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  199. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  200. curl_setopt($curl, CURLOPT_POSTFIELDS, $mensaje);
  201. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  202. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  203. $respuesta = curl_exec($curl);
  204. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  205. $err = curl_error($curl);
  206. curl_close($curl);
  207. if ($err)
  208. {
  209. $arrayResp = array(
  210. "Status" => $status,
  211. "to" => $apiTo,
  212. "text" => $err
  213. );
  214. return $arrayResp;
  215. }
  216. else
  217. {
  218. $arrayResp = array(
  219. "Status" => $status,
  220. "text" => explode("\n", $respuesta)
  221. );
  222. return $arrayResp;
  223. }
  224. } */
  225. ?>