PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/tpv/wshandler.php

https://github.com/alugo/Goteo
PHP | 226 lines | 118 code | 38 blank | 70 comment | 14 complexity | fd45833d69118cc108917234b670e00b MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. * Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
  4. * This file is part of Goteo.
  5. *
  6. * Goteo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Goteo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
  18. *
  19. */
  20. /****************************************************
  21. wshandler.php
  22. This file contains methods to make calls to bank webservice
  23. Called by /library/tpv.php
  24. ****************************************************/
  25. require_once 'library/paypal/stub.php'; // sí, uso el stub de paypal
  26. require_once 'library/paypal/log.php'; // sí, uso el log de paypal
  27. class WSHandler {
  28. /*
  29. * public variables
  30. */
  31. /*
  32. * Error ID
  33. */
  34. public $error_id = '';
  35. /*
  36. * Error Message
  37. */
  38. public $error_message = '';
  39. /*
  40. * Result FAILURE or SUCCESS
  41. */
  42. public $isSuccess;
  43. /*
  44. * Last Error
  45. */
  46. private $LastError;
  47. /*
  48. * Calls the actual WEB Service and returns the response.
  49. */
  50. function callWebService($data, $url) {
  51. $response = null;
  52. try {
  53. $response = tpvcall($data, $url);
  54. $isFault = false;
  55. if(empty($response) || trim($response) == '')
  56. {
  57. $isFault = true;
  58. $fault = new FaultMessage();
  59. $errorData = new ErrorData();
  60. $errorData->errorId = 'API Error' ;
  61. $errorData->message = 'response is empty.' ;
  62. $fault->error = $errorData;
  63. $this->isSuccess = 'Failure' ;
  64. $this->LastError = $fault;
  65. $response = null;
  66. }
  67. else
  68. {
  69. $isFault = false;
  70. $this->isSuccess = 'Success' ;
  71. if($isFault)
  72. {
  73. $this->isSuccess = 'Failure' ;
  74. $this->LastError = $response ;
  75. $response = null ;
  76. }
  77. }
  78. }
  79. catch(Exception $ex) {
  80. return null;
  81. }
  82. return $response;
  83. }
  84. /*
  85. * Returns Error ID
  86. */
  87. function getErrorId() {
  88. $errorId = '';
  89. if($this->LastError != null) {
  90. if(is_array($this->LastError->error))
  91. {
  92. $errorId = $this->LastError->error[0]->errorId ;
  93. }
  94. else
  95. {
  96. $errorId = $this->LastError->error->errorId ;
  97. }
  98. }
  99. return $errorId ;
  100. }
  101. /*
  102. * Returns Error Message
  103. */
  104. function getErrorMessage() {
  105. $errorMessage = '';
  106. if($this->LastError != null) {
  107. if(is_array($this->LastError->error))
  108. {
  109. $errorMessage = $this->LastError->error[0]->message ;
  110. }
  111. else
  112. {
  113. $errorMessage = $this->LastError->error->message ;
  114. }
  115. }
  116. return $errorMessage ;
  117. }
  118. /*
  119. * Returns Last error
  120. */
  121. public function getLastError()
  122. {
  123. return $this->LastError;
  124. }
  125. /*
  126. * Sets the Last error
  127. */
  128. public function setLastError($error)
  129. {
  130. $this->LastError = $error;
  131. }
  132. }
  133. /**
  134. * call: Function to perform the a call to sermepa webservice
  135. * @methodName is name of API method.
  136. * @a is String
  137. * $serviceName is String
  138. * returns an associtive array containing the response from the server.
  139. */
  140. function tpvcall($data, $endpoint)
  141. {
  142. //setting the curl parameters.
  143. $ch = curl_init();
  144. //For Debugging
  145. // curl_setopt($ch, CURLOPT_HEADER, true);
  146. // curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  147. // curl_setopt($ch, CURLOPT_VERBOSE, true);
  148. curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  149. curl_setopt($ch, CURLOPT_URL,$endpoint);
  150. curl_setopt($ch, CURLOPT_POST, true);
  151. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //no se exactamente para que es, está en los ejemplos
  152. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  153. // tiene que ser application/x-www-form-urlencoded
  154. $the_data = array();
  155. foreach ($data as $key=>$value) {
  156. $the_data[] = $key.'='.$value;
  157. }
  158. curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $the_data)); // datos clave=>valor del POST
  159. //setting the MsgStr as POST FIELD to curl
  160. $conf = array('mode' => 0600, 'timeFormat' => '%X %x');
  161. $logger = &Log::singleton('file', 'logs/'.date('Ymd').'_invest.log', 'caller', $conf);
  162. $logger->log('##### TPV call '.date('d/m/Y').' #####');
  163. $logger->log("endpoint: $endpoint");
  164. $logger->log("request: " . implode(' ', $the_data));
  165. if(isset($_SESSION['curl_error_no'])) {
  166. unset($_SESSION['curl_error_no']);
  167. }
  168. if(isset($_SESSION['curl_error_msg'])) {
  169. unset($_SESSION['curl_error_msg']);
  170. }
  171. //getting response from server
  172. $response = curl_exec($ch);
  173. // curl_getinfo($ch);
  174. $logger->log("response: ".trim(htmlentities($response)));
  175. $logger->log('##### END TPV call '.date('d/m/Y').' #####');
  176. $logger->close();
  177. if (curl_errno($ch)) {
  178. @mail(\GOTEO_MAIL,
  179. 'Ha fallado el handler de tpv ' . SITE_URL,
  180. 'curl_error: ' . curl_errno($ch) . '<br />' . curl_error($ch) . '<hr /><pre>'.print_r($data, 1).'</pre>');
  181. return null;
  182. } else {
  183. //closing the curl
  184. curl_close($ch);
  185. }
  186. return $response;
  187. }
  188. ?>