2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 */
183 $user = User
::get($uid);
185 cb_erreur("uid invalide");
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");
194 echo ($ref = $matches[1]);
195 $res = XDB
::query("SELECT mail, text, confirmation
196 FROM paiement.paiements
197 WHERE id={?}", $ref);
198 if (!list($conf_mail, $conf_title, $conf_text) = $res->fetchOneRow()) {
199 cb_erreur("référence de commande inconnue");
202 /* on extrait le code de retour */
203 if ($champ906 != "0000") {
204 $res = XDB
::query('SELECT rcb.text, c.id, c.text
205 FROM paiement.codeRCB AS rcb
206 LEFT JOIN paiement.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)");
211 cb_erreur("erreur inconnue lors du paiement");
215 /* on fait l'insertion en base de donnees */
216 XDB
::execute("INSERT INTO paiement.transactions (id, uid, ref, fullref, montant, cle, comment)
217 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
218 $champ901, $user->id(), $ref, $champ200, $montant, $champ905, Env
::v('comment'));
220 /* on genere le mail de confirmation */
221 $conf_text = str_replace(array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>'),
222 array($user->firstName(), $user->lastName(), $user->promo(), $montant,
223 $user->isFemale() ?
'Chère' : 'Cher',
224 $user->isFemale() ?
'Chère' : 'Cher'), $conf_text);
227 $mymail = new PlMailer();
228 $mymail->setFrom($conf_mail);
229 $mymail->addCc($conf_mail);
230 $mymail->setSubject($conf_title);
231 $mymail->setWikiBody($conf_text);
232 $mymail->sendTo($user);
234 /* on envoie les details de la transaction à telepaiement@ */
235 $mymail = new PlMailer();
236 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
237 $mymail->addTo($globals->money
->email
);
238 $mymail->setSubject($conf_title);
239 $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" .
240 'mail : ' . $user->forlifeEmail() . "\n\n" .
241 "paiement : $conf_title ($conf_mail)\n".
242 "reference : $champ200\n".
243 "montant : $montant\n\n".
244 "dump de REQUEST:\n".
245 var_export($_REQUEST,true
);
246 $mymail->setTxtBody($msg);
251 function handler_paypal_return(&$page, $uid = null
)
253 $page->changeTpl('payment/retour_paypal.tpl');
255 /* reference banque (numero de transaction) */
256 $no_transaction = Env
::s('tx');
257 /* token a renvoyer pour avoir plus d'information */
258 $clef = Env
::s('sig');
260 $status = Env
::s('st');
262 $reason = ($status == 'Pending')? Env
::s('pending_reason'): Env
::s('reason_code');
263 /* reference complete de la commande */
264 $fullref = Env
::s('cm');
265 /* montant de la transaction */
266 $montant_nb = Env
::s('amt');
268 $montant_dev = Env
::s('cc');
269 $montant = "$montant_nb $montant_dev";
271 /* on extrait le code de retour */
272 if ($status != "Completed") {
274 paypal_erreur("erreur lors du paiement : $status - $reason");
276 paypal_erreur("Paiement annulé", false
);
279 /* on extrait les informations sur l'utilisateur */
280 $user = User
::get($uid);
282 paypal_erreur("uid invalide");
285 /* on extrait la reference de la commande */
286 if (!ereg('-xorg-([0-9]+)$', $fullref, $matches)) {
287 paypal_erreur("référence de commande invalide");
291 $res = XDB
::query("SELECT mail, text, confirmation
292 FROM paiement.paiements
293 WHERE id = {?}", $ref);
294 if (!list($conf_mail,$conf_title,$conf_text) = $res->fetchOneRow()) {
295 paypal_erreur("référence de commande inconnue");
298 /* on fait l'insertion en base de donnees */
299 XDB
::execute("INSERT INTO paiement.transactions (id, uid, ref, fullref, montant, cle, comment)
300 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
301 $no_transaction, $user->id(), $ref, $fullref, $montant, $clef, Env
::v('comment'));
303 /* on genere le mail de confirmation */
304 $conf_text = str_replace(array('<prenom>', '<nom>', '<promo>', '<montant>', '<salutation>', '<cher>'),
305 array($user->firstName(), $user->lastName(), $user->promo(), $montant,
306 $user->isFemale() ?
'Chère' : 'Cher',
307 $user->isFemale() ?
'Chère' : 'Cher'), $conf_text);
310 $mymail = new PlMailer();
311 $mymail->setFrom($conf_mail);
312 $mymail->addCc($conf_mail);
313 $mymail->setSubject($conf_title);
314 $mymail->setWikiBody($conf_text);
315 $mymail->sendTo($user);
317 /* on envoie les details de la transaction à telepaiement@ */
318 $mymail = new PlMailer();
319 $mymail->setFrom("webmaster@" . $globals->mail
->domain
);
320 $mymail->addTo($globals->money
->email
);
321 $mymail->setSubject($conf_title);
322 $msg = 'utilisateur : ' . $user->login() . ' (' . $user->id() . ')' . "\n" .
323 'mail : ' . $user->forlifeEmail() . "\n\n" .
324 "paiement : $conf_title ($conf_mail)\n".
325 "reference : $champ200\n".
326 "montant : $montant\n\n".
327 "dump de REQUEST:\n".
328 var_export($_REQUEST,true
);
329 $mymail->setTxtBody($msg);
332 $page->assign('texte', $conf_text);
333 $page->assign('erreur', $erreur);
336 function handler_xnet_payment(&$page, $pid = null
)
340 $perms = S
::v('perms');
341 if (!$perms->hasFlag('groupmember')) {
345 $res = XDB
::query("SELECT 1
346 FROM groupex.evenements AS e
347 INNER JOIN groupex.evenements_participants AS ep ON (ep.eid = e.eid AND uid = {?})
348 WHERE e.paiement_id = {?} AND e.asso_id = {?}",
349 S
::i('uid'), $pid, $globals->asso('id'));
350 if ($res->numRows() == 0) {
355 if (!is_null($pid)) {
356 return $this->handler_payment($page, $pid);
358 $page->changeTpl('payment/xnet.tpl');
361 "SELECT id, text, url
362 FROM {$globals->money->mpay_tprefix}paiements
363 WHERE asso_id = {?} AND NOT FIND_IN_SET('old', flags)
364 ORDER BY id DESC", $globals->asso('id'));
365 $tit = $res->fetchAllAssoc();
366 $page->assign('titres', $tit);
369 // TODO: replug sort.
372 foreach($tit as $foo) {
375 $res = XDB
::query('SELECT t.uid, timestamp AS `date`, t.comment, montant
376 FROM ' . $globals->money
->mpay_tprefix
. 'transactions AS t
377 WHERE t.ref = {?}', $pid);
378 $trans[$pid] = User
::getBulkUsersWithUIDs($res->fetchAllAssoc(), 'uid', 'user');
380 foreach ($trans[$pid] as $i => $t) {
381 $sum +
= strtr(substr($t['montant'], 0, strpos($t['montant'], 'EUR')), ',', '.');
382 $trans[$pid][$i]['montant'] = str_replace('EUR', '€', $t['montant']);
384 $trans[$pid][] = array('nom' => 'somme totale',
385 'montant' => strtr($sum, '.', ',').' €');
387 $res = XDB
::iterRow("SELECT e.eid, e.short_name, e.intitule, ep.nb, ei.montant, ep.paid
388 FROM groupex.evenements AS e
389 LEFT JOIN groupex.evenements_participants AS ep ON (ep.eid = e.eid AND uid = {?})
390 INNER JOIN groupex.evenements_items AS ei ON (ep.eid = ei.eid AND ep.item_id = ei.item_id)
391 WHERE e.paiement_id = {?}",
393 $event[$pid] = array();
394 $event[$pid]['paid'] = 0;
396 $event[$pid]['topay'] = 0;
397 while(list($eid, $shortname, $title, $nb, $montant, $paid) = $res->next()) {
398 $event[$pid]['topay'] +
= ($nb * $montant);
399 $event[$pid]['eid'] = $eid;
400 $event[$pid]['shortname'] = $shortname;
401 $event[$pid]['title'] = $title;
402 $event[$pid]['ins'] = !is_null($nb);
403 $event[$pid]['paid'] = $paid;
406 $res = XDB
::query("SELECT montant
407 FROM {$globals->money->mpay_tprefix}transactions AS t
408 WHERE ref = {?} AND uid = {?}", $pid, S
::v('uid'));
409 $montants = $res->fetchColumn();
411 foreach ($montants as $m) {
412 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
413 $event[$pid]['paid'] +
= trim($p);
416 $page->register_modifier('decode_comment', 'decode_comment');
417 $page->assign('trans', $trans);
418 $page->assign('event', $event);
421 function handler_admin(&$page, $action = 'list', $id = null
) {
422 $page->setTitle('Administration - Paiements');
423 $page->assign('title', 'Gestion des télépaiements');
424 $table_editor = new PLTableEditor('admin/payments','paiement.paiements','id');
425 $table_editor->add_join_table('paiement.transactions','ref',true
);
426 $table_editor->add_sort_field('flags');
427 $table_editor->add_sort_field('id', true
, true
);
428 $table_editor->on_delete("UPDATE paiement.paiements SET flags = 'old' WHERE id = {?}", "Le paiement a été archivé");
429 $table_editor->describe('text','intitulé',true
);
430 $table_editor->describe('url','site web',false
);
431 $table_editor->describe('montant_def','montant par défaut',false
);
432 $table_editor->describe('montant_min','montant minimum',false
);
433 $table_editor->describe('montant_max','montant maximum',false
);
434 $table_editor->describe('mail','email contact',true
);
435 $table_editor->describe('confirmation','message confirmation',false
);
436 $table_editor->apply($page, $action, $id);
440 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: