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