| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program 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 General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | /* sort en affichant une erreur */ |
| 23 | function cb_erreur($text) { |
| 24 | global $globals; |
| 25 | $mymail = new PlMailer(); |
| 26 | $mymail->addTo($globals->money->email); |
| 27 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
| 28 | $mymail->setSubject("erreur lors d'un télépaiement (CyberPaiement)"); |
| 29 | $mymail->setTxtBody("\n\n".var_export($_REQUEST,true)); |
| 30 | $mymail->send(); |
| 31 | exit; |
| 32 | } |
| 33 | |
| 34 | /* sort en affichant une erreur */ |
| 35 | function paypal_erreur($text, $send=true) |
| 36 | { |
| 37 | global $erreur, $globals; |
| 38 | if ($erreur) return; |
| 39 | $erreur = $text; |
| 40 | if (!$send) return; |
| 41 | |
| 42 | $mymail = new PlMailer(); |
| 43 | $mymail->addTo($globals->money->email); |
| 44 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
| 45 | $mymail->setSubject("erreur lors d'un télépaiement (PayPal)"); |
| 46 | $mymail->setTxtBody("\n\n".var_export($_REQUEST,true)); |
| 47 | $mymail->send(); |
| 48 | |
| 49 | Platal::page()->trigError($text); |
| 50 | } |
| 51 | |
| 52 | /* http://fr.wikipedia.org/wiki/Formule_de_Luhn */ |
| 53 | function luhn($nombre) { |
| 54 | $s = strrev($nombre); |
| 55 | $sum = 0; |
| 56 | for ($i = 0; $i < strlen($s); $i++) { |
| 57 | $dgt = $s{$i}; |
| 58 | $sum += ($i % 2) ? (2*$dgt) % 9 : $dgt; |
| 59 | } |
| 60 | return $sum % 10; |
| 61 | } |
| 62 | |
| 63 | /* calcule la clé d'acceptation a partir de 5 champs */ |
| 64 | function cle_accept($d1,$d2,$d3,$d4,$d5) |
| 65 | { |
| 66 | $m1 = luhn($d1.$d5); |
| 67 | $m2 = luhn($d2.$d5); |
| 68 | $m3 = luhn($d3.$d5); |
| 69 | $m4 = luhn($d4.$d5); |
| 70 | $n = $m1 + $m2 + $m3 + $m4; |
| 71 | $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 72 | return $alpha{$n-1}.$m1.$m2.$m3.$m4; |
| 73 | } |
| 74 | |
| 75 | /* decode the comment */ |
| 76 | function comment_decode($comment) { |
| 77 | $comment = urldecode($comment); |
| 78 | if (is_utf8($comment)) { |
| 79 | return $comment; |
| 80 | } else { |
| 81 | return utf8_encode($comment); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | class PaymentModule extends PLModule |
| 87 | { |
| 88 | function handlers() |
| 89 | { |
| 90 | return array( |
| 91 | 'payment' => $this->make_hook('payment', AUTH_MDP), |
| 92 | 'payment/cyber_return' => $this->make_hook('cyber_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
| 93 | 'payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
| 94 | '%grp/paiement' => $this->make_hook('xnet_payment', AUTH_MDP), |
| 95 | '%grp/payment' => $this->make_hook('xnet_payment', AUTH_MDP), |
| 96 | '%grp/payment/cyber_return' => $this->make_hook('cyber_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
| 97 | '%grp/payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC, 'user', NO_HTTPS), |
| 98 | 'admin/payments' => $this->make_hook('admin', AUTH_MDP, 'admin'), |
| 99 | |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | function handler_payment(&$page, $ref = -1) |
| 104 | { |
| 105 | global $globals; |
| 106 | |
| 107 | require_once 'profil.func.inc.php' ; |
| 108 | $this->load('money.inc.php'); |
| 109 | |
| 110 | if (!empty($GLOBALS['IS_XNET_SITE'])) { |
| 111 | if (!$globals->asso('id')) { |
| 112 | return PL_NOT_FOUND; |
| 113 | } |
| 114 | $res = XDB::query("SELECT asso_id |
| 115 | FROM payments |
| 116 | WHERE asso_id = {?} AND id = {?}", |
| 117 | $globals->asso('id'), $ref); |
| 118 | if (!$res->numRows()) { |
| 119 | return PL_FORBIDDEN; |
| 120 | } |
| 121 | } |
| 122 | $page->changeTpl('payment/index.tpl'); |
| 123 | $page->setTitle('Télépaiements'); |
| 124 | |
| 125 | // initialisation |
| 126 | $op = Env::v('op', 'select'); |
| 127 | $meth = new PayMethod(Env::i('methode', -1)); |
| 128 | $pay = new Payment($ref); |
| 129 | |
| 130 | if($pay->flags->hasflag('old')){ |
| 131 | $page->trigError("La transaction selectionnée est périmée."); |
| 132 | $pay = new Payment(); |
| 133 | } |
| 134 | $val = Env::v('montant') != 0 ? Env::v('montant') : $pay->montant_def; |
| 135 | |
| 136 | if (($e = $pay->check($val)) !== true) { |
| 137 | $page->trigError($e); |
| 138 | } |
| 139 | |
| 140 | if ($op=='submit') { |
| 141 | $pay->init($val, $meth); |
| 142 | $pay->prepareform($pay); |
| 143 | } else { |
| 144 | $res = XDB::iterator("SELECT timestamp, amount |
| 145 | FROM payment_transactions |
| 146 | WHERE uid = {?} AND ref = {?} |
| 147 | ORDER BY timestamp DESC", |
| 148 | S::v('uid', -1), $ref); |
| 149 | |
| 150 | if ($res->total()) $page->assign('transactions', $res); |
| 151 | } |
| 152 | |
| 153 | $val = floor($val).".".substr(floor(($val - floor($val))*100+100),1); |
| 154 | $page->assign('montant',$val); |
| 155 | $page->assign('comment',Env::v('comment')); |
| 156 | |
| 157 | $page->assign('meth', $meth); |
| 158 | $page->assign('pay', $pay); |
| 159 | $page->assign('evtlink', $pay->event()); |
| 160 | |
| 161 | $page->assign('prefix', $globals->money->mpay_tprefix); |
| 162 | } |
| 163 | |
| 164 | function handler_cyber_return(&$page, $uid = null) |
| 165 | { |
| 166 | /* reference banque (numero de transaction) */ |
| 167 | $champ901 = Env::s('CHAMP901'); |
| 168 | /* cle d'acceptation */ |
| 169 | $champ905 = Env::s('CHAMP905'); |
| 170 | /* code retour */ |
| 171 | $champ906 = Env::s('CHAMP906'); |
| 172 | /* email renvoye par la banque */ |
| 173 | $champ104 = Env::s('CHAMP104'); |
| 174 | /* reference complete de la commande */ |
| 175 | $champ200 = Env::s('CHAMP200'); |
| 176 | /* montant de la transaction */ |
| 177 | $champ201 = Env::s('CHAMP201'); |
| 178 | /* devise */ |
| 179 | $champ202 = Env::s('CHAMP202'); |
| 180 | $montant = "$champ201 $champ202"; |
| 181 | |
| 182 | /* on extrait les informations sur l'utilisateur */ |
| 183 | $user = User::get($uid); |
| 184 | if (!$user) { |
| 185 | cb_erreur("uid invalide"); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* on extrait la reference de la commande */ |
| 190 | if (!ereg('-xorg-([0-9]+)$', $champ200, $matches)) { |
| 191 | cb_erreur("référence de commande invalide"); |
| 192 | } |
| 193 | |
| 194 | echo ($ref = $matches[1]); |
| 195 | $res = XDB::query("SELECT mail, text, confirmation |
| 196 | FROM payments |
| 197 | WHERE id={?}", $ref); |
| 198 | if (!list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow()) { |
| 199 | cb_erreur("référence de commande inconnue"); |
| 200 | } |
| 201 | |
| 202 | /* on extrait le code de retour */ |
| 203 | if ($champ906 != "0000") { |
| 204 | $res = XDB::query('SELECT rcb.text, c.id, c.text |
| 205 | FROM payment_codeRCB AS rcb |
| 206 | LEFT JOIN payment_codeC AS c ON (rcb.codeC = c.id) |
| 207 | WHERE rcb.id = {?}', $champ906); |
| 208 | if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) { |
| 209 | cb_erreur("erreur lors du paiement : $c_text ($c_id)"); |
| 210 | } else{ |
| 211 | cb_erreur("erreur inconnue lors du paiement"); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* on fait l'insertion en base de donnees */ |
| 216 | XDB::execute("INSERT INTO payment_transactions (id, uid, ref, fullref, amount, pkey, comment) |
| 217 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})", |
| 218 | $champ901, $user->id(), $ref, $champ200, $montant, $champ905, Env::v('comment')); |
| 219 | |
| 220 | // We check if it is an Xnet payment and then update the related ML. |
| 221 | $res = XDB::query('SELECT eid |
| 222 | FROM group_events |
| 223 | WHERE paiement_id = {?}', $ref); |
| 224 | if ($eid = $res->fetchOneCell()) { |
| 225 | require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php'; |
| 226 | $evt = get_event_detail($eid); |
| 227 | subscribe_lists_event(0, $uid, $evt, $montant, true); |
| 228 | } |
| 229 | |
| 230 | /* on genere le mail de confirmation */ |
| 231 | $conf_text = str_replace( |
| 232 | array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>', 'comment>'), |
| 233 | array($user->firstName(), $user->lastName(), $user->promo(), $montant, |
| 234 | $user->isFemale() ? 'Chère' : 'Cher', $user->isFemale() ? 'Chère' : 'Cher', |
| 235 | Env::v('comment')), $conf_text); |
| 236 | |
| 237 | global $globals; |
| 238 | $mymail = new PlMailer(); |
| 239 | $mymail->setFrom($conf_mail); |
| 240 | $mymail->addCc($conf_mail); |
| 241 | $mymail->setSubject($conf_title); |
| 242 | $mymail->setWikiBody($conf_text); |
| 243 | $mymail->sendTo($user); |
| 244 | |
| 245 | /* on envoie les details de la transaction à telepaiement@ */ |
| 246 | $mymail = new PlMailer(); |
| 247 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
| 248 | $mymail->addTo($globals->money->email); |
| 249 | $mymail->setSubject($conf_title); |
| 250 | $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" . |
| 251 | 'mail : ' . $user->forlifeEmail() . "\n\n" . |
| 252 | "paiement : $conf_title ($conf_mail)\n". |
| 253 | "reference : $champ200\n". |
| 254 | "montant : $montant\n\n". |
| 255 | "dump de REQUEST:\n". |
| 256 | var_export($_REQUEST,true); |
| 257 | $mymail->setTxtBody($msg); |
| 258 | $mymail->send(); |
| 259 | exit; |
| 260 | } |
| 261 | |
| 262 | function handler_paypal_return(&$page, $uid = null) |
| 263 | { |
| 264 | $page->changeTpl('payment/retour_paypal.tpl'); |
| 265 | |
| 266 | /* reference banque (numero de transaction) */ |
| 267 | $no_transaction = Env::s('tx'); |
| 268 | /* token a renvoyer pour avoir plus d'information */ |
| 269 | $clef = Env::s('sig'); |
| 270 | /* code retour */ |
| 271 | $status = Env::s('st'); |
| 272 | /* raison */ |
| 273 | $reason = ($status == 'Pending')? Env::s('pending_reason'): Env::s('reason_code'); |
| 274 | /* reference complete de la commande */ |
| 275 | $fullref = Env::s('cm'); |
| 276 | /* montant de la transaction */ |
| 277 | $montant_nb = Env::s('amt'); |
| 278 | /* devise */ |
| 279 | $montant_dev = Env::s('cc'); |
| 280 | $montant = "$montant_nb $montant_dev"; |
| 281 | |
| 282 | /* on extrait le code de retour */ |
| 283 | if ($status != "Completed") { |
| 284 | if ($status) |
| 285 | paypal_erreur("erreur lors du paiement : $status - $reason"); |
| 286 | else |
| 287 | paypal_erreur("Paiement annulé", false); |
| 288 | } |
| 289 | |
| 290 | /* on extrait les informations sur l'utilisateur */ |
| 291 | $user = User::get($uid); |
| 292 | if (!$user) { |
| 293 | paypal_erreur("uid invalide"); |
| 294 | } |
| 295 | |
| 296 | /* on extrait la reference de la commande */ |
| 297 | if (!ereg('-xorg-([0-9]+)$', $fullref, $matches)) { |
| 298 | paypal_erreur("référence de commande invalide"); |
| 299 | } |
| 300 | |
| 301 | $ref = $matches[1]; |
| 302 | $res = XDB::query("SELECT mail, text, confirmation |
| 303 | FROM payments |
| 304 | WHERE id = {?}", $ref); |
| 305 | if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) { |
| 306 | paypal_erreur("référence de commande inconnue"); |
| 307 | } |
| 308 | |
| 309 | /* on fait l'insertion en base de donnees */ |
| 310 | XDB::execute("INSERT INTO payment_transactions (id, uid, ref, fullref, amount, pkey, comment) |
| 311 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})", |
| 312 | $no_transaction, $user->id(), $ref, $fullref, $montant, $clef, Env::v('comment')); |
| 313 | |
| 314 | // We check if it is an Xnet payment and then update the related ML. |
| 315 | $res = XDB::query('SELECT eid |
| 316 | FROM group_events |
| 317 | WHERE paiement_id = {?}', $ref); |
| 318 | if ($eid = $res->fetchOneCell()) { |
| 319 | require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php'; |
| 320 | $evt = get_event_detail($eid); |
| 321 | subscribe_lists_event(0, $uid, $evt, $montant, true); |
| 322 | } |
| 323 | |
| 324 | /* on genere le mail de confirmation */ |
| 325 | $conf_text = str_replace(array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>'), |
| 326 | array($user->firstName(), $user->lastName(), $user->promo(), $montant, |
| 327 | $user->isFemale() ? 'Chère' : 'Cher', |
| 328 | $user->isFemale() ? 'Chère' : 'Cher'), $conf_text); |
| 329 | |
| 330 | global $globals; |
| 331 | $mymail = new PlMailer(); |
| 332 | $mymail->setFrom($conf_mail); |
| 333 | $mymail->addCc($conf_mail); |
| 334 | $mymail->setSubject($conf_title); |
| 335 | $mymail->setWikiBody($conf_text); |
| 336 | $mymail->sendTo($user); |
| 337 | |
| 338 | /* on envoie les details de la transaction à telepaiement@ */ |
| 339 | $mymail = new PlMailer(); |
| 340 | $mymail->setFrom("webmaster@" . $globals->mail->domain); |
| 341 | $mymail->addTo($globals->money->email); |
| 342 | $mymail->setSubject($conf_title); |
| 343 | $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" . |
| 344 | 'mail : ' . $user->forlifeEmail() . "\n\n" . |
| 345 | "paiement : $conf_title ($conf_mail)\n". |
| 346 | "reference : $champ200\n". |
| 347 | "montant : $montant\n\n". |
| 348 | "dump de REQUEST:\n". |
| 349 | var_export($_REQUEST,true); |
| 350 | $mymail->setTxtBody($msg); |
| 351 | $mymail->send(); |
| 352 | |
| 353 | $page->assign('texte', $conf_text); |
| 354 | $page->assign('erreur', $erreur); |
| 355 | } |
| 356 | |
| 357 | function handler_xnet_payment(&$page, $pid = null) |
| 358 | { |
| 359 | global $globals; |
| 360 | |
| 361 | $perms = S::v('perms'); |
| 362 | if (!$perms->hasFlag('groupmember')) { |
| 363 | if (is_null($pid)) { |
| 364 | return PL_FORBIDDEN; |
| 365 | } |
| 366 | $res = XDB::query("SELECT 1 |
| 367 | FROM group_events AS e |
| 368 | INNER JOIN group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?}) |
| 369 | WHERE e.paiement_id = {?} AND e.asso_id = {?}", |
| 370 | S::i('uid'), $pid, $globals->asso('id')); |
| 371 | if ($res->numRows() == 0) { |
| 372 | return PL_FORBIDDEN; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (!is_null($pid)) { |
| 377 | return $this->handler_payment($page, $pid); |
| 378 | } |
| 379 | $page->changeTpl('payment/xnet.tpl'); |
| 380 | |
| 381 | $res = XDB::query( |
| 382 | "SELECT id, text, url |
| 383 | FROM {$globals->money->mpay_tprefix}paiements |
| 384 | WHERE asso_id = {?} AND NOT FIND_IN_SET('old', flags) |
| 385 | ORDER BY id DESC", $globals->asso('id')); |
| 386 | $tit = $res->fetchAllAssoc(); |
| 387 | $page->assign('titres', $tit); |
| 388 | |
| 389 | |
| 390 | // TODO: replug sort. |
| 391 | $trans = array(); |
| 392 | $event = array(); |
| 393 | foreach($tit as $foo) { |
| 394 | $pid = $foo['id']; |
| 395 | if (may_update()) { |
| 396 | $res = XDB::query('SELECT t.uid, timestamp AS `date`, t.comment, montant |
| 397 | FROM ' . $globals->money->mpay_tprefix . 'transactions AS t |
| 398 | WHERE t.ref = {?}', $pid); |
| 399 | $trans[$pid] = User::getBulkUsersWithUIDs($res->fetchAllAssoc(), 'uid', 'user'); |
| 400 | $sum = 0; |
| 401 | foreach ($trans[$pid] as $i => $t) { |
| 402 | $sum += strtr(substr($t['montant'], 0, strpos($t['montant'], 'EUR')), ',', '.'); |
| 403 | $trans[$pid][$i]['montant'] = str_replace('EUR', '€', $t['montant']); |
| 404 | } |
| 405 | $trans[$pid][] = array('nom' => 'somme totale', |
| 406 | 'montant' => strtr($sum, '.', ',').' €'); |
| 407 | } |
| 408 | $res = XDB::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid |
| 409 | FROM group_events AS e |
| 410 | LEFT JOIN group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?}) |
| 411 | INNER JOIN group_event_items AS ei ON (ep.eid = ei.eid AND ep.item_id = ei.item_id) |
| 412 | WHERE e.paiement_id = {?}", |
| 413 | S::v('uid'), $pid); |
| 414 | $event[$pid] = array(); |
| 415 | $event[$pid]['paid'] = 0; |
| 416 | if ($res->total()) { |
| 417 | $event[$pid]['topay'] = 0; |
| 418 | while(list($eid, $shortname, $title, $nb, $montant, $paid) = $res->next()) { |
| 419 | $event[$pid]['topay'] += ($nb * $montant); |
| 420 | $event[$pid]['eid'] = $eid; |
| 421 | $event[$pid]['shortname'] = $shortname; |
| 422 | $event[$pid]['title'] = $title; |
| 423 | $event[$pid]['ins'] = !is_null($nb); |
| 424 | $event[$pid]['paid'] = $paid; |
| 425 | } |
| 426 | } |
| 427 | $res = XDB::query("SELECT montant |
| 428 | FROM {$globals->money->mpay_tprefix}transactions AS t |
| 429 | WHERE ref = {?} AND uid = {?}", $pid, S::v('uid')); |
| 430 | $montants = $res->fetchColumn(); |
| 431 | |
| 432 | foreach ($montants as $m) { |
| 433 | $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.'); |
| 434 | $event[$pid]['paid'] += trim($p); |
| 435 | } |
| 436 | } |
| 437 | $page->register_modifier('decode_comment', 'decode_comment'); |
| 438 | $page->assign('trans', $trans); |
| 439 | $page->assign('event', $event); |
| 440 | } |
| 441 | |
| 442 | function handler_admin(&$page, $action = 'list', $id = null) { |
| 443 | $page->setTitle('Administration - Paiements'); |
| 444 | $page->assign('title', 'Gestion des télépaiements'); |
| 445 | $table_editor = new PLTableEditor('admin/payments','payments','id'); |
| 446 | $table_editor->add_join_table('payment_transactions','ref',true); |
| 447 | $table_editor->add_sort_field('flags'); |
| 448 | $table_editor->add_sort_field('id', true, true); |
| 449 | $table_editor->on_delete("UPDATE payments SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé"); |
| 450 | $table_editor->describe('text','intitulé',true); |
| 451 | $table_editor->describe('url','site web',false); |
| 452 | $table_editor->describe('amount_def','montant par défaut',false); |
| 453 | $table_editor->describe('amount_min','montant minimum',false); |
| 454 | $table_editor->describe('amount_max','montant maximum',false); |
| 455 | $table_editor->describe('mail','email contact',true); |
| 456 | $table_editor->describe('confirmation','message confirmation',false); |
| 457 | $table_editor->apply($page, $action, $id); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 462 | ?> |