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

/library/tpv.php

https://github.com/alugo/Goteo
PHP | 117 lines | 66 code | 22 blank | 29 comment | 3 complexity | 1629a61cc6e60c7ab263caaae9af2e26 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. namespace Goteo\Library {
  21. use Goteo\Model\Invest,
  22. Goteo\Model\Project,
  23. Goteo\Core\Redirection;
  24. // this library depends on bank system. Contact us for development services or make it work somehow.
  25. /*
  26. * Clase para usar la pasarela de pago
  27. */
  28. class Tpv {
  29. /*
  30. * para ceca no hay preapproval, es el cargo directamente
  31. */
  32. public static function preapproval($invest, &$errors = array()) {
  33. return static::pay($invest, $errors);
  34. }
  35. public static function pay($invest, &$errors = array()) {
  36. if (\GOTEO_FREE) {
  37. $errors[] = 'Bank not implemented. Contact us for development services or make it work somehow';
  38. return false;
  39. }
  40. try {
  41. $project = Project::getMini($invest->project);
  42. // preparo codigo y cantidad
  43. $token = $invest->id . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9);
  44. $amount = $invest->amount * 100;
  45. // Guardar el codigo de preaproval en el registro de aporte (para confirmar o cancelar)
  46. $invest->setPreapproval($token);
  47. // mandarlo al tpv
  48. $urlTPV = TPV_REDIRECT_URL;
  49. $data = '';
  50. $MsgStr = '';
  51. foreach ($datos as $n => $v) {
  52. $data .= '<input name="'.$n.'" type="hidden" value="'.$v.'" />';
  53. $MsgStr .= "{$n}:'{$v}'; ";
  54. }
  55. $conf = array('mode' => 0600, 'timeFormat' => '%X %x');
  56. $logger = &\Log::singleton('file', 'logs/'.date('Ymd').'_invest.log', 'caller', $conf);
  57. $logger->log('##### TPV ['.$invest->id.'] '.date('d/m/Y').' User:'.$_SESSION['user']->id.'#####');
  58. $logger->log("Charge request: $MsgStr");
  59. $logger->close();
  60. Invest::setDetail($invest->id, 'tpv-conection', 'Ha iniciado la comunicacion con el tpv, operacion numero ' . $token . '. Proceso libary/tpv::pay');
  61. echo '<html><head><title>Goteo.org</title></head><body><form action="'.$urlTPV.'" method="post" id="form_tpv" enctype="application/x-www-form-urlencoded">'.$data.'</form><script type="text/javascript">document.getElementById("form_tpv").submit();</script></body></html>';
  62. return true;
  63. }
  64. catch(Exception $ex) {
  65. Invest::setDetail($invest->id, 'tpv-conection-fail', 'Ha fallado la comunicacion con el tpv. Proceso libary/tpv::pay');
  66. $errors[] = 'Error fatal en la comunicacion con el TPV, se ha reportado la incidencia. Disculpe las molestias.';
  67. @mail(\GOTEO_FAIL_MAIL, 'Error fatal en comunicacion TPV Sermepa', 'ERROR en ' . __FUNCTION__ . '<br />' . $ex->getMessage());
  68. return false;
  69. }
  70. }
  71. public static function cancelPreapproval ($invest, &$errors = array(), $fail = false) {
  72. return static::cancelPay($invest, $errors, $fail);
  73. }
  74. public static function cancelPay($invest, &$errors = array(), $fail = false) {
  75. if (\GOTEO_FREE) {
  76. $errors[] = 'Bank not implemented. Contact us for development services or make it work somehow';
  77. return false;
  78. }
  79. try {
  80. if (empty($invest->payment)) {
  81. $invest->cancel($fail);
  82. return true;
  83. }
  84. //echo \trace($datos);
  85. return false;
  86. }
  87. catch(Exception $ex) {
  88. Invest::setDetail($invest->id, 'tpv-cancel-conection-fail', 'Ha fallado la comunicacion con el tpv al anular la operacion. Proceso libary/tpv::cancelPay');
  89. $errors[] = 'Error fatal en la comunicacič´¸n con el TPV, se ha reportado la incidencia. Disculpe las molestias.';
  90. @mail(\GOTEO_FAIL_MAIL, 'Error fatal en comunicacion TPV Sermepa', 'ERROR en ' . __FUNCTION__ . '<br /><pre>' . print_r($handler, 1) . '</pre>');
  91. return false;
  92. }
  93. }
  94. }
  95. }