2 /***************************************************************************
3 * Copyright (C) 2003-2010 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) {
26 $mymail = new PlMailer();
27 $mymail->addTo($globals->money
->email
);
28 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
29 $mymail->setSubject("erreur lors d'un télépaiement (CyberPaiement)");
30 $mymail->setTxtBody("\n\n".var_export($_REQUEST,true
));
32 echo "Notification sent.\n";
36 /* sort en affichant une erreur */
37 function paypal_erreur($text, $send=true
)
39 global $erreur, $globals;
44 $mymail = new PlMailer();
45 $mymail->addTo($globals->money
->email
);
46 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
47 $mymail->setSubject("erreur lors d'un télépaiement (PayPal)");
48 $mymail->setTxtBody("\n\n".var_export($_REQUEST,true
));
51 Platal
::page()->trigError($text);
54 /* http://fr.wikipedia.org/wiki/Formule_de_Luhn */
55 function luhn($nombre) {
58 for ($i = 0; $i < strlen($s); $i++
) {
60 $sum +
= ($i %
2) ?
(2*$dgt) %
9 : $dgt;
65 /* calcule la clé d'acceptation a partir de 5 champs */
66 function cle_accept($d1,$d2,$d3,$d4,$d5)
72 $n = $m1 +
$m2 +
$m3 +
$m4;
73 $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
74 return $alpha{$n-1}.$m1.$m2.$m3.$m4;
77 /* decode the comment */
78 function comment_decode($comment) {
79 $comment = urldecode($comment);
80 if (is_utf8($comment)) {
83 return utf8_encode($comment);
88 class PaymentModule
extends PLModule
93 'payment' => $this->make_hook('payment', AUTH_MDP
),
94 'payment/cyber_return' => $this->make_hook('cyber_return', AUTH_PUBLIC
, 'user', NO_HTTPS
),
95 'payment/cyber2_return' => $this->make_hook('cyber2_return', AUTH_PUBLIC
, 'user', NO_HTTPS
),
96 'payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC
, 'user', NO_HTTPS
),
97 '%grp/paiement' => $this->make_hook('xnet_payment', AUTH_MDP
),
98 '%grp/payment' => $this->make_hook('xnet_payment', AUTH_MDP
),
99 '%grp/payment/cyber_return' => $this->make_hook('cyber_return', AUTH_PUBLIC
, 'user', NO_HTTPS
),
100 '%grp/payment/cyber2_return' => $this->make_hook('cyber2_return', AUTH_PUBLIC
, 'user', NO_HTTPS
),
101 '%grp/payment/paypal_return' => $this->make_hook('paypal_return', AUTH_PUBLIC
, 'user', NO_HTTPS
),
102 'admin/payments' => $this->make_hook('admin', AUTH_MDP
, 'admin'),
107 function handler_payment(&$page, $ref = -1)
111 require_once 'profil.func.inc.php' ;
112 $this->load('money.inc.php');
114 if (!empty($GLOBALS['IS_XNET_SITE'])) {
115 if (!$globals->asso('id')) {
118 $res = XDB
::query("SELECT asso_id
120 WHERE asso_id = {?} AND id = {?}",
121 $globals->asso('id'), $ref);
122 if (!$res->numRows()) {
126 $page->changeTpl('payment/index.tpl');
127 $page->setTitle('Télépaiements');
130 $op = Env
::v('op', 'select');
131 $meth = new PayMethod(Env
::i('methode', -1));
132 $pay = new Payment($ref);
134 if($pay->flags
->hasflag('old')){
135 $page->trigError("La transaction selectionnée est périmée.");
136 $pay = new Payment();
138 $val = Env
::v('montant') != 0 ? Env
::v('montant') : $pay->amount_def
;
140 if (($e = $pay->check($val)) !== true
) {
141 $page->trigError($e);
145 $pay->init($val, $meth);
146 $pay->prepareform($pay);
148 $res = XDB
::iterator("SELECT timestamp, amount
149 FROM payment_transactions
150 WHERE uid = {?} AND ref = {?}
151 ORDER BY timestamp DESC",
152 S
::v('uid', -1), $ref);
154 if ($res->total()) $page->assign('transactions', $res);
157 $val = floor($val).".".substr(floor(($val - floor($val))*100+
100),1);
158 $page->assign('montant',$val);
159 $page->assign('comment',Env
::v('comment'));
161 $page->assign('meth', $meth);
162 $page->assign('pay', $pay);
163 $page->assign('evtlink', $pay->event());
166 function handler_cyber_return(&$page, $uid = null
)
168 /* reference banque (numero de transaction) */
169 $champ901 = Env
::s('CHAMP901');
170 /* cle d'acceptation */
171 $champ905 = Env
::s('CHAMP905');
173 $champ906 = Env
::s('CHAMP906');
174 /* email renvoye par la banque */
175 $champ104 = Env
::s('CHAMP104');
176 /* reference complete de la commande */
177 $champ200 = Env
::s('CHAMP200');
178 /* montant de la transaction */
179 $champ201 = Env
::s('CHAMP201');
181 $champ202 = Env
::s('CHAMP202');
182 $montant = "$champ201 $champ202";
184 /* on extrait les informations sur l'utilisateur */
185 $user = User
::get($uid);
187 cb_erreur("uid invalide");
191 /* on extrait la reference de la commande */
192 if (!ereg('-xorg-([0-9]+)$', $champ200, $matches)) {
193 cb_erreur("référence de commande invalide");
196 echo ($ref = $matches[1]);
197 $res = XDB
::query("SELECT mail, text, confirmation
199 WHERE id={?}", $ref);
200 if (!list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow()) {
201 cb_erreur("référence de commande inconnue");
204 /* on extrait le code de retour */
205 if ($champ906 != "0000") {
206 $res = XDB
::query('SELECT rcb.text, c.id, c.text
207 FROM payment_codeRCB AS rcb
208 LEFT JOIN payment_codeC AS c ON (rcb.codeC = c.id)
209 WHERE rcb.id = {?}', $champ906);
210 if (list($rcb_text, $c_id, $c_text) = $res->fetchOneRow()) {
211 cb_erreur("erreur lors du paiement : $c_text ($c_id)");
213 cb_erreur("erreur inconnue lors du paiement");
217 /* on fait l'insertion en base de donnees */
218 XDB
::execute("INSERT INTO payment_transactions (id, uid, ref, fullref, amount, pkey, comment)
219 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
220 $champ901, $user->id(), $ref, $champ200, $montant, $champ905, Env
::v('comment'));
222 // We check if it is an Xnet payment and then update the related ML.
223 $res = XDB
::query('SELECT eid
225 WHERE paiement_id = {?}', $ref);
226 if ($eid = $res->fetchOneCell()) {
227 require_once dirname(__FILE__
) . '/xnetevents/xnetevents.inc.php';
228 $evt = get_event_detail($eid);
229 subscribe_lists_event($uid, $evt, 1, $montant, true
);
232 /* on genere le mail de confirmation */
233 $conf_text = str_replace(
234 array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>', 'comment>'),
235 array($user->firstName(), $user->lastName(), $user->promo(), $montant,
236 $user->isFemale() ?
'Chère' : 'Cher', $user->isFemale() ?
'Chère' : 'Cher',
237 Env
::v('comment')), $conf_text);
240 $mymail = new PlMailer();
241 $mymail->setFrom($conf_mail);
242 $mymail->addCc($conf_mail);
243 $mymail->setSubject($conf_title);
244 $mymail->setWikiBody($conf_text);
245 $mymail->sendTo($user);
247 /* on envoie les details de la transaction à telepaiement@ */
248 $mymail = new PlMailer();
249 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
250 $mymail->addTo($globals->money
->email
);
251 $mymail->setSubject($conf_title);
252 $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" .
253 'mail : ' . $user->forlifeEmail() . "\n\n" .
254 "paiement : $conf_title ($conf_mail)\n".
255 "reference : $champ200\n".
256 "montant : $montant\n\n".
257 "dump de REQUEST:\n".
258 var_export($_REQUEST,true
);
259 $mymail->setTxtBody($msg);
264 function handler_cyber2_return(&$page, $uid = null
)
266 global $globals, $platal;
268 /* on vérifie la signature */
269 $vads_params = array();
270 foreach($_REQUEST as $key => $value)
271 if(substr($key,0,5) == "vads_")
272 $vads_params[$key] = $value;
274 $signature = sha1(join('+',$vads_params).'+'.$globals->money
->cyperplus_key
);
275 //if($signature != Env::v('signature')) {
276 // cb_erreur("signature invalide");
279 /* on extrait les informations sur l'utilisateur */
280 $user = User
::get(Env
::v('vads_cust_id'));
282 cb_erreur("uid invalide");
285 /* on extrait la reference de la commande */
286 if (!ereg('-([0-9]+)$', Env
::v('vads_order_id'), $matches)) {
287 cb_erreur("référence de commande invalide");
291 $res = XDB
::query("SELECT mail, text, confirmation
293 WHERE id={?}", $ref);
294 if (!list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow()) {
295 cb_erreur("référence de commande inconnue");
298 /* on extrait le montant */
299 if (Env
::v('vads_currency') != "978") {
300 cb_erreur("monnaie autre que l'euro");
302 $montant = sprintf("%.02f", ((float)Env
::v('vads_amount'))/100) . " EUR";
304 /* on extrait le code de retour */
305 if (Env
::v('vads_result') != "00") {
306 cb_erreur("erreur lors du paiement : ?? (".Env
::v('vads_result').")");
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 Env
::v('vads_trans_date'), $user->id(), $ref, Env
::v('vads_order_id'), $montant, "", Env
::v('vads_order_info'));
313 echo "Paiement stored.\n";
315 // We check if it is an Xnet payment and then update the related ML.
316 $res = XDB
::query('SELECT eid
318 WHERE paiement_id = {?}', $ref);
319 if ($eid = $res->fetchOneCell()) {
320 require_once dirname(__FILE__
) . '/xnetevents/xnetevents.inc.php';
321 $evt = get_event_detail($eid);
322 subscribe_lists_event($user->id(), $evt, 1, $montant, true
);
325 /* on genere le mail de confirmation */
326 $conf_text = str_replace(
327 array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>', 'comment>'),
328 array($user->firstName(), $user->lastName(), $user->promo(), $montant,
329 $user->isFemale() ?
'Chère' : 'Cher', $user->isFemale() ?
'Chère' : 'Cher',
330 Env
::v('comment')), $conf_text);
333 $mymail = new PlMailer();
334 $mymail->setFrom($conf_mail);
335 $mymail->addCc($conf_mail);
336 $mymail->setSubject($conf_title);
337 $mymail->setWikiBody($conf_text);
338 $mymail->sendTo($user);
340 /* on envoie les details de la transaction à telepaiement@ */
341 $mymail = new PlMailer();
342 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
343 $mymail->addTo($globals->money
->email
);
344 $mymail->setSubject($conf_title);
345 $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" .
346 'mail : ' . $user->forlifeEmail() . "\n\n" .
347 "paiement : $conf_title ($conf_mail)\n".
348 "reference : " . Env
::v('vads_order_id') . "\n".
349 "montant : $montant\n\n".
350 "dump de REQUEST:\n".
351 var_export($_REQUEST,true
);
352 $mymail->setTxtBody($msg);
354 echo "Notifications sent.\n";
358 function handler_paypal_return(&$page, $uid = null
)
360 $page->changeTpl('payment/retour_paypal.tpl');
362 /* reference banque (numero de transaction) */
363 $no_transaction = Env
::s('tx');
364 /* token a renvoyer pour avoir plus d'information */
365 $clef = Env
::s('sig');
367 $status = Env
::s('st');
369 $reason = ($status == 'Pending')? Env
::s('pending_reason'): Env
::s('reason_code');
370 /* reference complete de la commande */
371 $fullref = Env
::s('cm');
372 /* montant de la transaction */
373 $montant_nb = Env
::s('amt');
375 $montant_dev = Env
::s('cc');
376 $montant = "$montant_nb $montant_dev";
378 /* on extrait le code de retour */
379 if ($status != "Completed") {
381 paypal_erreur("erreur lors du paiement : $status - $reason");
383 paypal_erreur("Paiement annulé", false
);
386 /* on extrait les informations sur l'utilisateur */
387 $user = User
::get($uid);
389 paypal_erreur("uid invalide");
392 /* on extrait la reference de la commande */
393 if (!ereg('-xorg-([0-9]+)$', $fullref, $matches)) {
394 paypal_erreur("référence de commande invalide");
398 $res = XDB
::query("SELECT mail, text, confirmation
400 WHERE id = {?}", $ref);
401 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
402 paypal_erreur("référence de commande inconnue");
405 /* on fait l'insertion en base de donnees */
406 XDB
::execute("INSERT INTO payment_transactions (id, uid, ref, fullref, amount, pkey, comment)
407 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
408 $no_transaction, $user->id(), $ref, $fullref, $montant, $clef, Env
::v('comment'));
410 // We check if it is an Xnet payment and then update the related ML.
411 $res = XDB
::query('SELECT eid
413 WHERE paiement_id = {?}', $ref);
414 if ($eid = $res->fetchOneCell()) {
415 require_once dirname(__FILE__
) . '/xnetevents/xnetevents.inc.php';
416 $evt = get_event_detail($eid);
417 subscribe_lists_event($user->id(), $evt, 1, $montant, true
);
420 /* on genere le mail de confirmation */
421 $conf_text = str_replace(array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>'),
422 array($user->firstName(), $user->lastName(), $user->promo(), $montant,
423 $user->isFemale() ?
'Chère' : 'Cher',
424 $user->isFemale() ?
'Chère' : 'Cher'), $conf_text);
427 $mymail = new PlMailer();
428 $mymail->setFrom($conf_mail);
429 $mymail->addCc($conf_mail);
430 $mymail->setSubject($conf_title);
431 $mymail->setWikiBody($conf_text);
432 $mymail->sendTo($user);
434 /* on envoie les details de la transaction à telepaiement@ */
435 $mymail = new PlMailer();
436 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
437 $mymail->addTo($globals->money
->email
);
438 $mymail->setSubject($conf_title);
439 $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" .
440 'mail : ' . $user->forlifeEmail() . "\n\n" .
441 "paiement : $conf_title ($conf_mail)\n".
442 "reference : $champ200\n".
443 "montant : $montant\n\n".
444 "dump de REQUEST:\n".
445 var_export($_REQUEST,true
);
446 $mymail->setTxtBody($msg);
449 $page->assign('texte', $conf_text);
450 $page->assign('erreur', $erreur);
453 function handler_xnet_payment(&$page, $pid = null
)
457 $perms = S
::v('perms');
458 if (!$perms->hasFlag('groupmember')) {
462 $res = XDB
::query("SELECT 1
463 FROM group_events AS e
464 INNER JOIN group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?})
465 WHERE e.paiement_id = {?} AND e.asso_id = {?}",
466 S
::i('uid'), $pid, $globals->asso('id'));
467 if ($res->numRows() == 0) {
472 if (!is_null($pid)) {
473 return $this->handler_payment($page, $pid);
475 $page->changeTpl('payment/xnet.tpl');
478 "SELECT id, text, url
480 WHERE asso_id = {?} AND NOT FIND_IN_SET('old', flags)
481 ORDER BY id DESC", $globals->asso('id'));
482 $tit = $res->fetchAllAssoc();
483 $page->assign('titres', $tit);
486 // TODO: replug sort.
489 foreach($tit as $foo) {
492 $res = XDB
::query('SELECT t.uid, timestamp AS `date`, t.comment, amount
493 FROM payment_transactions AS t
494 WHERE t.ref = {?}', $pid);
495 $trans[$pid] = User
::getBulkUsersWithUIDs($res->fetchAllAssoc(), 'uid', 'user');
497 foreach ($trans[$pid] as $i => $t) {
498 $sum +
= strtr(substr($t['amount'], 0, strpos($t['amount'], 'EUR')), ',', '.');
499 $trans[$pid][$i]['amount'] = str_replace('EUR', '€', $t['amount']);
501 $trans[$pid][] = array('nom' => 'somme totale',
502 'amount' => strtr($sum, '.', ',').' €');
504 $res = XDB
::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid
505 FROM group_events AS e
506 LEFT JOIN group_event_participants AS ep ON (ep.eid = e.eid AND uid = {?})
507 INNER JOIN group_event_items AS ei ON (ep.eid = ei.eid AND ep.item_id = ei.item_id)
508 WHERE e.paiement_id = {?}",
510 $event[$pid] = array();
511 $event[$pid]['paid'] = 0;
513 $event[$pid]['topay'] = 0;
514 while(list($eid, $shortname, $title, $nb, $montant, $paid) = $res->next()) {
515 $event[$pid]['topay'] +
= ($nb * $montant);
516 $event[$pid]['eid'] = $eid;
517 $event[$pid]['shortname'] = $shortname;
518 $event[$pid]['title'] = $title;
519 $event[$pid]['ins'] = !is_null($nb);
520 $event[$pid]['paid'] = $paid;
523 $res = XDB
::query("SELECT amount
524 FROM payment_transactions AS t
525 WHERE ref = {?} AND uid = {?}", $pid, S
::v('uid'));
526 $montants = $res->fetchColumn();
528 foreach ($montants as $m) {
529 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
530 $event[$pid]['paid'] +
= trim($p);
533 $page->register_modifier('decode_comment', 'decode_comment');
534 $page->assign('trans', $trans);
535 $page->assign('event', $event);
538 function handler_admin(&$page, $action = 'list', $id = null
) {
539 $page->setTitle('Administration - Paiements');
540 $page->assign('title', 'Gestion des télépaiements');
541 $table_editor = new PLTableEditor('admin/payments','payments','id');
542 $table_editor->add_join_table('payment_transactions','ref',true
);
543 $table_editor->add_sort_field('flags');
544 $table_editor->add_sort_field('id', true
, true
);
545 $table_editor->on_delete("UPDATE payments SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
546 $table_editor->describe('text','intitulé',true
);
547 $table_editor->describe('url','site web',false
);
548 $table_editor->describe('amount_def','montant par défaut',false
);
549 $table_editor->describe('amount_min','montant minimum',false
);
550 $table_editor->describe('amount_max','montant maximum',false
);
551 $table_editor->describe('mail','email contact',true
);
552 $table_editor->describe('confirmation','message confirmation',false
);
554 // adds a column with the start date of the linked event if there is one
555 $table_editor->add_option_table('group_events','group_events.paiement_id = t.id');
556 $table_editor->add_option_field('group_events.debut', 'related_event', 'évènement', 'timestamp');
558 $table_editor->apply($page, $action, $id);
562 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: