2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 /* sort en affichant une erreur */
23 function cb_erreur($text) {
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
));
34 /* sort en affichant une erreur */
35 function paypal_erreur($text, $send=true
)
37 global $erreur, $globals;
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
));
49 Platal
::page()->trigError($text);
52 /* http://fr.wikipedia.org/wiki/Formule_de_Luhn */
53 function luhn($nombre) {
56 for ($i = 0; $i < strlen($s); $i++
) {
58 $sum +
= ($i %
2) ?
(2*$dgt) %
9 : $dgt;
63 /* calcule la clé d'acceptation a partir de 5 champs */
64 function cle_accept($d1,$d2,$d3,$d4,$d5)
70 $n = $m1 +
$m2 +
$m3 +
$m4;
71 $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
72 return $alpha{$n-1}.$m1.$m2.$m3.$m4;
75 /* decode the comment */
76 function comment_decode($comment) {
77 $comment = urldecode($comment);
78 if (is_utf8($comment)) {
81 return utf8_encode($comment);
86 class PaymentModule
extends PLModule
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'),
103 function handler_payment(&$page, $ref = -1)
107 require_once 'profil.func.inc.php' ;
108 $this->load('money.inc.php');
110 if (!empty($GLOBALS['IS_XNET_SITE'])) {
111 if (!$globals->asso('id')) {
114 $res = XDB
::query("SELECT asso_id
115 FROM paiement.paiements
116 WHERE asso_id = {?} AND id = {?}",
117 $globals->asso('id'), $ref);
118 if (!$res->numRows()) {
122 $page->changeTpl('payment/index.tpl');
123 $page->setTitle('Télépaiements');
126 $op = Env
::v('op', 'select');
127 $meth = new PayMethod(Env
::i('methode', -1));
128 $pay = new Payment($ref);
130 if($pay->flags
->hasflag('old')){
131 $page->trigError("La transaction selectionnée est périmée.");
132 $pay = new Payment();
134 $val = Env
::v('montant') != 0 ? Env
::v('montant') : $pay->montant_def
;
136 if (($e = $pay->check($val)) !== true
) {
137 $page->trigError($e);
141 $pay->init($val, $meth);
142 $pay->prepareform($pay);
144 $res = XDB
::iterator("SELECT timestamp, montant
145 FROM paiement.transactions
146 WHERE uid = {?} AND ref = {?}
147 ORDER BY timestamp DESC",
148 S
::v('uid', -1), $ref);
150 if ($res->total()) $page->assign('transactions', $res);
153 $val = floor($val).".".substr(floor(($val - floor($val))*100+
100),1);
154 $page->assign('montant',$val);
155 $page->assign('comment',Env
::v('comment'));
157 $page->assign('meth', $meth);
158 $page->assign('pay', $pay);
159 $page->assign('evtlink', $pay->event());
161 $page->assign('prefix', $globals->money
->mpay_tprefix
);
164 function handler_cyber_return(&$page, $uid = null
)
166 /* reference banque (numero de transaction) */
167 $champ901 = Env
::s('CHAMP901');
168 /* cle d'acceptation */
169 $champ905 = Env
::s('CHAMP905');
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');
179 $champ202 = Env
::s('CHAMP202');
180 $montant = "$champ201 $champ202";
182 /* on extrait les informations sur l'utilisateur */
184 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET('femme', a.flags)
185 FROM auth_user_md5 AS a
186 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
187 WHERE a.user_id={?}", $uid);
188 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
189 cb_erreur("uid invalide");
193 /* on extrait la reference de la commande */
194 if (!ereg('-xorg-([0-9]+)$',$champ200,$matches)) {
195 cb_erreur("référence de commande invalide");
198 echo ($ref = $matches[1]);
199 $res = XDB
::query("SELECT mail,text,confirmation
200 FROM paiement.paiements
201 WHERE id={?}", $ref);
202 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
203 cb_erreur("référence de commande inconnue");
206 /* on extrait le code de retour */
207 if ($champ906 != "0000") {
208 $res = XDB
::query("SELECT rcb.text,c.id,c.text
209 FROM paiement.codeRCB AS rcb
210 LEFT JOIN paiement.codeC AS c ON rcb.codeC=c.id
211 WHERE rcb.id='$champ906'");
212 if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) {
213 cb_erreur("erreur lors du paiement : $c_text ($c_id)");
215 cb_erreur("erreur inconnue lors du paiement");
219 /* on fait l'insertion en base de donnees */
220 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
221 VALUES ({?},{?},{?},{?},{?},{?},{?})",
222 $champ901, $uid, $ref, $champ200, $montant, $champ905,Env
::v('comment'));
224 /* on genere le mail de confirmation */
225 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
226 $conf_text = str_replace("<nom>",$nom,$conf_text);
227 $conf_text = str_replace("<promo>",$promo,$conf_text);
228 $conf_text = str_replace("<montant>",$montant,$conf_text);
229 $conf_text = str_replace("<salutation>",$femme ?
"Chère" : "Cher",$conf_text);
230 $conf_text = str_replace("<cher>",$femme ?
"Chère" : "Cher",$conf_text);
233 $mymail = new PlMailer();
234 $mymail->setFrom($conf_mail);
235 $mymail->addTo("\"$prenom $nom\" <$forlife@" . $globals->mail
->domain
. '>');
236 $mymail->addCc($conf_mail);
237 $mymail->setSubject($conf_title);
238 $mymail->setWikiBody($conf_text);
239 $mymail->send(S
::v('mail_fmt') == 'html');
241 /* on envoie les details de la transaction à telepaiement@ */
242 $mymail = new PlMailer();
243 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
244 $mymail->addTo($globals->money
->email
);
245 $mymail->setSubject($conf_title);
246 $msg = "utilisateur : $prenom $nom ($uid)\n".
247 "mail : $forlife@polytechnique.org\n\n".
248 "paiement : $conf_title ($conf_mail)\n".
249 "reference : $champ200\n".
250 "montant : $montant\n\n".
251 "dump de REQUEST:\n".
252 var_export($_REQUEST,true
);
253 $mymail->setTxtBody($msg);
258 function handler_paypal_return(&$page, $uid = null
)
260 $page->changeTpl('payment/retour_paypal.tpl');
262 /* reference banque (numero de transaction) */
263 $no_transaction = Env
::s('tx');
264 /* token a renvoyer pour avoir plus d'information */
265 $clef = Env
::s('sig');
267 $status = Env
::s('st');
269 $reason = ($status == 'Pending')? Env
::s('pending_reason'): Env
::s('reason_code');
270 /* reference complete de la commande */
271 $fullref = Env
::s('cm');
272 /* montant de la transaction */
273 $montant_nb = Env
::s('amt');
275 $montant_dev = Env
::s('cc');
276 $montant = "$montant_nb $montant_dev";
278 /* on extrait le code de retour */
279 if ($status != "Completed") {
281 paypal_erreur("erreur lors du paiement : $status - $reason");
283 paypal_erreur("Paiement annulé", false
);
286 /* on extrait les informations sur l'utilisateur */
288 SELECT a.prenom,a.nom,a.promo,l.alias,FIND_IN_SET('femme', a.flags)
289 FROM auth_user_md5 AS a
290 INNER JOIN aliases AS l ON (a.user_id=l.id AND type!='homonyme')
291 WHERE a.user_id={?}", $uid);
292 if (!list($prenom,$nom,$promo,$forlife,$femme) = $res->fetchOneRow()) {
293 paypal_erreur("uid invalide");
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");
302 $res = XDB
::query("SELECT mail,text,confirmation
303 FROM paiement.paiements
304 WHERE id={?}", $ref);
305 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
306 paypal_erreur("référence de commande inconnue");
309 /* on fait l'insertion en base de donnees */
310 XDB
::execute("INSERT INTO paiement.transactions (id,uid,ref,fullref,montant,cle,comment)
311 VALUES ({?},{?},{?},{?},{?},{?},{?})",
312 $no_transaction, $uid, $ref, $fullref, $montant, $clef, Env
::v('comment'));
314 /* on genere le mail de confirmation */
315 $conf_text = str_replace("<prenom>",$prenom,$conf_text);
316 $conf_text = str_replace("<nom>",$nom,$conf_text);
317 $conf_text = str_replace("<promo>",$promo,$conf_text);
318 $conf_text = str_replace("<montant>",$montant,$conf_text);
319 $conf_text = str_replace("<salutation>",$femme ?
"Chère" : "Cher",$conf_text);
320 $conf_text = str_replace("<cher>",$femme ?
"Chère" : "Cher",$conf_text);
323 $mymail = new PlMailer();
324 $mymail->setFrom($conf_mail);
325 $mymail->addTo("\"$prenom $nom\" <$forlife@" . $globals->mail
->domain
. '>');
326 $mymail->addCc($conf_mail);
327 $mymail->setSubject($conf_title);
328 $mymail->setWikiBody($conf_text);
329 $mymail->send(S
::v('mail_fmt') == 'html');
331 /* on envoie les details de la transaction à telepaiement@ */
332 $mymail = new PlMailer();
333 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
334 $mymail->addTo($globals->money
->email
);
335 $mymail->setSubject($conf_title);
336 $msg = "utilisateur : $prenom $nom ($uid)\n".
337 "mail : $forlife@polytechnique.org\n\n".
338 "paiement : $conf_title ($conf_mail)\n".
339 "reference : $no_transaction\n".
340 "montant : $montant\n\n".
341 "dump de REQUEST:\n".
342 var_export($_REQUEST,true
);
343 $mymail->setTxtBody($msg);
346 $page->assign('texte', $conf_text);
347 $page->assign('erreur', $erreur);
350 function handler_xnet_payment(&$page, $pid = null
)
354 $perms = S
::v('perms');
355 if (!$perms->hasFlag('groupmember')) {
359 $res = XDB
::query("SELECT 1
360 FROM groupex.evenements AS e
361 INNER JOIN groupex.evenements_participants AS ep ON (ep.eid = e.eid AND uid = {?})
362 WHERE e.paiement_id = {?} AND e.asso_id = {?}",
363 S
::i('uid'), $pid, $globals->asso('id'));
364 if ($res->numRows() == 0) {
369 if (!is_null($pid)) {
370 return $this->handler_payment($page, $pid);
372 $page->changeTpl('payment/xnet.tpl');
375 "SELECT id, text, url
376 FROM {$globals->money->mpay_tprefix}paiements
377 WHERE asso_id = {?} AND NOT FIND_IN_SET('old', flags)
378 ORDER BY id DESC", $globals->asso('id'));
379 $tit = $res->fetchAllAssoc();
380 $page->assign('titres', $tit);
382 $order = Env
::v('order', 'timestamp');
383 $orders = array('timestamp', 'nom', 'promo', 'montant', 'comment');
384 if (!in_array($order, $orders)) {
385 $order = 'timestamp';
386 } elseif ($order == 'comment') {
387 $order = 't.comment';
389 $inv_order = Env
::v('order_inv', 0);
390 $page->assign('order', $order);
391 $page->assign('order_inv', !$inv_order);
393 if ($order == 'timestamp') {
394 $inv_order = !$inv_order;
398 $inv_order = ' DESC';
402 if ($order == 'montant') {
403 $order = 'LENGTH(montant) '.$inv_order.', montant';
406 $orderby = 'ORDER BY '.$order.$inv_order;
407 if ($order != 'nom') {
408 $orderby .= ', nom'; $inv_order = '';
410 $orderby .= ', prenom'.$inv_order;
411 if ($order != 'timestamp') {
412 $orderby .= ', timestamp DESC';
417 foreach($tit as $foo) {
420 $res = XDB
::query("SELECT IF(u.nom_usage<>'', u.nom_usage, u.nom) AS nom,
421 u.prenom, u.promo, a.alias, timestamp AS `date`, t.comment, montant
422 FROM {$globals->money->mpay_tprefix}transactions AS t
423 INNER JOIN auth_user_md5 AS u ON ( t.uid = u.user_id )
424 INNER JOIN aliases AS a ON ( t.uid = a.id AND a.type='a_vie' )
425 WHERE ref = {?} ".$orderby, $pid);
426 $trans[$pid] = $res->fetchAllAssoc();
428 foreach ($trans[$pid] as $i => $t) {
429 $sum +
= strtr(substr($t['montant'], 0, strpos($t['montant'], 'EUR')), ',', '.');
430 $trans[$pid][$i]['montant'] = str_replace('EUR', '€', $t['montant']);
432 $trans[$pid][] = array('nom' => 'somme totale',
433 'montant' => strtr($sum, '.', ',').' €');
435 $res = XDB
::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid
436 FROM groupex.evenements AS e
437 LEFT JOIN groupex.evenements_participants AS ep ON (ep.eid = e.eid AND uid = {?})
438 INNER JOIN groupex.evenements_items AS ei ON (ep.eid = ei.eid AND ep.item_id = ei.item_id)
439 WHERE e.paiement_id = {?}",
441 $event[$pid] = array();
442 $event[$pid]['paid'] = 0;
444 $event[$pid]['topay'] = 0;
445 while(list($eid, $shortname, $title, $nb, $montant, $paid) = $res->next()) {
446 $event[$pid]['topay'] +
= ($nb * $montant);
447 $event[$pid]['eid'] = $eid;
448 $event[$pid]['shortname'] = $shortname;
449 $event[$pid]['title'] = $title;
450 $event[$pid]['ins'] = !is_null($nb);
451 $event[$pid]['paid'] = $paid;
454 $res = XDB
::query("SELECT montant
455 FROM {$globals->money->mpay_tprefix}transactions AS t
456 WHERE ref = {?} AND uid = {?}", $pid, S
::v('uid'));
457 $montants = $res->fetchColumn();
459 foreach ($montants as $m) {
460 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
461 $event[$pid]['paid'] +
= trim($p);
464 $page->register_modifier('decode_comment', 'decode_comment');
465 $page->assign('trans', $trans);
466 $page->assign('event', $event);
469 function handler_admin(&$page, $action = 'list', $id = null
) {
470 $page->setTitle('Administration - Paiements');
471 $page->assign('title', 'Gestion des télépaiements');
472 $table_editor = new PLTableEditor('admin/payments','paiement.paiements','id');
473 $table_editor->add_join_table('paiement.transactions','ref',true
);
474 $table_editor->add_sort_field('flags');
475 $table_editor->add_sort_field('id', true
, true
);
476 $table_editor->on_delete("UPDATE paiement.paiements SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
477 $table_editor->describe('text','intitulé',true
);
478 $table_editor->describe('url','site web',false
);
479 $table_editor->describe('montant_def','montant par défaut',false
);
480 $table_editor->describe('montant_min','montant minimum',false
);
481 $table_editor->describe('montant_max','montant maximum',false
);
482 $table_editor->describe('mail','email contact',true
);
483 $table_editor->describe('confirmation','message confirmation',false
);
484 $table_editor->apply($page, $action, $id);
488 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: